Re: fork problems (zombie children)

Larry 'Daffy' Daffner (vizzie@airmail.net)
Wed, 08 May 1996 19:04:45 -0500


Thomas Zerucha writes:
-> What am I doing wrong? The following program:
->
->
-> when run with PID=8704 creates children that stay undead until the parent
-> process terminates, e.g. from "ps -x":
->
-> 8704 v14 8 0:00 ./forktest
-> 8705 v14 8 0:00 (forktest) Z 8704
-> 8707 v14 8 0:00 (forktest) Z 8704
-> 8708 v14 8 0:00 (forktest) Z 8704
-> 8710 v14 8 0:00 (forktest) Z 8704
-> 8712 v14 8 0:00 (forktest) Z 8704
-> 8714 v14 8 0:00 (forktest) Z 8704
-> 8716 v14 8 0:00 (forktest) Z 8704
-> 8717 v14 8 0:00 (forktest) Z 8704

These are all zombies. IE, they're waiting for their parent to pick
them up, and the process will stay around until the parent waits for
them. So, try:

-> #include <signal.h>
#include <sys/types.h>
-> #include <sys/wait.h>
-> main(){
int status;

-> for(;;) {
-> if( !fork() ) {
-> exit(0); /* child, exit immediately */
-> }
wait(&status);
-> sleep(1);
-> }
-> }
The additions should cause the children to exit before the sleep.

Please take any further replies/questions off the linux-kernel list,
as this isn't an appropriate topic.

-Larry