Operating System | Orphan Process - cook the code

Monday 1 January 2018

Operating System | Orphan Process

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.

a child process whose parent process terminates before it does becomes an orphan process. Such situations are typically handled with a special "root" (or "init") process, which is assigned as the new parent of a process when its parent process exits. This special process detects when an orphan process terminates and then retrieves its exit status, allowing the system to deallocate the terminated child process.

Image result for Orphan Processes
orphan process

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;
}

Q. Are Orphan processes harmful for system ?
A. Yes. Orphan processes take resources while they are in the system, and can potentially leave a server starved for resources. Having too many Orphan processes will overload the init process and can hang-up a Linux system. We can say that a normal user who has access to your Linux server is capable to easily kill your Linux server in a  minute.

References:

  1. http://en.wikipedia.org/wiki/Orphan_process
  2. http://en.wikipedia.org/wiki/Init
  3. http://en.wikipedia.org/wiki/Daemon_(computer_software)

No comments:

Post a Comment