Skip to content
systemcoding.dev
Systems Programming

Zombie and Orphan Processes Explained with Examples

See what zombie and orphan processes are, how each one appears in ps on Linux, why a zombie cannot be killed, and how to fix the parent that causes it.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, Linux and GNU logos.

A zombie is a process that has already ended but still has a row in the process table, and an orphan is a process that is still running after its parent ended. They sound alike and have opposite causes: a zombie is a parent that is too slow to collect a result, and an orphan is a parent that left too early. Only one of them is a bug you have to fix. This post builds both in C on Linux 6.12 with glibc 2.40, shows them in ps, and explains what to do about each. The complete guide to Unix processes covers the surrounding calls.

The behavior is described in wait(2) and credentials(7) in the Linux man-pages.

What a zombie is

When a process ends, the kernel frees its memory, closes its files and removes it from the scheduler. It keeps one small record, containing the exit status and the resource usage, so the parent can read it later with wait or waitpid. Between the end of the child and the parent's call, the child is in state Z, zombie, also shown as <defunct>.

A zombie uses no CPU and no memory beyond that record. What it holds is a process ID and a slot in the process table. One zombie is harmless. A parent that creates thousands without ever waiting will eventually reach the limit and see fork fail with EAGAIN.

Make a zombie on purpose

This parent creates a child that exits at once and then sleeps without waiting:

zombie.cc
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return EXIT_FAILURE;
    }
    if (pid == 0)
        _exit(0);

    printf("parent %d, child %d\n", (int)getpid(), (int)pid);
    fflush(stdout);
    sleep(60);
    return EXIT_SUCCESS;
}

Run it, and in a second terminal ask ps for the children of the parent:

Shell
ps -o pid,ppid,stat,comm --ppid 8800

The program printed parent 8800, child 8801, so the child is the second line of the ps output. The state column shows Z and the name carries <defunct>:

Text
parent 8800, child 8801
Text
    PID    PPID STAT COMMAND
   8801    8800 Z+   zombie <defunct>

The process IDs will differ on your machine. After 60 seconds the parent ends, the zombie is reparented to init and removed, which is the second way a zombie disappears.

Why kill does not remove a zombie

People try kill -9 on a zombie and find it still there. The signal has nothing to act on. The process has already ended, so no code of the process can receive it. What remains is the kernel's record, and only two events remove it:

  1. the parent calls wait or waitpid, which collects the status;

  2. the parent ends, and the kernel gives the zombie to a new parent, usually process 1, which collects it at once.

So the fix for a zombie is never in the zombie. Look at PPID in the ps output and fix or restart that process. Killing the parent clears all its zombies because init adopts and reaps them.

Collect the status to prevent it

Calling waitpid after the child ends removes the record. This version of the parent waits and prints what it learned:

reaped.cc
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return EXIT_FAILURE;
    }
    if (pid == 0)
        _exit(5);

    sleep(2);
    system("ps -o pid,stat,comm --ppid $PPID");

    int status;
    waitpid(pid, &status, 0);
    printf("collected exit code %d\n", WEXITSTATUS(status));
    return EXIT_SUCCESS;
}

The system call runs through a shell, and in that shell $PPID is the program's process ID. During the two-second sleep the child is a zombie, so ps lists it. After waitpid the record is gone:

Text
    PID STAT COMMAND
   9102 Z    reaped <defunct>
collected exit code 5

The exact process ID varies. A parent that cannot block in waitpid has two other choices: a SIGCHLD handler that reaps in a loop, or setting the disposition of SIGCHLD to SIG_IGN, which makes the kernel discard the records itself. Both appear in the next posts of this series.

What an orphan is

An orphan is a running process whose parent has ended. The kernel does not kill it. It reparents the orphan to process 1, or to the nearest ancestor that has declared itself a subreaper with prctl(PR_SET_CHILD_SUBREAPER). Orphans are normal: every daemon is one by design, because starting a background service means arranging for the parent to leave.

This program makes an orphan. The parent exits first, and the child reports its parent twice:

orphan.cc
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return EXIT_FAILURE;
    }
    if (pid == 0) {
        printf("child: parent is %d\n", (int)getppid());
        sleep(2);
        printf("child: parent is now %d\n", (int)getppid());
        return EXIT_SUCCESS;
    }

    return EXIT_SUCCESS;
}

The shell prompt returns as soon as the parent exits, and the child prints its second line later:

Text
child: parent is 9400
child: parent is now 1

On a desktop Linux session the second number is often not 1. A user-level service manager, such as systemd --user, can mark itself as a subreaper, and then orphans in that session are adopted by it, so you may see a larger number. In a container the first process of the container plays the part of init, and if that process does not reap children, zombies accumulate inside the container.

The container trap

The container case deserves a warning, because it is where most real zombie bugs appear today. If a container's first process is your application and your application never calls wait for adopted orphans, every orphan it inherits that later ends stays a zombie, and nothing outside can clear them. Container runtimes offer an init flag that runs a tiny reaping process as PID 1, and some images ship one. If you write a program that is meant to run as PID 1, it must reap: loop on waitpid(-1, ..., WNOHANG) from a SIGCHLD handler.

Zombie or orphan, which one is the bug

Use the symptom to decide:

SymptomWhat it isIs it a bug
ps shows Z or <defunct> under a long-lived parentzombieyes, in the parent: it never waits
PPID of a process is 1 although you started it from a shellorphanno, unless you expected to control it
Number of zombies grows with every requestzombie leakyes: reap in a loop or ignore SIGCHLD
A background job survives its terminalorphan, maybe also a different sessionusually intended

To find zombies on a machine, list processes whose state starts with Z and group them by parent:

Shell
ps -eo stat,ppid,pid,comm | awk '$1 ~ /^Z/ { print $2 }' | sort | uniq -c
Text
     37 4410

The output means 37 zombies belong to the parent with process ID 4410. Check what that parent is with ps -p 4410, then fix it or restart it.

Where the zombie rules differ

Threads change who must wait. In Linux any thread of the parent may call wait for the children of the whole process, so one thread can reap what another created. A zombie with a multithreaded parent is still the process's fault, not a single thread's. The rules also differ for a child whose parent has set SA_NOCLDWAIT or ignored SIGCHLD: there are no zombies at all, and waitpid fails with ECHILD, which breaks programs that expect to read an exit status. On systems other than Linux the details of reparenting vary, but the zombie state exists on every Unix.

Share

Keep reading

Flat illustration of two options weighed side by side, with the macOS, Linux and Apple logos.
Kernels

Mach Traps vs BSD System Calls on macOS

Learn how macOS routes one trap instruction to either a Mach trap or a BSD system call, how the number sign and class decide, and why you should call libSystem.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.