Exit and reap
A process ends with the _exit system call, which releases its memory and file descriptors. The kernel keeps only one thing: the exit status, and a few accounting numbers. The process is now a zombie, which is the state Z in ps. The zombie holds a PID, so that the parent can find out how the child ended. The parent collects the status with wait, and the kernel then removes the entry. This removal is called reaping.
A zombie uses almost no memory and no CPU. It is an entry in the process table, and the process table has a fixed size, so a parent that never reaps its children can eventually block new processes from starting. That is the real reason zombies matter. A few are harmless, but thousands from one buggy program are a problem.
Seeing a zombie
A zombie shows up as a process with the state Z and a command name in brackets in some tools. The ps output below lists the states of all processes, so you can search for any process in state Z.
ps -eo pid,ppid,stat,comm | grep -w ZOn a healthy system this command often prints nothing, because shells and init systems reap their children promptly. The command is useful when a machine feels full of stale processes. The PPID column tells you which parent is failing to reap, and that parent is the program to fix or restart.
You cannot kill a zombie with a signal, because it is already dead. Sending SIGKILL does nothing useful. The only ways to clear a zombie are for the parent to call wait, or for the parent itself to exit so that the zombie is passed to a new parent that reaps it.
Orphans and reparenting
An orphan is a process that is still running after its parent has exited. The kernel reparents it to a process that acts as a reaper, usually PID 1 or a process that has marked itself as a child subreaper. The new parent calls wait when the orphan finishes, so the orphan does not become a zombie for long. The change of PPID is visible in ps, so you can observe it directly.
sh -c 'sleep 300 & echo $!' > orphan.txt
cat orphan.txtThe outer shell starts a background sleep and exits right away, so the sleep becomes an orphan. The command writes the PID of the sleep to orphan.txt. Read that PID and look up the sleep with ps -o pid,ppid,args -p. Its PPID is no longer the shell that started it, but a process that the kernel chose to adopt it.
Shells reap their children
An interactive shell reaps background jobs when they finish, and it prints a notice such as Done at the next prompt. A script that starts background jobs also reaps them, but only through wait, or through the normal reaping that the shell performs on SIGCHLD. Bash handles SIGCHLD by reaping every child that has exited, so a zombie from a bash job is rare.
The situation that produces zombies is a program written in another language that forks children and never calls wait for them. Each child finishes, stays a zombie, and holds its PID until the parent dies. A process supervisor, which you will build later in this module, must reap the processes it starts, or it becomes a source of zombies itself.
init and subreapers
PID 1 has a special role. It is usually the init system, and it reaps all orphans that reach it. A container or a minimal system may use a different process as PID 1, and that process must reap orphans too, or zombies accumulate. Linux also lets a process mark itself as a child subreaper with the prctl call, so that orphans from its descendants are adopted by it, not by PID 1. Service managers use this to supervise trees of daemons.
If a process shows Z in ps and its parent is the shell you are working in, the cause is usually a missing wait in a script you wrote. Check the parent before you restart anything.
A useful habit is to check for zombies whenever a machine behaves strangely. A zombie is not a resource problem by itself, so an unexplained count of zombies points to a bug in the parent. The fix is almost always in the parent program: it needs to call wait for each child, or to install a handler for SIGCHLD that reaps them. Restarting the parent also works, because the kernel then reparents the zombies to PID 1, which reaps them. Restarting is a workaround, not a cure, so find the parent that is failing to reap.
Hands-on: record the states
In the lab, record the state of your own shell and then record a list of every process with its state. The first file shows how a live shell looks. The second file is a snapshot of the whole table, which you can compare later.
grep State /proc/$$/status > state.txt
ps -eo pid,stat,comm > procs.txt
head -n 2 procs.txtThe first line of procs.txt is the header with the column names, followed by one line per process. The state column is the same one you saw in the first unit. Zombies would appear with a Z in that column, and in a healthy lab you should see none.