Understanding Parent‑Child Processes: Fork, Orphan and Zombie Explained
This article explains the concepts of processes, their identifiers, the fork system call, and the differences between orphan and zombie processes, providing code examples, output analysis, and practical ways to handle each situation on Unix‑like systems.
What Is a Process?
A process is an executing instance of a program, identified by a unique PID (process ID). The article shows how different operating systems display processes (Windows, macOS, Linux) and lists common fields from the ps -ef output, such as UID, PID, PPID, C, STIME, TTY, TIME, and CMD.
PID Characteristics
PID 0 is reserved for the kernel and does not correspond to a user‑space program. PID 1 is the init (or systemd) process, which adopts orphaned processes.
fork(): Creating a Child Process
The fork() system call duplicates the calling process. In the child, fork() returns 0; in the parent, it returns the child's PID. The article provides a C example that prints the return value and demonstrates the distinction.
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
pid_t p1 = fork();
printf("%d
", p1);
if(p1 > 0){
printf("Parent pid = %d, p1 = %d
", getpid(), p1);
} else {
printf("Child pid = %d, ppid = %d, p1 = %d
", getpid(), getppid(), p1);
}
return 0;
}Running this program shows the parent receiving the child's PID and the child receiving 0.
Orphan Processes
An orphan occurs when a parent exits before its child. The child is then adopted by PID 1 ( init). The article includes a C program that forces the parent to exit early, demonstrating that the child becomes an orphan and is re‑parented to init.
Zombie Processes
A zombie appears when a child terminates but the parent does not call wait() or waitpid() to reap the exit status. The child remains in the process table as defunct. The article provides a program that creates a zombie by having the child exit while the parent sleeps in an infinite loop, and shows the resulting process list.
Handling Orphans and Zombies
Terminate the parent : Let the child become an orphan, which init will clean up.
Parent calls wait() or waitpid() : Reaps the child immediately, preventing zombies.
Double fork : The first fork creates a child; the child forks again and exits, leaving the grandchild adopted by init.
Signal handling : Install a handler for SIGCHLD to call wait() when a child exits.
Ignore the signal : Use signal(SIGCHLD, SIG_IGN) or signal(SIGCLD, SIG_IGN) so the kernel automatically reaps the child.
Summary
The article demonstrates that process management on Unix‑like systems involves understanding PID allocation, the behavior of fork(), and the lifecycle of orphan and zombie processes. Proper handling—through waiting, signal handling, or double‑fork techniques—prevents resource leakage and ensures system stability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
