How to kill zombie processes & What is zombie process?

I would like to explore linux zombie process in this blog ..

Zombie processes are also called “defunct” processes in the system. Those are processes whose execution is completed, but the parent is not aware of it or parent didnt reap its child, so it holds entry in process table. It will not waste any other resources in the system. The only resource it wastes is this entry in the table. It becomes an issue only when we/system reaches the situation: “The total no of processes running in the system is equal to the max limit of number of processes”. Then we may worry about them. The main chance for zombie creation is when a parent didnt wait() for this child, in other words crappy application developers can generate a zombie process 🙂

How can we kill zombie processes?

It is said that we can send SIGKILL to kill zombie processes . This is NOT correct. How-ever you can try the methods mentioned below to get rid of this.

1) Reboot the system

2) Try to send “SIGCHLD” signal to the parent process. This may work , if parent honours this signal

3) Change the application/parent to include wait() family of system calls, so there wont be any more zombie process from that application as this will cause proper reaping of child process.

4) You can try to kill parent process.

5) As a last option you may play with debuggers to get rid of it.

Now, for fun I tried to create a zombie process in my system ..

[root@humbles-lap ~]# cat zombie.c

// include necessary headers..

int main ()
{
pid_t child_pid;

if ((child_pid=fork())==0)
{
printf (“\n I am child”);
sleep (10);
exit(0);
}
if (child_pid >0 )
{
printf (“\n I am parent & child pid is %d”, child_pid);

sleep (20);
}
printf ( “\n I am end of main”);
pause();

}

Lets compile and run it ..

[root@humbles-lap ~]# gcc -o zombie zombie.c
[root@humbles-lap ~]# ./zombie

I am child
I am parent and child pid is 20263

When above program is running.. In another terminal:

[root@humbles-lap ~]$ ps aux |grep zom
root 20262 0.0 0.0 4000 276 pts/0 S+ 21:03 0:00 ./zombie
root 20263 0.0 0.0 0 0 pts/0 Z+ 21:03 0:00 [zombie] =====> so zombie process generated. 🙂
root 20283 0.0 0.0 103376 824 pts/1 S+ 21:04 0:00 grep –color=auto zom

Rest of the stuffs are homework…