![]() |
VOOZH | about |
A zombie process is a process that has completed its execution but still remains in the process table because its parent process has not yet read its exit status. It is called a "zombie" because it is no longer active or running, but it still exists as a placeholder in the system. Zombie processes do not consume system resources like CPU or memory, but they can clutter the process table if not handled properly, especially if many zombie processes accumulate.
wait() to read its status.let's see an example of Zombie Process:
Output :
Now check the process table using the following command in the terminal
$ ps -eaf
Here the entry [a.out] defunct shows the zombie process.
Why do we need to prevent the creation of the Zombie process?
There is one process table per system. The size of the process table is finite. If too many zombie processes are generated, then the process table will be full. That is, the system will not be able to generate any new process, then the system will come to a standstill. Hence, we need to prevent the creation of zombie processes.
Zombie processes can be prevented by ensuring proper cleanup of child processes after they terminate. Preventing Zombie Process helps maintain a clean process table and prevents system issues caused by lingering zombie processes.
Different techniques to prevent zombie process are:
When the parent process calls wait(), after the creation of a child, it indicates that, it will wait for the child to complete and it will reap the exit status of the child. The parent process is suspended(waits in a waiting queue) until the child is terminated. It must be understood that during this period, the parent process does nothing just wait.
(Point 2. is wrong and doesn't prevent zombie creation, its the job of parent process to remove the child process from process list and is not done by the kernel, infact SIG_IGN is the default handler for SIGCHLD signal to the parent process).
When a child is terminated, a corresponding SIGCHLD signal is delivered to the parent, if we call the 'signal(SIGCHLD,SIG_IGN)', then the SIGCHLD signal is ignored by the system, and the child process entry is deleted from the process table Thus, no zombie is created. However, in this case, the parent cannot know about the exit status of the child.
The parent process installs a signal handler for the SIGCHLD signal. The signal handler calls wait() system call within it. In this scenario, when the child terminated, the SIGCHLD is delivered to the parent. On receipt of SIGCHLD, the corresponding handler is activated, which in turn calls the wait() system call. Hence, the parent collects the exit status almost immediately and the child entry in the process table is cleared. Thus no zombie is created.
Output:
Here no any [a.out] defunct i.e. no Zombie process is created.
This involves creating a grandchild process which is then orphaned by the parent process. This ensures that the grandchild process is inherited by the init process, which reaps it automatically. This prevents the creation of a zombie process.
This system call is similar to the wait() system call, but it allows the parent process to wait for a specific child process to terminate. This way, the parent can collect the exit status of the child and prevent the creation of a zombie process.
A zombie process is a process that has completed its execution but still exists in the process table because its parent process hasnโt read its exit status. Itโs not active or using resources, but it remains as an entry in the system. On the other hand, an orphan process is a process whose parent has terminated while the child is still running. Orphan processes are adopted by the init process (or its equivalent), which takes responsibility for them to ensure they continue running smoothly. The key difference is that zombies are finished processes waiting to be cleaned up, while orphans are still active and working without their original parent.
read more about - Zombie and Orphan Processes
A zombie process is when a child process terminates but remains in the process table because its parent has not picked up its exit status. It doesnโt consume CPU or memory but can fill up the process table and cause issues if many zombie processes are present. Understanding zombie processes through code examples helps you to recognize them and fix them. To prevent zombie processes the parent process should use functions like wait() or waitpid() to clean up child processes or use signal handlers like SIGCHLD to automatically reap terminated child processes.