Nov 032009

A zombie, or defunct, process is a process that has terminated, but its parent process has not taken it out of the process table with the wait() family of system calls. This typically happens when a program forks/execs another program, but then doesn’t reap it properly. The easiest way to write the parent to properly reap its children is to setup a signal handler for SIGCHLD that calls wait().

See the man pages on your local system for signal and wait(2).

Zombie and defunct states of a process are different things. Zombie is created when a child dies and parent didn’t call wait. Defunct is created when parent dies before the child finishes.

Defunct processes can’t be killed since they are already dead. To make them disappear you have to kill their parent process…

It’s not possible to kill defunct processes.

A good way to kill lists of processes is:

$ ps axf | grep name-of-process | grep -v -grep | awk '{print "kill -9 ",$1}' | sh

A way to find the parent process of defunct processes is:

$ ps axf | grep defunct | grep -v -grep | awk '{print "kill -9 ",$1}' | sh

Probably the first search will destroy the parent of all defunct processes and recursive subcalls will produce an error because neither the child nor the parent does not exist anymore after deleting the first found parent.

You should not pipe the output to ksh you should pipe it into a file an manual edit the file because we should know what to delete.

Small code that simulate a Zombie process

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main ()
 pid_t child_pid;

 child_pid = fork ();
 if (child_pid > 0) {
   sleep (60);
 else {
   exit (0);
 return 0;

Save it as zombie.c, compile it with:

cc zombie.c -o zombie

and run it with:

$ ./zombie

Now check the output of this command (within 1 minute) in another console window:

$ ps axf | grep zombie | grep -v grep
19245 pts/1    S+     0:00          |           \_ ./zombie
19246 pts/1    Z+     0:00          |               \_ [zombie] <defunct>

The child process is marked as <defunct>, and its status code is Z, for zombie. When the program exits, the child process is inherited by init. This process should cleans up the zombie proces automatically.

This is how to kill it

$ ps axf | grep zombie | grep -v grep | awk '{print "kill -9 ",$1}' | sh

  2 Responses to “Zombie Process”

Comments (1)

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>