Zombie processes are created in Unix/Linux systems.
We can remove them via the
But is there any in-built clean-up mechanism in Linux to handle zombie processes?
Zombie processes are already dead. You cannot kill them. The
kill command or system call has no effect on a zombie process. (You can make a zombie go away with
kill, but you have to shoot the parent, not the zombie, as we'll see in a minute.)
A zombie process is not really a process, it's only an entry in the process table. There are no other resources associated with the zombie process: it doesn't have any memory or any running code, it doesn't hold any files open, etc.
When a process dies, the last thing to go, after all other resources are cleaned up, is the entry in the process table. This entry is kept around, forming a zombie, to allow the parent process to track the exit status of the child. The parent reads the exit status by calling one of the
wait family of syscalls; at this point, the zombie disappears. Calling
wait is said to reap the child, extending the metaphor of a zombie being dead but in some way still not fully processed into the afterlife. The parent can also indicate that it doesn't care (by ignoring the SIGCHLD signal, or by calling
sigaction with the
SA_NOCLDWAIT flag), in which case the entry in the process table is deleted immediately when the child dies.
Thus a zombie only exists when a process has died and its parent hasn't called
wait yet. This state can only last as long as the parent is still running. If the parent dies before the child or dies without reading the child's status, the zombie's parent process is set to the process with PID 1, which is
init. One of the jobs of
init is to call
wait in a loop and thus reap any zombie process left behind by its parent.
kill a zombie process, as it's already dead.
Zombie processes have to be
waited by their parents, so that their exit status to be collected.
The only "built-in clean-up mechanism" there is in Linux, works for the case that any parent-process dies before it collects its children's exit status. In this case, each child is inherited by the
init process, which will
wait on the child, collect its exit status and remove its entry in the process table.
As soon as the parent of a process dies, the process becomes an orphan - disregarding if it's a zombie or not. What happens to orphans? They get a new parent, called
wait on each orphan that it collected, thus reaping the orphan (that may also have been a zombie as well).
No, there is no in-built cleaning mechanism for zombies. Zombies go to zombie heaven once their parent is killed. Until then, the OS keeps them to return the exit status to parent.