Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating A Zombie Process Using the kill Function

I'm trying to create a zombie process with the kill function but it simply kills the child and returns 0.

int main ()
{
  pid_t child_pid;

  child_pid = fork ();

  if (child_pid > 0) {
    kill(getpid(),SIGKILL);
  }
  else {
    exit (0);
  }

  return 0;
}

When I check the status of the process there is no z in the status column.

like image 496
robo Avatar asked Oct 19 '22 10:10

robo


1 Answers

Here is a simple recipe which should create a zombie:

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

int main()
{
    int pid = fork();
    if(pid == 0) {
        /* child */
        while(1) pause();
    } else {
        /* parent */
        sleep(1);
        kill(pid, SIGKILL);
        printf("pid %d should be a zombie\n", pid);
        while(1) pause();
    }
}

The key is that the parent -- i.e. this program -- keeps running but does not do a wait() on the dying child.

Zombies are dead children that have not been waited for. If this program waited for its dead child, it would go away and not be a zombie. If this program exited, the zombie child would be inherited by somebody else (probably init), which would probably do the wait, and the child would go away and not be a zombie.

As far as I know, the whole reason for zombies is that the dead child exited with an exit status, which somebody might want. But where Unix stores the exit status is in the empty husk of the dead process, and how you fetch a dead child's exit status is by waiting for it. So Unix is keeping the zombie around just to keep its exit status around just in case the parent wants it but hasn't gotten around to calling wait yet.

So it's actually kind of poetic: Unix's philosophy here is basically that no child's death should go unnoticed.

like image 53
Steve Summit Avatar answered Oct 21 '22 04:10

Steve Summit