What are zombie and orphan processes?
MediumConceptProcesses & Threads
Answer
A zombie process is a child that has finished (called exit()) but whose parent has not yet read its exit status with wait(). It no longer runs or holds memory, but its entry stays in the process table. An orphan is a child whose parent ended first; on Linux it is adopted by init/systemd (PID 1) or by a designated "subreaper" process, which collects its status when it exits.
- Zombies exist because the kernel must keep the exit status until the parent asks for it.
- One zombie is harmless, but a parent that never calls
wait()can pile up thousands of them and exhaust the available process IDs. - Fixes: call
wait()orwaitpid(), handle theSIGCHLDsignal, or end the parent so that its zombies are adopted and cleaned up. - An orphan is not a problem in itself; it keeps running normally.
Example:
if (fork() == 0) exit(0); // child ends at once
sleep(60); // parent never calls wait()
For 60 seconds, ps shows the child in state Z, marked <defunct>. If instead the parent exited while the child was still running, the child would become an orphan and its parent PID would change to that of the adopting process.
More in Processes & Threads
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is the difference between a process and a thread? | Concept | Easy | Read the answer to What is the difference between a process and a thread? | |||
| 2 | What are the states of a process, and what is a PCB? | Concept | Easy | Read the answer to What are the states of a process, and what is a PCB? | |||
| 3 | What is a context switch, and why is it considered overhead? | Concept | Medium | Read the answer to What is a context switch, and why is it considered overhead? | |||
| 4 | What does fork() do, and what does it return? | Concept | Medium | Read the answer to What does fork() do, and what does it return? | |||
| 5 | What are zombie and orphan processes? | Concept | Medium | Read the answer to What are zombie and orphan processes? |