Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to execute zombie and orphan process in a single C program?
In this section we will see how to execute zombie process and orphan process in a single program in C. Before going to the main discussion, let us see what are the zombie process and orphan process.
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.
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.
Syntax
pid_t fork(void); pid_t getpid(void); pid_t getppid(void); int sleep(unsigned int seconds);
Example: Creating Zombie and Orphan Process
In the following code we will execute zombie and orphan process simultaneously. Here we have a parent process, it has a child, and this child has another child (grandchild). The child process sleeps for 5 seconds to allow the parent to finish, making it an orphan. The grandchild finishes execution while its parent sleeps, creating a zombie process −
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int x = fork(); // create child process
if (x > 0) { // if x is non zero, then it is parent process
printf("Inside Parent---- PID is : %d
", getpid());
// Parent exits quickly, making child an orphan
}
else if (x == 0) { // for child process x will be 0
sleep(5); // wait for parent to finish
printf("Inside Child---- PID :%d and PID of parent : %d
", getpid(), getppid());
x = fork(); // create grandchild
if (x > 0) {
// Child process sleeps, allowing grandchild to finish first
sleep(1);
printf("Child waking up---- Parent PID : %d
", getppid());
}
else if (x == 0) {
printf("Inside grandchild process---- PID : %d, Parent PID : %d
", getpid(), getppid());
// Grandchild exits immediately, becomes zombie until parent reaps
}
}
return 0;
}
Output
Inside Parent---- PID is : 3821 Inside Child---- PID :3822 and PID of parent : 1 Inside grandchild process---- PID : 3823, Parent PID : 3822 Child waking up---- Parent PID : 1
How It Works
- Orphan Creation: The parent process exits quickly while the child sleeps, making the child an orphan (adopted by init process PID 1).
- Zombie Creation: The grandchild process finishes execution before its parent (child) wakes up, creating a zombie entry in the process table.
- Process Hierarchy: Parent ? Child ? Grandchild, where each level demonstrates different process states.
Conclusion
This program demonstrates both zombie and orphan processes in a single execution. The careful timing with sleep() calls creates the necessary conditions for both scenarios to occur simultaneously.
