A zombie has finished running, but its parent has not read its exit status yet. An orphan is still running, but its parent has died. Orphans are harmless. A few zombies are harmless, but many zombies are a bug.
Zombie. The parent reads a child's exit status by calling wait() or waitpid(). Until it does, the child stays a zombie. The kernel has already freed the zombie's memory. It only keeps a small entry in the process table with the PID and the exit status. ps shows a zombie in state Z, marked <defunct>. You can't kill a zombie, because it is already dead. To remove it, you have two options:
- Make the parent reap its children, which means calling
wait()to collect their exit status. - End the parent. Then init (PID 1) adopts the zombie and reaps it.
Orphan. When a parent dies, the kernel gives its running children a new parent. This is called re-parenting. The new parent is PID 1, or the nearest ancestor marked as a child subreaper (a process that asked to adopt orphans in its subtree). The new parent reaps the orphan when it exits. Orphans are normal and harmless.
Why are many zombies a problem? A parent that never reaps its children leaks zombies. Each zombie holds a PID, and the number of PIDs is limited.
- A zombie has exited, but its parent hasn't called wait() to read its exit status.
- Zombies show as Z or <defunct> and can't be killed, so reap them or end the parent.
- An orphan is still running after its parent died, and it is re-parented to init or a subreaper.
- Many zombies are a bug, because each one holds a PID.