We are going to explain what a zombie process is, how it is created, and how it is removed.
Steps:
Definition: A zombie process is a process that has completed execution but still has an entry in the process table because its exit status has not been read by its parent process.
Creation:
When a child process terminates, it sends an exit status to the parent.
The child process remains in the process table as a zombie until the parent reads its exit status (via wait or waitpid system call).
If the parent does not wait for the child, the child remains as a zombie.
Removal:
The zombie is removed when the parent process reads its exit status (by calling wait or waitpid).
If the parent process terminates without waiting for the child, the child process becomes an orphan and is adopted by the init process (PID 1). The init process periodically calls wait on its children, so the zombie is cleaned up.
Additional note: Zombie processes do not consume system resources (like memory) except for the process table entry. However, if there are too many zombies, the process table can fill up, preventing new processes from being created.
How to handle: It's the responsibility of the parent to wait for its children. If the parent is not designed to wait, it should ignore the SIGCHLD signal or set the child to be automatically reaped (using SA_NOCLDWAIT or by setting the signal handler for SIGCHLD to SIG_IGN).
In Unix-like systems, the zombie state is represented by 'Z' in the output of ps.
Let's structure the answer accordingly.
Zombie Process
A zombie process (sometimes called a defunct process) is a finished child process that still has an entry in the system's process table because its exit status hasn't been collected by its parent process.
Key Characteristics:
Does not run or consume CPU/memory (only occupies a small entry in the process table).
State shown as Z or <defunct> in tools like ps.
Cannot be killed (since it's already dead).
How a Zombie is Created
Child process terminates
– A child process calls exit() or returns from main().
Kernel retains process information
– The child’s exit status, resource usage, and PID are preserved in the process table.
Parent is notified
– The kernel sends SIGCHLD to the parent, signaling a child’s death.
Parent must wait()
– The parent must call wait() or waitpid() to read the child’s exit status.
– Until this happens, the child remains a zombie.
How a Zombie is Removed
Scenario
Mechanism
Parent calls wait()
Parent explicitly collects exit status → zombie entry cleared.
Parent ignores SIGCHLD
If the parent sets SIGCHLD to SIG_IGN (or uses SA_NOCLDWAIT), zombies are auto-reaped.
Parent dies before child
The orphaned zombie becomes a child of init (PID 1). init periodically calls wait() on all children → zombie removed.
Why Zombies Persist
Buggy parent that never calls wait().
Parent intentionally leaves zombies (rare, e.g., to avoid race conditions).
Parent is hung/stuck and cannot reap children.
Example Scenario
c
// Parent code snippet
pid_t pid = fork();
if (pid == 0) {
// Child
exit(0);
} else {
// Parent: forgets to call wait()
sleep(100); // Zombie lives until parent exits or calls wait()
}
Dangers of Zombies
Process table exhaustion: Each zombie consumes one PID slot. If too many accumulate, the system may refuse to create new processes.
Indicates poor programming: A well-designed parent should reap children promptly.
How to Detect & Handle
List zombies:
bash
ps aux | grepdefunct
# or
top -b -n 1 | grep Z
Fix at code level:
Ensure parents call wait()/waitpid().
Handle SIGCHLD properly (e.g., with a signal handler that calls waitpid(-1, ..., WNOHANG)).
If parent is buggy/unfixable:
Kill the parent process (not the zombie). Once the parent dies, init adopts and reaps the zombie.
Summary Table
Aspect
Detail
State
Dead, but entry remains in process table
Created when
Child terminates, parent hasn't called wait()
Removed when
Parent calls wait()or parent dies (adopted by init)
Does not
Consume CPU/RAM (only PID slot)
Cannot be
Killed directly (only parent or init can clear it)
Zombies are normal in short-lived fork-exec patterns but should be automatically reaped in robust systems to avoid resource leaks.