943,681 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1487
  • C RSS
Aug 18th, 2009
0

how to fork a grandchild process

Expand Post »
Im having some trouble in forking. All i want to do is to create a child process and under the child process i need to create a grand child..here's my code


Quote ...

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main(int argc, char *argcv[])
{
int val;
val =fork();

if(val==0){
printf("Child process running\n");
printf("Child:%d PID:%d\n",val,getpid());

val=fork();
if(val==0){
printf("Grand child created\n");
printf("Grand child:%d PID:%d\n",val,getpid());


}
}
else if(val>0){
printf("Parent process running\n");
printf("Parent:%d PID:%d\n",val,getpid());
}
else{
printf("Error\n");
}
return 0;
}
Once i run the program the grand child and the child process has a different PID. what am i doing wrong? thanks guy
Last edited by herms14; Aug 18th, 2009 at 9:27 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
herms14 is offline Offline
46 posts
since Mar 2008
Aug 18th, 2009
0

Re: how to fork a grandchild process

I think they should have different pid's
Reputation Points: 11
Solved Threads: 2
Light Poster
kinger29 is offline Offline
35 posts
since Mar 2008
Aug 23rd, 2009
0

Re: how to fork a grandchild process

Click to Expand / Collapse  Quote originally posted by kinger29 ...
I think they should have different pid's

waht do you mean by different pid's/ should i declare a new fork? what shoud i do to create a new grand child
Reputation Points: 10
Solved Threads: 0
Light Poster
herms14 is offline Offline
46 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: can't get fopen to work
Next Thread in C Forum Timeline: File Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC