Matthew Gregan wrote:
This is how I remember it being.On Thu, Jan 16, 2003 at 09:30:02AM +1300, Zane Gilmore wrote:There are also processes that become what are called zombies. That is when their parent process dies and they are left running.A process becomes a zombie when it dies. It stays in the zombie state until the parent process calls one of the wait() family of calls (wait(), waitpid(), etc.) on the child and receives the child's exit status.These are impossible to remove without a reboot.No. The parent process needs to call wait() on the zombie child to receive its exit status, then the zombie is cleaned up by the kernel. If the parent dies, the child is reparented to init, which will wait() on the reparented child and thus cause it to be cleaned up by the kernel.
But look at this short program:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int zombies = 5;
int child = 0;
while (zombies && !child) {
child = fork();
zombies--;
}
if (child) {
printf("Child pid: %d\n",child);
sleep(1);
printf("Child finished pid: %d\n",child);
return 0;
} else {
printf("parent\n");
sleep(10);
printf("parent finished\n");
return 0;
}
}
Save it a file "zombie.c", compile with "gcc -o zombie zombie.c", and
run with ./zombie (love that command line!)
I would expect it to produce 5 zombies after 1 second (when the 5 child
processes exit). The children aren't waited for, for another 9 seconds, but top doesn't have 5 zombies. The last child, though, hangs around in
the stopped state for until the parent finishes.
What's going on? Have I missed something? I don't have any relevant
books handy....
Cheers,
Carl.
