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,

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.

 

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

Output is,

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

Its output will be something like,

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.