Process management is a fantastic feature for any Linux user. More times than not, you will find yourself diving into the Linux process manager hunting for errors and ways to optimize your system.

One of the features that you will come across in Linux and Unix-based system processes is zombie processes.

With your coffee ready, a terminal session and a curious mind, let's dive in.

What is a Zombie Process?

A zombie process refers to a process that has completed execution using the exit system call but still reserves an entry in the process table.

In Linux and Unix systems, when a process dies, it is not removed from the memory immediately. It remains there as a zombie process. The parent of that process is then notified that the child process has completed execution. Using the wait sys call, the parent process will read the information of the zombie process and othere useful information.

The parent will then ue this information to clean up the child process and release the memory and process descriptor.

Therefore, zombie processes are not generally a bad thing. They are actually a feature that allows processes to clean up after themselves.

Linux Show Zombie Processes

Before running the commands to view zombine process, keep in mind that unless the parent process has a bug that prevents it from cleaning up completed child processes, you will hardly see zombine processes running for long.

In Linux, we can view zombine processes by running the top command as shown:

top

The command will launch the top utility allowing you to view all the running processes including any available zombie processes.

However, it can be difficult to find any zombine process especially since most programs will clean them ip. To best illustrate zombie process, let us create one.

Creating a Zombie Process

We can create a simple program that illustrates how a zombie process works. For example, we can have a prent process that creates a child process. The parent will then sleep for a few minutes while the child process performs its desired tasks and exits. This will force the child process to complete and become a zombie. But since the parent is sleeping, it will not be in a position to clean the zombine process.

Start by creating a simple c program:

touch geekbits_zombie.c

Edit the file with your favorite text editor:

nano geekbits_zombie.c

Finally, add the code as shown below:

#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int main() {
    pid_t pid;
    // create child process identical to parent
    pid = fork();
    // if pid is greater than 0 = parent
    // if pid is 0 = child
    // if child is -ve = fork() failed

    // parent
    if (pid > 0) {
        sleep(300);
    } else {
        exit(0);
    }
    return 0;
}

The code above is relatively simple and descriptive. But for better understanding, let us break it down into sections.

As usual, we start by importing the libraries we require. For this program, we need the unistd.h header file which gives us access to the POSIX API.

The sys/types.h file gives us access to the definition of various types including the pid_t which is useful for process IDs.

Finally, standard library because, well, we need it.

In the main() function, we start by creating a child process which is identical to the parent process using the fork() syst call.

We then check if the PID value. If its greater than 0, then that's the parent which means we want that process to sleep for 5 minutes. This will force the process to be unable to clean up the child process.

If the PID is not equal 0, then that's the child process which should exit immediately.

Once we have the program ready, save the file and compile it by running the command:

gcc -o geekbits_zombie geekbits_zombine.c

The command will compile the C program and save it into geekbits_zombie binary.

To test the zombie process, we can run it in the background as:

./geekbits_zombie &

Once started in the background, we can check for any zombie processes with top:

top

You can run the zombie process multiple times to increase the zombie processes.

To view the details of the zombie process, we can run the command:

top -b -n1 -u $USER

The command will show the details of the zombie processes:

Once the parent wakes up from the sleep of 5 minutes, it will start to cleanup all the completed (zombie) processes.

Manually Terminate Zombie Process

Although its the work of the parent to clean up zombie processes, if the parent has a bug preventing it to do cleanup in an efficient way, you can manually tell the parent to kill the zombie process.

Run the command:

sudo kill -s SIGCHLD [parent_pid]

Replace the parent_pid with the Process ID of the parent whose zombie process you wish to kill.

This will force the parent to check for any zombie processes and terminate them. If the parent is unable to comply, there is nothing else you can do unless you terminate the parent process itself.

Keep in mind that zombie processes are a normal feature implemented by many process. Hence, unless you see a large amount of zombie processes on your system, they are harmless and perfectly okay to ignore.

Conclusion

In this article, we covered the concept of zombie processes in Linux and Unix-based systems. We also illustrated how we can manually create zombie processes in C.

Thanks for reading & Happy coding!!!

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.