Open In App
Related Articles

How to execute zombie and orphan process in a single program?

Improve Article
Save Article
Like Article

Prerequisite: Zombie and Orphan Processes in C

Zombie Process:
A zombie process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process to read its child’s exit status.
A process that terminates cannot leave the system until its parent accepts its return code. If its parent process is already dead, it’ll already have been adopted by the “init” process, which always accepts its children’s return codes. However, if a process’s parent is alive but never executes a wait ( ), the process’s return code will never be accepted and the process will remain a zombie.

Orphan Process:
An orphan process is a process that is still executing, but whose parent has died. They do not become zombie processes; instead, they are adopted by init (process ID 1), which waits on its children.
When a parent dies before its child, the child is automatically adopted by the original “init” process whose PID is 1.

In the following code, we have made a scenario that there is a parent and it has a child and that child also has a child, firstly if our process gets into child process, we put our system into sleep for 5 sec so that we could finish up the parent process so that its child become orphan, then we have made a child’s child as zombie process, the child’s child finishes its execution while the parent(i.e child) sleeps for 1 seconds, hence the child’s child doesn’t call terminate, and it’s entry still exists in the process table.

Below is the implementation of above approach:

// C program to execute zombie and
// orphan process in a single program
#include <stdio.h>
int main()
    int x;
    x = fork();
    if (x > 0)
                              : %d\n", getpid());
    else if (x == 0) {
        x = fork();
        if (x > 0) {
           PARENT PROCESS ID : %d\n", getpid(), getppid());
                               : %d\n", getppid());
        else if (x == 0)
            printf("IN CHILD'S CHILD PROCESS\n
                  MY PARENT ID : %d\n", getppid());
    return 0;


Note: Above code may not work with the online compiler as fork() is disabled.

Last Updated : 04 Sep, 2018
Like Article
Save Article
Similar Reads