Unix / Linux: How to Create Zombie Process

Objective: Analyse how zombie processes or defunct processes are created and how to remove them from the process table.

A zombie process or defunct process is a process that has completed execution but still has an entry in the process table. This occurs for child processes that has been forked by a parent process using the fork() system call but not reaped by the parent process using the wait() system call.

The parent process has to make a wait() (or waitpid()) system call to get the exit status of the child process once the child process has terminated. If the wait() call is not performed by the parent process, the child process will become a zombie process.

A SIGCHLD signal is sent to the parent process whenever a child process terminates. The parent process needs to have a SIGCHLD signal handler function defined with a call to wait() – this will remove the child process from the process table.

To remove zombie processes 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 process, and if the parent process can be killed, the next step is to kill the parent process. When a child process loses its parent process, init (PID 1) becomes its new parent. init periodically executes the wait() system call to reap any zombie processes with init as parent.

I wrote a simple C program to create a zombie process.

Save the above code as zombie.c and compile it using gcc.

Next, run the program.

From another terminal, do a ps to display running processes. You will see a zombie process.

Once the parent process has terminated (after 30 seconds), the zombie process will be removed from the process table by the init process.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }