Table of contents
1.
Introduction
2.
Understanding Processes
3.
Zombie Processes
3.1.
Example
4.
Orphan Processes
4.1.
Example
5.
Frequently Asked Questions
5.1.
What is a Zombie process in an OS?
5.2.
What is an Orphan process?
5.3.
How does an OS handle these processes?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Zombie and Orphan Process in OS

Author Lekhika
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Every Operating System (OS) hosts a plethora of processes running concurrently. However, managing these processes isn't always seamless. This article provides a detailed perspective on two particular types of processes, known as Zombie and Orphan processes, that every system administrator and developer should understand.

Zombie and Orphan Process in OS

Understanding Processes

Before we delve into Zombie and Orphan processes, it's essential to understand what a process is. A process is essentially a running instance of a program. Each process has a unique Process ID (PID) and is managed by the OS.

Zombie Processes

Zombie, or defunct, processes are processes that have finished execution but still linger in the process table. They occur when a parent process fails to read the child's exit status.

Example

For instance, consider the following C code:

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>


int main(){
    pid_t child_pid;
    
    child_pid = fork();


    if(child_pid > 0){
        sleep(60);   // Parent process sleeps for 60 seconds
    }
    else{
        exit(0);     // Child process exits immediately
    }


    return 0;
}
You can also try this code with Online C Compiler
Run Code


In this code, the child process exits before the parent, becoming a Zombie process for the duration of the parent's sleep time.

Orphan Processes

Orphan processes are those whose parent process has finished or terminated, leaving the child process still running. The OS assigns these Orphan processes to the init process, which periodically performs a wait operation to eliminate any zombie processes.

Example

Consider the following C code:

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>


int main(){
    pid_t child_pid;
    
    child_pid = fork();


    if(child_pid > 0){
        exit(0);     // Parent process exits immediately
    }
    else{
        sleep(60);   // Child process sleeps for 60 seconds
    }


    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

In this case, the parent process exits immediately after the fork, while the child process continues execution, becoming an Orphan process.

Also see, Difference Between Bit and Byte, Demand Paging in OS

Frequently Asked Questions

What is a Zombie process in an OS?

A Zombie process is a process that has completed execution but still remains in the process table because the parent process didn't read its exit status.

What is an Orphan process?

An Orphan process is a running process whose parent process has finished or terminated.

How does an OS handle these processes?

The OS reassigns Orphan processes to the init process and periodically removes Zombie processes from the process table.

Conclusion

Understanding Zombie and Orphan processes is crucial in system administration and process management. While these processes are generally harmless, excessive numbers could indicate an issue and may consume system resources unnecessarily. Hence, monitoring and maintaining healthy process relationships in any OS is paramount for smooth performance.

Recommended Article: fork() system call

Difference Between List and Set

Live masterclass