Zombie process vs Orphan process

Viewed 87974

A Zombie is created when a parent process does not use the wait system call after a child dies to read its exit status, and an orphan is child process that is reclaimed by init when the original parent process terminates before the child.

In terms of memory management and the process table how are these processes handled differently, specifically in UNIX?

What is an example or extreme case when the creation of zombies or orphans can be detrimental to the greater application or system?

9 Answers

Orphan - Parent exit , Init process becomes the parent of child process. Whenever child is terminated, process table gets deleted by os.

Zombie - When the child terminates it gives exit status to parent. Meanwhile time suppose your parent is in sleep state and unable to receive any status from child. Though the child exit but the process occupies space in process table

check out this command in linux ubuntu >>ps -eo pid,ppid,status,cmd

If you found something like defunc at the end i.e your process is zombie and occupying space.

Zombie Process: A process that has finished the execution but still has an entry in the process table to report to its parent process is known as a zombie process. A child process always first becomes a zombie before being removed from the process table. The parent process reads the exit status of the child process which reaps off the child process entry from the process table.

Orphan Process: A process whose parent process no more exists i.e. either finished or terminated without waiting for its child process to terminate is called an orphan process.

I would like to add 2 code snippets featuring an orphan and a zombie process. But first, I will post the definition of these processes as stated in the book "Operating System Concepts" by Silberschatz, Galvin and Gagn:

If no parent waiting (did not invoke wait()) process is a zombie

If parent terminated without invoking wait , process is an orphan

Orphan

// A C program to demonstrate Orphan Process.  
// Parent process finishes execution while the 
// child process is running. The child process 
// becomes orphan. 

#include <stdio.h>  //printf
#include <stdlib.h> //exit
#include <sys/types.h> //fork
#include <unistd.h> //fork and sleep
  
int main() 
{ 
    // Fork returns process id 
    // in parent process 
    pid_t child_pid = fork(); 
  
    // Parent process didn't use wait and finished before child
    // so the child becomes an orphan process

    // Parent process 
    if (child_pid > 0) {
        printf("I finished my execution before my child"); 
    }
    else // Child process 
        if (child_pid == 0) { 
            sleep(1); //sleep for 1 second
            printf("This printf will not be executed"); 
        } 
        else{
            //error occurred
        }
  
    return 0; 
}

Output

I finished my execution before my child

Zombie

// A C program to demonstrate Zombie Process. 
// Child becomes Zombie as parent is not waiting
// when child process exits. 

#include <stdio.h>  //printf
#include <stdlib.h> //exit
#include <sys/types.h> //fork
#include <unistd.h> //fork and sleep

int main() 
{ 
    // Fork returns process id 
    // in parent process 
    pid_t child_pid = fork(); 
 
    // Parent process didn't use wait 
    // so the child becomes a zombie process

    // Parent process 
    if (child_pid > 0){ 
        sleep(1); //sleep for 1 second
        printf("\nI don't wait for my child");
    }
    else // Child process 
        if(child_pid == 0){ 
            printf("My parent doesn't wait me");
            exit(0);
        }
        else{
            //error occurred
        }
    
    return 0; 
} 

Output

My parent doesn't wait me

I don't wait for my child

Edit: Source and inspiration taken from here

A process which has finished the execution but still has the entry in the process table to report to its parent process is known as a zombie process. A process whose parent process no more exists i.e. either finished or terminated without waiting for its child process to terminate is called an orphan process

Here is one summary per doc

Zombie Process Orphan Process
A Zombie is a process that has completed its task but still, it shows an entry in a process table. A child process that remains running even after its parent process is terminated or completed without waiting for the child process execution is called an orphan.
Zombie process states always indicated by Z The orphan process was created unknowingly due to a system crash.
The zombie process is treated as dead they are not used for system processing An orphan process is a computer process even after their parent terminates init is become a parent and continue the remaining task.
To remove the zombie process execute the kill command. Terminate the Orphan process using the SIGHUP signal.

Another good article list some scenarios.

Related