Re: Zombie curiosity
Re: Zombie curiosity
- Subject: Re: Zombie curiosity
- From: Justin Walker <email@hidden>
- Date: Wed, 1 Sep 2004 08:03:54 -0500
At the risk of prolonging this thread beyond its useful life, I've
cobbled up a small C program that illustrates what I've been talking
about (appended to this mail).
If you run this program with no arguments, it forks two child processes
and exits. The children sleep for a specified time, and then exit. In
the interim, in another window, run something like "ps -oppid -aux" and
you will see the two processes listed as children of init. After they
exit, there will be no zombies (because init cleans things up quickly;
it's possible you can see them as zombies if you are lucky, but it's
unlikely).
If you run this program with argument 'w', the parent will wait for the
children, and reap them as they exit. Running the above 'ps' while the
children sleep will show them as children of the parent. After the
parent exits, there will be no zombies.
If you run this program with argument 'r', the parent will sleep longer
than the children sleep, and then reap them. Running 'ps' with the
children sleeping will show them as children of the parent process.
Running 'ps' after the children exit, but before the parent wakes will
show two zombie processes whose parent is the parent process. Once the
parent wakes and reaps the children, there will be no zombies.
I hope this helps explain what's going on.
Regards,
Justin
==========================zombie.c===============================
/*
* Example to show how zombies are treated
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int SleepTime = 30, WaitForKids=0;
main(int argc, char *argv[])
{ int pidA, pidB, ReapTime = 0;
if (argc > 1)
{ if (argv[1][0]=='w')
WaitForKids=1;
else if (argv[1][0] == 'r')
{ WaitForKids=1;
ReapTime = 20;
}
}
if ((pidA = fork()) < 0)
{ perror("FORK1");
exit(-1);
} else if (pidA) /* in Parent */
{ printf("Spawned %d\n", pidA);
if ((pidB = fork()) < 0)
{ perror("FORK2");
exit(-1);
} else if (pidB) /* in Parent */
{ printf("Spawned %d\n", pidB);
if (WaitForKids == 0)
exit(0);
} else /* In child B */
{ printf("Child B...");fflush(stdout);
sleep(SleepTime);
printf("Slept\n");
exit(0);
}
} else /* In child A */
{ printf("Child A...");fflush(stdout);
sleep(SleepTime);
printf("Slept\n");
exit(0);
}
ReapEm(ReapTime);
}
ReapEm(int ReapTime)
{ int Status, rval;
if (ReapTime)
{ printf("Sleeping...");fflush(stdout);
sleep(ReapTime+SleepTime);
}
while ((rval = wait(&Status)) != -1)
printf("WAIT: %d, %x\n", rval, Status);
printf("WAIT: %d\n", errno);
}
--
Justin C. Walker, Curmudgeon-At-Large *
Institute for the Enhancement | When LuteFisk is outlawed
of the Director's Income | Only outlaws will have
| LuteFisk
*--------------------------------------*-------------------------------*
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.