Zombie And Orphan Processes In C

Zombie Processes

A zombie process finishes its execution but maintains an entry in the process table to notify its parent process. Before being removed from the process table, a child process transitions into a zombie state. The parent process retrieves the exit status of the child process and eliminates the child process's entry from the process table.

If a parent process neglects to execute the wait or waitpid function to collect the exit status of its child processes, these processes may accumulate, leading to a significant volume of zombie processes. While this accumulation of zombie processes can consume system resources like process table slots, the immediate impact is typically minimal until a substantial number of zombie processes remain unclaimed.

In this code snippet, the child process finishes its execution by employing the exit system call. Meanwhile, the parent process goes into a sleeping state for 40 seconds without invoking wait, leading to the child process's entry persisting in the process table.

Example:

Example

// A C program to implement the Zombie Process. 

// When the child's procedure ends, the child becomes a zombie. 

#include <stdlib.h> 

#include <sys/types.h> 

#include <unistd.h> 

int main() 

{ 

	// Forks returns the process ID of the parent process. 

	pid_t child_pid = fork(); 



	// condition to enter the parent process

	if (child_pid > 0) 

		sleep(40); 



	//The else part of the child process

	else		

		exit(0); 



	return 0; 

}

It is an example program that utilizes the fork function.

Orphan Processes

An orphan process refers to a child process that continues running even after its parent process has terminated or finished its execution. In cases where the parent process ends before the child processes, orphaned children are abandoned.

Orphaned tasks are adopted by the init process (often with PID 1), serving as the progenitor of the system. The init process embraces orphaned tasks and assumes the role of their new guardian. By having a different parent process that will eventually receive their termination signal, orphan tasks are averted from transforming into zombie tasks.

Orphan processes typically persist unaffected by the termination of their parent process. Nonetheless, they might encounter issues if they relied on specific resources or connections to their parent that are no longer accessible.

Example:

Example

// Program for creation of fork() which desribes about the Zombie and Orphan process

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/wait.h>



void creatingZombieProcess() {

 pid_t child_pid = fork();



 if (child_pid > 0) {

 // The parent process creation

 printf("The parent process with PID: %d\n", child_pid);

 printf("The parent process waits...\n");

 sleep(10); // time for sleeping of parent process

 printf("Parent process is completed.\n");

 } else if (child_pid == 0) {

 // the child Process

 printf("Execution of the child process.\n");

 exit(0);

 } else {

 // if the creation of fork is failed()

 perror("Fork failed");

 exit(EXIT_FAILURE);

 }

}



void creatingOrphanProcess() {

 pid_t child_pid = fork();



 if (child_pid > 0) {

 // The parent process creation

 printf("The parent process with PID: %d\n", child_pid);

 printf("The parent process is terminated.\n");

 // termination of the parent process

 exit(0);

 } else if (child_pid == 0) {

 // 

 printf("Child process executing and then sleeping for a while...\n");

 sleep(10); //the sleep time for child process

 printf("Child process completed.\n");

 } else {

 // if the creation of fork is failed()

 perror("Fork failed");

 

 exit(EXIT_FAILURE);

 }

}



int main() {

 printf("The process of creating the Zombie process..\n");

 creatingZombieProcess();



 printf("\nCreation of the Orphan process...\n");

 creatingOrphanProcess();



 // To avoid zombie processes, the parent waits for all child processes to finish.

 printf("\nThe parent process is waits for all child processes\n");

 while (wait(NULL) > 0);



 printf("If all processes are completed.The parent process is exits \n");

 return 0;

}

Output:

Output

The process of creating the Zombie process..

The parent process with PID: 7941

The parent process waits...

Execution of the child process.

Parent process is completed.



Creation of the Orphan process...

The parent process with PID: 7988

The parent process is terminated.

Child process executing and then sleeping for a while...

Child process completed.



The parent process is waits for all child processes

If all processes are completed. The parent process is exits

Input Required

This code uses input(). Please provide values below: