![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| fork() help | alexicopax | High Level Programming | 3 | 03-08-2007 01:08 AM |
| shm sem fork etc... Please help | Dana73 | High Level Programming | 1 | 02-28-2006 05:51 AM |
| Fork or what? | crippe | High Level Programming | 0 | 03-08-2005 02:21 AM |
| fork() | MKSRaja | High Level Programming | 2 | 02-07-2005 08:55 AM |
| Fork | Deepali | UNIX for Dummies Questions & Answers | 5 | 08-26-2001 05:14 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
hi all
About this code for (i = 1; i < n; i++) if ((childpid = fork()) <= 0) break; I really can't understand the output . and the way fork () return the value . how about the process Id ,the child process Id and the parent ID in this case so please answer me soon |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Quote:
That is it returns once in the child with return code of 0, and return once in parent with retcode of the child pid, if all went right and if my memories didnt fail agen |
|
#3
|
|||
|
|||
|
Quote:
Regards, |
|
#4
|
|||
|
|||
|
Quote:
If it all if there is any anything to be done by the child there would be only 2^(n-1)-2 child processes and 1 parent process as for validates between and 1 and < n |
|
#5
|
|||
|
|||
|
People try to be a little too clever with fork sometimes... whenever I use it I try to write my code like this:
Code:
pid_t pid=fork();
if(pid > 0)
{
int status;
/* This process has created an independent child process */
do_other_stuff();
/* Wait for it to finish so it doesn't become a zombie */
wait(&status);
}
if(pid == 0)
{
/* This process IS the child process! */
do_stuff();
/* Exit the program when we're done doing stuff */
exit(0);
}
else
{
/* No child process was created because of some sort of error */
}
|
|
#6
|
|||
|
|||
|
Quote:
pid_t pid; if( (pid=fork()) < 0 ) { /* Error Stuff */ } else if(pid == 0 ) { /* child stuff */ } else { /* parent stuff */ } the above will not mandate two if-condition checks |
|||
| Google The UNIX and Linux Forums |