regarding fork() multiple process
stil a newbie,trying to fork multiple child but the program does not go as i want...
My code : Code:
#include <stdio.h> Parent, my pid is 6461: Spawned child #1 whose pid is 6462 Parent, my pid is 6461: Spawned child #2 whose pid is 6463 I am child #1, my pid is 6462; my parent's pid is 6461 I am child #2, my pid is 6463; my parent's pid is 6461 I am child #3, my pid is 6464; my parent's pid is 6461 Parent, my pid is 6461: Spawned child #3 whose pid is 6464 but my output runs lik Parent,my pid is 1907:Spawned child 0 whose pid is 2910 I am child 0,my pid 2911,my parent's pid is 2910 Parent,my pid is 1907:Spawned child 1 whose pid is 2910 Parent,my pid is 2910:Spawned child 1 whose pid is 2911 Parent,my pid is 1907:Spawned child 2 whose pid is 2910 Parent,my pid is 2910:Spawned child 2 whose pid is 2911 I am child 1,my pid 2913,my parent's pid is 1 Parent,my pid is 1:Spawned child 2 whose pid is 2913 sharina@ubuntu:~$ I am child 2,my pid 2916,my parent's pid is 1 I am child 2,my pid 2915,my parent's pid is 1 I am child 2,my pid 2914,my parent's pid is 1 I am child 1,my pid 2912,my parent's pid is 1 Parent,my pid is 1:Spawned child 2 whose pid is 2912 I am child 2,my pid 2917,my parent's pid is 1 can anyone point out the logic? thx in advance.. |
Quote:
We are able to read stil, wan, lik, and thx. But that style is discouraged here and next time might result in significantly delaying the useful answers. This time, your main bug seems to be that you didn't put in a break statement after printing that the code is executing in the child. The output you say you want seems to assume that only the original parent will continue through the loop forking more children. But as you coded it, each process (parent or child) independently continues through the loop. But you also have several errors in the values you are printing and/or misunderstandings of the meanings of getpid() and getppid(). For example: Quote:
Then you say "Spawned child" i, but i is 0 to 2 representing positions in the pid[] array, and you say you want 1 based reporting, which would be "Spawned child" i+1. Then you say "whose pid is" getpid() but that is the current process pid, which is the parent from the point of view of the last fork. You should have "whose pid is" pid[i] |
sory...my bad,1st time posting this forum. i will make it more readable next time. May i know how to exit the condition? i know is using exit(-1) or exit(1), but i tried before and when i compile it gets an error of using the exit() status.sorry im still a newbie..
|
Quote:
But if you prefer to use the exit() function, that is also good. exit() is declared in stdlib.h, so if you want to use exit() you need #include <stdlib.h> Corrected code (using break) is Code:
#include <stdio.h> |
thanks to you johnsfine, somehow i managed i get some idea, now i understand much about the logic. According the code,I dun understand this part
Code:
printf("Parent,my pid is %d:Spawned child %d whose pid is %d\n",getpid(),i+1,pid[i]); |
alright, after been thinking awhile, I finally how it works, thanks johnfine. Now I know parent can only call getpid() because it stand for the process id itself instead of parent,because parent only have 1, however child has a parent pid and its own pid, so 2 system call. Thousand thanks^^
|
Quote:
Quote:
|
OK.But I got 1 last problem, the output show
Code:
Parent,my pid is 8602:Spawned child 1 whose pid is 8603 Code:
I am child 3,my pid 8605,my parent's pid is 1 |
Quote:
Have a read of the man-page for wait() There's an example program at the end which should help. |
Quote:
You get that behavior because parent process exits before the child process asks the system for the identity of its parent. getppid() is giving you the pid of the parent at the moment getppid() is called, not the original parent. If the original parent is gone then getppid() returns 1. |
Ok, i understand what's the problem. But when i change
Code:
i <= 3 |
Changing the conditional from '<' to '<=' really doesn't have any relevance to the problem you're seeing.
You can change your approach and pass the parents pid across the fork() instead of letting each child call getppid() Code:
#include <unistd.h> Code:
#include <unistd.h> |
Quote:
Code:
else if ( WIFEXITED(status) ) |
It's a macro from wait.h provided to be used with the wait() call. It's all on the man-page.
|
Quote:
google WIFEXITED and many of the hits are online copies of man 3 wait such as the one at: http://linux.die.net/man/3/wait In that man page you can find the description of WIFEXITED Code:
the following macros, which are defined in <sys/wait.h> Quote:
man 3 wait Or the OP might not have that man page installed on his Linux system (I don't have it on any of my Linux systems. Not sure why. Most man 3 pages I've tried are there.) When missing a man page, google (to find an online copy of the man page) is usually faster than finding out what package you need to install. |
All times are GMT -5. The time now is 12:17 AM. |