Pete's Linux Advent Calendar 2007

The 3rd day

What is a Zombie?

A Zombie is a terminated child process that no parent has waited for. Here is how to produce a zombie process:
/*  zombie: creates a zombie
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv) {
  int pid;
  char cmd[255];
  if((pid=fork()) == 0) {
    /* child */
    printf("child: my pid: %d, my parent: %d\n",getpid(),getppid());
    printf("child: exiting\n");
  } else {
    /* parent */
    printf("parent: my child: %d, my pid: %d, my parent: %d\n",pid,getpid(),getppid());
    printf("parent: sleeping for a second ...\n");
    sleep(1);
    sprintf(cmd,"ps -fp %d",pid);
    system(cmd);
    printf("parent: exiting\n");
  }
  return 0;
}
If you save this file as zombie.c and then run make zombie you can run the program. The output will look similair to this:
child: my pid: 16109, my parent: 16108
child: exiting
parent: my child: 16109, my pid: 16108, my parent: 10343
parent: sleeping for a second ...
UID        PID  PPID  C STIME TTY          TIME CMD
pkruse   16109 16108  0 09:23 pts/65   00:00:00 [zombie] 
parent: exiting
Notice that the ps command indicates the state of the child process as a zombie.
Pete's Linux Advent Calendar 2007

Valid HTML 4.01!

Comments? send mail to pkruse@arcor.de my monogram