This page looks best with JavaScript enabled

The rise of the zombies has arrived

 ·  🎃 kr0m

In Linux, there are times when a process ends up in a zombie state. This is because the child process has finished its execution, but the parent process responsible for monitoring the state of its child is not doing so. In this article, we will learn how to locate the parent process in order to get rid of the child, all explained through a simple program written in C.

We can compile a simple program that does just that:

vi zombie.c

#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;
}

This program creates a child process using fork, waits for 60 seconds, during which time the parent is in sleep and knows nothing about its children, and the fork process is in a zombie state.

cc zombie.c -o zombie
./zombie
ps aux|grep zombie

XX     21646  0.0  0.0      0     0 pts/0    Z+   11:36   0:00 [zombie] <defunct>

We can verify that it is not possible to kill it:

kill -9 21646
ps aux|grep zombie

XX     21646  0.0  0.0      0     0 pts/0    Z+   11:36   0:00 [zombie] <defunct>

First, we need to locate its parent:

cat /proc/21646/stat|awk -F " " ‘{print$4}’

21645

We finish off the parent:

kill -9 21645

With this, we will have gotten rid of the zombie process ;)

If you liked the article, you can treat me to a RedBull here