What is an orphan process?

Normally in a process life cycle, the process completes its task and then exits from the list of processes running in the system freeing up the hardware resources it was utilizing. Processes can also create sub-processes known as children's processes to do other tasks. The process that created the child process is known as the parent process.

Orphan process

An orphan process is a child process currently performing its execution, whose parent has finished its execution and has terminated, leaving the process table without waiting for the child process to finish.

How are they created?

Now let's observe how orphan processes are created with the help of a diagram.

How processes become orphan
How processes become orphan

In the diagram, we see that a parent process creates a child process using the fork() system call. The parent then finishes its execution and does not wait for the child process to finish. When the parent process terminates, the child does not have a parent process. Hence, it becomes an orphan process.

Note: When a process becomes an orphan process, the kernel assigns it the init process as its new parent so it gets adopted by the init process.

Code example

Below, we can see a C code example that further explains the creation of an orphan process.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){

    int childPid = fork();

    if(childPid == -1){
        printf("Error creating a child process!\n");
        exit(-1);
    }

    if(childPid == 0){
        printf("\nChild: I am going to sleep for 2 seconds!\n");
        sleep(2);
        printf("\nChild: I have awoken!\n");
        printf("\nChild: I have the PID: %d, and my parent's PID: %d\n", getpid(), getppid());
        exit(0);

    }
    else{
        printf("\nParent: I have the PID: %d, and my child's PID: %d\n", getpid(), childPid);
        printf("\nParent: I am exitting!\n");
        exit(0);
    }
}
Orphan process C code demonstration

When we run the code above, we see that first, the parent finishes its execution and exits without waiting on the child process.

When the child process wakes up, it displays its PID and the parent PID. In place of the parent PID, we see PID 1, which is not the actual PID of the parent. The PID of 1 refers to the init process, which shows that the process is an orphan process that the init process has been adopted.

Code explanation

  • Line 7: We call fork() to create a new process called the child process.

  • Lines 9–12: Check if the fork() function returned a -1 value representing an error in creating a child process.

  • Lines 14–21: Here, we write the code for the child process. The child process sleeps for 2 seconds using the sleep() function call and displays its own and its paternt PID using the functions getpid() and getppid().

  • Lines 22–26: Here, we write the code for the parent process. The parent process also prints its PID and the child's PID. It then exits before the child, so the child becomes an orphan.

Practice quiz

Now that we have understood what an orphan process is and looked at how they are formed using a C application, we will try to test our knowledge by solving the quiz below.

1

When does an orphan process occur?

A)

When the parent process terminates before the child process.

B)

When the child process terminates before the parent process.

Question 1 of 40 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved