Fundamentals 7 min read

Why Do Zombie Processes Appear in Linux and How to Eliminate Them?

This article explains the Linux zombie process concept, shows how they are created when a parent neglects wait(), demonstrates the issue with sample C code and ps output, discusses the risks of PID exhaustion, and provides proper handling techniques using wait() and process reaping.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why Do Zombie Processes Appear in Linux and How to Eliminate Them?

What Is a Zombie Process?

In Linux, a process can query the kernel for its parent PID or the execution status of its child processes. When a child terminates, the kernel retains a small amount of information (PID, exit status, CPU time) until the parent calls wait() or waitpid(). Until then the terminated child remains in a zombie state.

How Zombie Processes Are Created

A zombie appears because the parent process does not invoke wait() after the child exits. The kernel cannot discard the child’s descriptor fields until the parent acknowledges the termination, which is why the zombie state exists.

Demo: Generating a Zombie

The following program forks a child that immediately exits while the parent sleeps without calling wait(). Compile and run it to observe the zombie.

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

int main(void)
{
    int i = 100;
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork failed.");
        exit(1);
    }
    if (pid > 0) {
        printf("This is the parent process. My PID is %d.
", getpid());
        for (; i > 0; i--) {
            sleep(1);
        }
    } else if (pid == 0) {
        printf("This is the child process. My PID is: %d. My PPID is: %d.
", getpid(), getppid());
    }
    return 0;
}

Compile and run:

$ gcc zomprocdemo.c -o zomprocdemo
$ ./zomprocdemo

While the parent sleeps, use ps -ef | grep Z (or similar) to see the child listed with a status Z , indicating it is a zombie.

Risks of Zombie Processes

Although the kernel releases most resources of a terminated process, it keeps the PID and exit information until the parent reaps it. If many zombies accumulate, they consume PID slots, and because the system has a finite number of PIDs, new processes may fail to start.

Proper Cleanup of Child Processes

To avoid zombies, the parent should always call wait() (or waitpid()) after forking. If a zombie does appear, you cannot kill it directly; instead, terminate its parent so the zombie becomes an orphan and is adopted by the init system (e.g., systemd), which will reap it.

Demo: Using wait() Correctly

The following example shows the parent waiting for the child, retrieving the child’s exit code with WEXITSTATUS, and thus preventing a zombie.

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

int main(void)
{
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork failed");
        exit(1);
    }
    if (pid == 0) {
        for (int i = 3; i > 0; i--) {
            printf("This is the child
");
            sleep(1);
        }
        exit(3); // exit with code 3 for test
    } else {
        int stat_val;
        wait(&stat_val);
        if (WIFEXITED(stat_val)) {
            printf("Child exited with code %d
", WEXITSTATUS(stat_val));
        }
    }
    return 0;
}

Running this program shows the parent prints the child’s exit code and no zombie remains.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxforkwaitzombie process
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.