Create dummy zombie process in Ubuntu

As software testers, we sometimes need to know how a zombie process looks like on our system. We can run various tests on it if we know specifically which program and process ID is associated with that zombie process.

What is a Zombie Process?

A zombie or a defunct process in Linux is a process that has been completed, but its entry still remains in the process table due to lack of correspondence between the parent and child processes. Usually, a parent process keeps a check on the status of its child processes through the wait() function. When the child process has finished, the wait function signals the parent to completely exit the process from the memory. However, if the parent fails to call the wait function for any of its children, the child process remains alive in the system as a dead or zombie process. These zombie processes might accumulate, in large numbers, on your system and affect its performance.

When you run the top command and a zombie is running, it will be indicated by the second line of the output as follows:

Zombie process shown in top command

However, if there is no zombie process running on your system, you can create a dummy for testing purposes as described in this article. We are running this process on Ubuntu 18.04.

Creating a Zombie-Process

In this section, we will write a c program to run a zombie process on our system.

Open a text editor and enter the following code:

Code

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

Save this file as zombie.c

The zombie process created through this code will run for 60 seconds. You can increase the time duration by specifying a time(in seconds) in the sleep() function.

Open the Terminal and run the following command to compile this program:

$ cc zombie.c -o zombie

Now an executable file by the name of zombie will be created.

Please note that you will need to install the GNU C compiler if it is already not installed on your system through the following command:

$ sudo apt-get install build-essential

Now run the zombie program through the following command:

./zombie

You will see the following output:

Output of zombie file

You can use the parent process ID (PPID) and child process ID (PID) during testing; for example by killing this zombie process through the kill command.

While this process is running, you can view the system performance in another Terminal window through the top command. You will be able to see 1 zombie process through the second line of the output.

The ps command will now also show this defunct process and the zombie program responsible for it:

$ ps axo stat,ppid,pid,comm | grep -w defunct

Result of ps command

This command will give you the state, parentID, the process ID, the program that is running the zombie process (the zombie program that we ran in this tutorial).

You have now learned how to create a simple dummy zombie process. This will give you an idea of how it looks on your system and how you can use it for testing purposes.

How to Create a Dummy Zombie Process in Ubuntu 18.04 LTS