What is a Zombie Process ?

In this article we will discuss what is a Zombie Process.

Zombie Process

In Linux, when a process exits then its not directly removed from the process table. OS expects the parent of this terminated process to wait for it and read its exit status. If parent process don’t wait for this terminated process then Process will get stuck in Terminated state and called Zombie Process.

Parent process can wait for a child process by calling wait() or waitpid() function. We will discuss these functions in separate article.

Although a Zombie Process will not use much resource but there will be an entry in process table for it and also OS can not use its process ID for any other process.

Let’s see how a child process turns into a Zombie Process by this example,

#include <iostream>
#include <unistd.h>

int main()
{
	std::cout<<"Current process Id : "<<getpid()<<std::endl;

	pid_t childProcessId = fork();

	// If fork call Code after
	if(childProcessId < 0)
	{
		std::cout<<"Failed to Create a new Process"<<std::endl;
	}
	else if (childProcessId == 0)
	{
		// This code will be executed in Child Process Only
		std::cout<<"Child Process Id : "<<getpid()<< " Its parent ID : "<<getppid()<<std::endl;

		std::cout<<"Child Process exits"<<std::endl;
	}
	else if (childProcessId > 0)
	{
		// This code will be executed in Parent Process Only
		std::cout<<"Parent Process Id : "<<getpid()<< " Its Child Process ID : "<<childProcessId<<std::endl;
		sleep(30);
		std::cout<<"Parent Process exits"<<std::endl;
	}

}

In the above example, we created a child process that will exit after executing 2 lines but Parent Process will exit after 60 seconds.
As, parent has not waited for this child process, therefore this child process will become Zombie.


Frequently Asked:


[showads ad=inside_post]

 

Let’s test it, Run the above as background process after compiling i.e.

g++ zombie.cpp -o zombie
./zombie &

Output is,

Current process Id : 4116
Parent Process Id : 4116 Its Child Process ID : 4117
Child Process Id : 4117 Its parent ID : 4116
Child Process exits

Now Lets check if child proces became zombie, by running ps command i.e. run following command,

ps -aux | grep zombie

Its output will be something like,

varun 4116 0.0 0.0 12604 1408 pts/1 S 22:47 0:00 ./zombie
varun 4117 0.0 0.0 0 0 pts/1 Z 22:47 0:00 [zombie] <defunct>

Now you can see the child process “Zombie” with ID “4117” has a “Z” in State, this denotes a Zombie Process.

What if a Parent Process dies before waiting for child process ?

If a parent process dies before waiting for Child Process then this child process is inherited by process “init“. init process periodically waits for all its exited children processes. So, eventually that process will be finally removed from system.

For example, in above example parent process will die after 60 seconds, hence That child process will be inherited by init. Also, as init periodically waits for all exited children.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top