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:
#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.
./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:
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:
21645
We finish off the parent:
With this, we will have gotten rid of the zombie process ;)