Zombie and Orphan Process in OS

Learn via video courses
Topics Covered

Introduction

Processes in OS bear parent-child relationship and they have their entry in the system. An orphan process is formed when its parent dies while the process continues to execute, while a zombie process is a process that has terminated but its entry is there in the system.

What is the Zombie process?

Zombie process is also known as "dead" process. Ideally, when a process completes its execution, its entry from the process table should be removed but this does not happen in the case of zombie a process.

Analogy: Zombie, mythological, is a dead person revived physically. Similarly, a zombie process in os is a dead process (completed its execution) but is still revived (its entry is present in memory).

Zombie process

Note: Process table is a data structure in RAM to store information about a process.

What happens with the zombie processes?

  • wait() system call is used for the removal of zombie processes.
  • wait() call ensures that the parent doesn't execute or sits idle till the child process is completed.
  • When the child process completes executing, the parent process removes entries of the child process from the process table. This is called "reaping of child".

zombie process in os

Zombie Process Code Example

In the code given below, we'll see how a zombie process is created.

  • The child process completes its execution by using exit() system call.
  • So when the child finishes its execution ‘SIGCHLD’ signal is delivered to the parent process by the kernel. Parents should, ideally, read the child's status from the process table and then delete the child's entry.
  • But here the parent does not wait for the child to terminate, rather it does its own subsequent job, i.e. here sleeping for 60 seconds.
  • So the child's exit status is never read by the parent and the child's entry continues to remain there in the process table even when the child has died.

Note: Kernel sends a SIGCHLD signal to the parent process to indicate that the child process has ended ·

Why are lots of zombie processes harmful to the system?

A lot of zombie processes in OS are harmful as

  • The OS has one process table of finite size, so lots of zombie processes will result in a full process table.
  • A full process table means that OS cannot create a new process when required and Zombie processes in OS are of no use as the process has died but its entry is occupying the space in memory

An extreme case - Fork bomb The program below creates infinitely many zombie processes because the parent does not wait for its child to process.

zombie process example

  • PIDs in os are finite, when all the PIDs have been consumed by Zombie Process, no new process can be created.
  • Solution: Reboot the system

What is the Orphan Process?

We'll again use real-life analogy to understand the orphan process.

  • In the real world orphans are those children whose parents are dead.
  • Similarly, a process which is executing (is alive) but it's parent process has terminated (dead) is called an orphan process.

What will happen with the orphan processes?

  • In the real world orphans are adopted by guardians who look after them.
  • Similarly, the orphan process in Linux is adopted by a new process, which is mostly init process (pid=1). This is called re-parenting.
  • Reparenting is done by the kernel, when the kernel detects an orphan process in OS and assigns a new parent process.
  • The new parent process asks the kernel for cleaning of the PCB of the orphan process and the new parent waits till the child completes its execution.

orphan processes

Orphan Process Code Example

In the example, the parent process sleeps for 20 seconds while the child process sleeps for 30 seconds.

  1. So after sleeping for 20 seconds the parent completes its execution while the child process is still there for at least 30 seconds.
  2. When the child process becomes an orphan process, the kernel reassigns the parent process to the child process.
  3. As a result the parent process id of the child process before and after sleep() will be different.

Note: Kernel is a central component of an operating system that manages operations of computers and hardware.

Output

Why are too many Orphan processes harmful to the system?

  • Orphan processes in OS hold resources when present in the system.
  • Orphan processes in a large number can overload the init process and hang up a system.

Conclusion

Let's go through what we have learned till now.

  • In Linux OS, processes have parent-child relationships.
  • A zombie process in OS is one that has completed its execution but its entry in the process table.
  • wait() system call is used to deal with zombie processes.
  • An orphan process in OS is one which is executed but it’s parent process has terminated is called an orphan process.
  • Kernel allocates a new process as a parent process to an orphan process. Mostly the new parent is the init process (pid=1).
  • Too many zombie processes and orphan processes are harmful.