
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Zombie and Orphan Processes in Linux
Every program runs as a process in Linux operating system. When a process ends or gets disconnected from its parent, special types of processes can be created. These processes are known as zombie and orphan processes.
Details about the zombie and orphan processes are given as follows:
Zombie Processes
A zombie process is a process whose execution is completed but it still has an entry in the process table. Zombie processes usually occur for child processes, as the parent process still needs to read its child's exit status. Once this is done using the wait system call, the zombie process is eliminated from the process table. This is known as reaping the zombie process.
A diagram that demonstrates the creation and termination of a zombie process is given as follows
Zombie processes don't use any system resources but they do retain their process ID. If there are a lot of zombie processes, then all the available process ID's are monopolized by them. This prevents other processes from running as there are no process ID's available.
Example
In this example, we create a child process using fork(), where the child exits immediately, making it a zombie process temporarily while the parent keeps running and sleeping for 20 seconds.
#include<iostream> #include<unistd.h> #include<sys/types.h> #include<cstdlib> using namespace std; int main() { pid_t pid = fork(); if (pid > 0) { // Parent process cout<< "Parent process sleeping... (PID: " << getpid() << ")"<< endl; sleep(20); // Delay to keep zombie visible } else if (pid == 0) { // Child process cout<<"Child process exiting... (PID: "<< getpid()<< ")"<<endl; exit(0); } else { cerr<<"Fork failed!"<<endl; } return 0; }
Following is the output to the above program:
Parent process sleeping... (PID: 265835) Child process exiting... (PID: 265836)
Orphan Processes
Orphan processes are those processes that are still running even though their parent process has terminated or finished. A process can be orphaned intentionally or unintentionally.
An intentionally orphaned process runs in the background without any manual support. This is usually done to start an indefinitely running service or to complete a long-running job without user attention.
An unintentionally orphaned process is created when its parent process crashes or terminates. Unintentional orphan processes can be avoided using the process group mechanism.
Example
In this example, we create a child process, but the parent exits immediately, leaving the child to be orphaned and later adopted by the system's init (or systemd) process while it continues running.
#include<iostream> #include<unistd.h> #include<sys/types.h> #include<cstdlib> using namespace std; int main() { pid_t pid = fork(); if (pid > 0) { // Parent exits immediately cout<<"Parent exiting... (PID: "<<getpid()<< ")"<<endl; exit(0); } else if (pid == 0) { // Child sleeps, gets adopted by init/systemd sleep(10); cout<<"Child running, now adopted by init (PID: "<<getpid()<<")"<<endl; } else { cerr<<"Fork failed!"<<endl; } return 0; }
Following is the output to the above program:
Parent exiting... (PID: 267909) Child running, now adopted by init (PID: 267910)
The difference between Zombie and Orphan Processes in Linux:
Process Type | Parent Alive? | Child Alive? | System Impact |
---|---|---|---|
Zombie | Yes | NO | Wastes PID(temporary) |
Orphan | No | Yes | Handled by init process |