Results 1 to 4 of 4
Hi,
i was trying to play with fork,exec and signal for spawning multiple new shells, but it seems that i'm doing blunder somewhere.
<sample code>
1 /*
2 * The ...
- 04-30-2008 #1
fork multiple shells
Hi,
i was trying to play with fork,exec and signal for spawning multiple new shells, but it seems that i'm doing blunder somewhere.
<sample code>
1 /*
2 * The idea is to fork multpile(equal to $ULIMIT) childs
3 * and replace their images with process:csh
4 * during this the parent will save the pids of all the spawned childs
5 * till count reaches $ULIMIT and will start killing all the
6 * childs.
7 */
8
9 /*
10 * The implementation to store the PIDs of all the child
11 * forked here is using an array
12 */
13
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <errno.h>
20
21 #define ULIMIT 2
22 int main(void){
23 pid_t pid;
24 int count;
25 int ret;
26 int childpids[ULIMIT];
27 for(count = 0; count < ULIMIT; count++){
28 pid = vfork();
29 if(pid < 0){
30 perror("fork error");
31 exit(1);
32 }
33 if(pid == 0){ /*In child,Replace it with a new process: csh*/
34 ret = execlp("csh", "/bin/csh",(char *)0);
35 if(ret){
36 fprintf(stderr,"could not exec csh for count: %d\n:",count);
37 exit(1);
38 }
39 }
40 else{ /*In Parent*/
41 printf("child's pid is %d\n",pid);
42 printf("count reached = %d\n",count);
43 childpids[count] = pid;
44 }
45 }
/* Now start killing all the childs(c- shells)*/
46 while(--count){
47 if(kill(SIGKILL,childpids[count]) == -1){
48 perror("kill error");
49 exit(1);
50 }
51 }
52 kill(SIGKILL,childpids[count]);
53 exit(EXIT_SUCCESS);
54 }
<sample code>
root@localhost ]# gcc -Wall -Werror fork-csh.c -o fork-csh
[root@localhost ]# ./fork-csh
# child id is 14318
[1]+ Stopped ./fork-csh
[root@localhost ]# ps -el|grep csh|grep -v grep
4 T 0 14317 12639 0 75 0 - 344 finish pts/1 00:00:00 fork-csh
4 Z 0 14318 14317 0 80 0 - 0 do_exi pts/1 00:00:00 csh <defunct>
4 T 0 14319 14317 0 75 0 - 421 finish pts/1 00:00:00 csh
instead of my logic working i get "zombie process"
please let me know how to let my logic working.
~amit
- 04-30-2008 #2
After the parent does a fork(), it receives the pid of the child process. You already understand that part.
But when a child process finishes, it could be because the parent killed it (as you want to do) or for some other reason. The parent can find out that reason by calling waitpid(). Until the parent process does this for a child process that has finished, the child process doesn't entirely go away; enough of it remains to retain the information that the parent process can receive by doing the waitpid(). That is what a zombie process is.
When the waitpid() reports to the parent what the final status of the child is, then the child process really and truly disappears.
For more information, do this at the shell prompt:
If man pages are not installed on your system, google this:Code:man waitpid
Hope this helps.Code:linux man waitpid
--
Bill
Old age and treachery will overcome youth and skill.
- 04-30-2008 #3
Thanks Bill for throwing the light here

Now i think i have to change a lot in my stupid code.
i will come back with more on this,
thanks again for the pointer.
~amit
- 05-03-2008 #4Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
All that exists of a zombie process is its entry in the process table which, as Bill has pointed out, contains the exit status. All the time the parent process exists, the "zombie" entry will remain until the exit status is read. Killing the parent will get rid of the child, because orphaned processes are inherited by the init process, which will read the exit status. Any wait process will do, it doesn't have to be a waitpid.


Reply With Quote