Posted by: kezhong | May 28, 2009

Zombie Process Lab

On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the process that started the (now zombie) process to read its exit status.

When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. However, the process’s entry in the process table remains. The parent can read the child’s exit status by executing the wait system call, at which stage the zombie is removed.

A zombie process is not the same as an orphan process. An orphan process is a process that is still executing, but whose parent has died. They do not become zombie processes; instead, they are adopted by init (process ID 1), which waits on its children.

To remove zombies from a system, the SIGCHLD signal can be sent to the parent manually, using the kill command. If the parent process still refuses to reap the zombie, the next step would be to remove the parent process. When a process loses its parent, init becomes its new parent. Init periodically executes the wait system call to reap any zombies with init as parent.

The following program can create five zombie processes.
#include <unistd.h>

int main(void)
{
int i;
for( i = 0; i < 5; i++){
if( fork() == 0){
_exit(0);
}
}
while(1);
}

Compile and run this program.
$ cc zom1.c -o zom1
$ ./zom1 &

Open another termial, and check using top command:
top – 11:21:14 up  2:57,  4 users,  load average: 0.89, 0.61, 0.55
Tasks: 178 total,   4 running, 169 sleeping,   0 stopped,   5 zombie
Cpu(s): 50.6%us,  0.2%sy,  0.0%ni, 49.2%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1034228k total,   823984k used,   210244k free,    74588k buffers
Swap:  2040244k total,        0k used,  2040244k free,   397956k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11342 kezhong   20   0  1668  268  216 R 99.9  0.0   1:46.18 zom1
11343 kezhong   20   0     0    0    0 Z  0.0  0.0   0:00.00 zom1 <defunct>
11344 kezhong   20   0     0    0    0 Z  0.0  0.0   0:00.00 zom1 <defunct>
11345 kezhong   20   0     0    0    0 Z  0.0  0.0   0:00.00 zom1 <defunct>
11346 kezhong   20   0     0    0    0 Z  0.0  0.0   0:00.00 zom1 <defunct>
11347 kezhong   20   0     0    0    0 Z  0.0  0.0   0:00.00 zom1 <defunct>

Remove zombie processes.
$ kill SIGCHLD 11342

Check again using top command, you will find the zombie processes have disappeared.

References

http://en.wikipedia.org/wiki/Zombie_process


Responses

  1. Thank you for post. It’s really good read.
    I love to read kezhong.wordpress.com.

    miami teeth whitening


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: