473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

creating child process using fork function

Hi

I have to develop a c program to create a parent process that spawn three child process. i have written a code but that was not working out.Please help me in getting the desired output of my program. Here is my code:
#include <stdio.h>
int main(void)
{
int pid;
pid = fork();
if(pid > 1)
{
printf("I AM THE PARENT PROCESS\n");
}
else if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
pid = fork();
if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
else if(pid > 0)
{

pid = fork();

if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
}
}
else
{

printf("couldn't fork");
}
}

Thanks
Sowmya
Apr 2 '07 #1
5 10978
arne
315 Expert 100+
Hi

I have to develop a c program to create a parent process that spawn three child process. i have written a code but that was not working out.Please help me in getting the desired output of my program. Here is my code:
#include <stdio.h>
int main(void)
{
int pid;
pid = fork();
if(pid > 1)
{
printf("I AM THE PARENT PROCESS\n");
}
else if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
pid = fork();
if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
else if(pid > 0)
{

pid = fork();

if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
}
}
else
{

printf("couldn't fork");
}
}

Thanks
Sowmya

Your code looks ok to me (in terms of: it should work, please use code tags next time to make it more readable). The only minor issue is that you're forking from the child instead of from the parent, which would be the more usual case.

You may also want to have a look at the getpid() function to verify that all messages are coming from different processes.

Do you have any specific problem?
Apr 2 '07 #2
Your code looks ok to me (in terms of: it should work, please use code tags next time to make it more readable). The only minor issue is that you're forking from the child instead of from the parent, which would be the more usual case.

You may also want to have a look at the getpid() function to verify that all messages are coming from different processes.

Do you have any specific problem?

Hi arne,

Thanks for your suggestion..

I will make the required changes .

Thanks ,
Sowmya.
Apr 2 '07 #3
Hi,

My program should return value 0 for child process ,but i am getting a bigger value ,can you please help me in this regard.Here is my code:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<unistd.h>
  4.  main()
  5. {
  6.         int pid,return_value;
  7.         pid=fork();
  8.         if(pid == 0)
  9.         {
  10.                 printf("I AM THE CHILD PROCESS\n");
  11.                 printf("The process id is %d and the return value is %d\n",getpid(),return_value);
  12.         }
  13.         else if(pid > 0)
  14.         {
  15.                 printf("I AM THE PARENT PROCESS\n");
  16.                 printf("The process id is %d \n",getpid());
  17.         pid=fork();
  18.                  if(pid == 0)
  19.                 {
  20.                         printf("I AM THE CHILD PROCESS\n");
  21.                         printf("The process id1 is %d and the return value is %d\n",getpid(),return_value);
  22.                 }
  23.                 else if(pid > 0)
  24.                 {
  25.                 pid = fork();
  26.                 if(pid == 0)
  27.         {
  28.                         printf("I AM THE CHILD PROCESS\n");
  29.                         printf("The process id2 is %d and the return value is %d\n",getpid(),return_value);
  30.                         }
  31.                         else
  32.                         {
  33.                                 printf("couldn't fork");
  34.                         }
  35.                 }
  36.         }
  37. }
Apr 3 '07 #4
arne
315 Expert 100+
Hi,
My program should return value 0 for child process ,but i am getting a bigger value ,can you please help me in this regard.
This is because you actually never assign a value to 'return_value', so it's undefined. (The return value of fork() you already assign to 'pid', no need to use another variable anyway.)

What does getpid() say about the PIDs (process IDs) of your processes? Should work fine, no?
Apr 3 '07 #5
Hi arne.

I got the desired output i guess.Here is my output.
I AM THE CHILD PROCESS and return value is 0
I AM THE PARENT PROCESS and return value is 8869 and process id 26585
I AM THE CHILD PROCESS and return value is 0
I AM THE CHILD PROCESS and return value is 0

Thanks,
Sowmya.
Apr 4 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Clunixchit | last post by:
am I lame ?? sounds like i didnt grab something !!! ive changed my code now!! whats wrong?? y for 2 loops, it prints 3 loops? #include <stdio.h> #include <sys/types.h> #include <sys/wait.h>...
3
by: Bo | last post by:
In my asp.net webservice application, I need to launch a DOS process as authorized users. To impersonate users, I use <impersonation = true> in my webconfig. I can't use Diagnostics.Process.Start,...
7
by: newgoat | last post by:
I just wrote a short program to see process switch when sleep() is invoked within a process. The code is as follows: #include<stdio.h> #include<unistd.h> #include<sys/types.h>...
2
by: Fred Heida | last post by:
Hi, Question on Vista Run As Administrator. If i have exe which creates a child process, using the Process class, and this exe is Run As Asministror, is there a way to have the child process...
1
by: vasu1308 | last post by:
Hi all, I want know about writing a program that spwans 3 child processes using fork() function call. Thanks in advance Vasu
3
by: raghav82 | last post by:
For sending the message in linux environment- the perl script however uses fork() to release the client process as soon as possible and then in the child process handles the message send. For...
3
by: LH2007 | last post by:
hi everybody!I'm newbie.I have a exercise about perl like this: Creating 2 process P1(parent) P2 (child) P1 get characters from keyboard If press Ctrl-c P1 send a signal terminate P2...
0
by: eso40043 | last post by:
Hello, due to a memory leak (bug, I guess) in pyraf(or rather in IRAF) I have to fork an iterative process that goes through hundreds of image frames and does unspeakable things to them. In the...
1
by: rparimi | last post by:
Hello pythoners, When I create temporary file using the tempfile module, and forkI) later on in my program, I always see errors when the program exits. Is this because the child process deletes...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.