Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a zombie process necessary?

Wikipedia basically gives all the possible information about zombie processes that I NEED to know but just a simple line on how it might be useful..in that a conflict in PIDs will not exist in the event the parent process creates another child process.

How is this then actually "useful"? Wouldn't the PID be then available if the named zombie process were to be removed instead of being kept there?

Or are there any other reasons as to why the zombie process should exist?

like image 819
Jon Gan Avatar asked May 07 '13 10:05

Jon Gan


2 Answers

Zombie processes are actually really important and definitely need to exist. First it's important to understand how process creation works in Unix/Linux. The only way to create a new process is for an existing process to create a new child process via fork(). In this way, all of the processes on the system are arranged in a nice orderly tree heirarchy. Try running ps -Hu <your username> on a Linux system to see the heirarchy of processes that you own.

In many programs it is critically important for a parent process to be able to obtain basic information about its child processes that have exited. This basic information includes the exit status and resource usage of the child. When the parent is ready to get information about a dead child process it calls one of the wait() functions to wait for a child to exit and obtain exit status and resource usage info.

But what happens if a child process exits before the parent waits for it? This is where zombie processes become necessary. The operating system can't just discard the child process; the operation of the parent process may be dependent upon knowing the exit status or resource usage of the child. i.e. The parent process might need to know that the child exited abnormally, or it might be collecting CPU usage statistics for its children, etc. So, the only choice is to save off that information and make it available to the parent when it finally does call wait(). This information is what a zombie process is and it's a critical part of how process management works on Unix/Linux. Zombie processes allow the parent to be guaranteed to be able to retreive exit status, accounting information, and process id for child processes, regardless of whether the parent calls wait() before or after the child process exits.

This is why a zombie process is necessary.

Footnote: If the parent process never calls wait(), then the child process is reparented to the init process when the parent process dies, and init will wait() for the child.

like image 86
mshildt Avatar answered Nov 10 '22 22:11

mshildt


The answer is on Wikipedia as well, which is:

This entry is still needed to allow the parent process to read its child's exit status.

like image 21
Uko Avatar answered Nov 10 '22 22:11

Uko