From: jbii@HDFS1.acd.com ( John O. Bell II ) Subject: Zombie processes, and what to do about them... Date: Sun, 14 Feb 1993 02:16:55 GMT
In article <1krc56$47u@sixgun.East.Sun.COM> tgendron@caneel.East.Sun.COM writes:
>In article 4v98Gibj55@s-link.escape.de, benj@s-link.escape.de (Benjamin Harling) writes:
>
>>How do I get rid of a zombie (Z) process? Kill -9 PID doesn't work.
>>
>A zompie occurs when the child is left on the doorstep by it's parrent.
>That is, the porcess has terminated but the parent is not waiting. This is
>from the man page for ps (SunOS 5.1). If you do a ls -l you can determine the
>parent's pid. Kill the parent and the zombie should go away.
This is the straight skinny on zombie processes (much thanks to the article
in the March 1993 issue of UNIXWORLD, written by Ray Swartz):
Processes are created on the UNIX system by use of the fork(2) system call.
When a fork() call is made, a new entry is made in the process table that
corresponds to the newly forked process. One of the pieces of data stored
in the process table is the ID number of the parent process that created
the forked process.
A process moves in and out of various execution states throughout its
lifetime. When a process terminates, it enters what is called the "zombie"
state. A process in the zombie state is said to be "defunct".
In the zombie state, a processes' entry in the process table is replaced
with its exit status, and the user and system time required to run it. This
information is placed in the process table so the kernel can store it until
it is able to report it to the parent process. Thus, a defunct (zombie)
process entry exists whenever a child process terminates.
A child's exit status is returned to its parent process only if the
parent process waits for it by executing a wait(2) system call. When a
child's exit status is reported to its parent, the child's process table
entry is removed.
If the parent never calls a wait(), the zombie child stays in the process
table until the parent process terminates. When a process terminates, all of
that processes' children are inherited by the init process. When a zombie
is inherited by init, it is removed from the process table.
In conclusion, if you have a defunct process listed when you do a ps, it
is most likely a process of a still-executing parent. It can't be killed
because a zombie process is never scheduled to run, and thus will never
receive the signal you're sending it (hence the name "zombie"... you can't
kill something that is already dead :-) ).
Here is a neat program to try (also provided in the article) which can
illustrate what you are running up against. The name of the program is
mkzombie.c ...
#include <stdio.h>
main() {
switch(fork()) {
case 0:
exit(0);
default:
sleep(60);
}
}
After forking a child, the program sleeps for a minute. Because the child
process exits immediately (by way of exit(0) being the only instruction
executed for case 0:), it is in a zombie state for the entire minute that
its parent is sleeping. To see this, run the program in the background and
then do a ps -ef (or just a ps on a Sun system). You'll see the child
zombie in the process table with its state listed as Z. As an exercise, you
can go ahead and try to kill the zombie process; you'll notice that this
doesn't work, and it only goes away after the parent (i.e. the main program)
finishes execution.
Hope this helps clear things up a bit on zombie/defunct processes...
John Bell
Applied Computing Devices, Inc.
jbii@hdfs1.acd.com