Difference between Zombie and Orphan Processes

This article provides complete information about Zombie and Orphan process on Linux or Unix Operating system with suitable code example.


Topic

  • What is Zombie process?
  • What is Orphan Process?
  • Zombie vs Orphan Process


Solution


Zombie Process

On Unix and Linux operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table. It is a process in the "Terminated state".

This occurs for child processes, where the process table entry is still needed to allow the parent process to read its child’s exit status: once the exit status is read via the wait system call by the parent process, the zombie’s entry is removed from the process table and it is said to be "reaped". A child process always first becomes a zombie before being removed from the process table. Zombie process cause a resource leak(Only the resource required to maintain a process in the process table). Unlike normal processes, the kill command has no effect on a zombie process.

Zombie processes can be identified in the output from the Unix ps command by the presence of a "Z" in the "STAT" column(ps command). Zombies that exist for more than a short period of time typically indicate a bug in the parent program, or just an uncommon decision to not reap the children. If the parent program is no longer running, zombie processes typically indicate a bug in the operating system. As with other resource leaks, the presence of a few zombies is not worrisome in itself, but may indicate a problem that would grow serious under heavier loads. Since there is no memory allocated to zombie processes, the only system memory usage is for the process table entry itself. The primary concern with many zombies is not running out of memory, but rather running out of process table entries, concretely process ID numbers.

To remove zombie processes from a system, the SIGCHLD signal can be sent to the parent manually, using the kill -s SIGCHILD <PID> command. If the parent process still refuses to reap the zombie, and if it would be fine to terminate the parent process, the next step can be to remove the parent process. When a process loses its parent, init becomes its new parent. init periodically executes the wait system call to reap any zombies with init as parent.

Zombie Process Code Example
// A C program to demonstrate Zombie Process. 
// Child becomes Zombie as parent is sleeping 
// when child process exits. 
#include <stdlib.h> 
#include <sys/types.h> 
#include <unistd.h> 
int main() 
{ 
    // Fork returns process id in parent process 
    pid_t child_pid = fork(); 

    // Parent process 
    if (child_pid > 0) 
        sleep(50); 

    // Child process 
    else        
        exit(0); 

    return 0; 
} 


Orphan Process

Zombie processes should not be confused with orphan processes: an orphan process is a process that is still executing, but whose parent has died. When the parent dies, the orphaned child process is adopted by init or systemd (process ID 1). When orphan processes die, they do not remain as zombie processes; instead, they are waited on by init. The result is that a process that is both a zombie and an orphan will be reaped automatically.

Orphan Process Code Example

In the following code, parent finishes execution and exits while the child process is still executing and is called an orphan process now. However, the orphan process is soon adopted by init process, once its parent process dies.

// 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> 
#include <sys/types.h> 
#include <unistd.h> 

int main() 
{ 
    // Create a child process    
    int pid = fork(); 

    if (pid > 0) 
        printf("in parent process"); 

    // Note that pid is 0 in child process 
    // and negative if fork() fails 
    else if (pid == 0) 
    { 
        sleep(30); 
        printf("in child process"); 
    } 

    return 0; 
} 


Reference
www.geeksforgeeks.org
www.wikipedia.org


You May Also Like

avatar

About the Author: TekFik

TekFik is a technical blogging site helps techies and engineers to solve their day to day issues and also allows everyone to share knowledge and feedback. Please feel free to contact us at tekfik.rd@gmail.com if there is anything.

Leave a Reply

Your email address will not be published. Required fields are marked *