Program to demonstrate Zombie and Orphan Process

/////////////////////////////////////////////////////////////////Zombie process///////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

void main(){
	pid_t child_pid, parent_pid;
	
	child_pid = fork();
	
	if (child_pid<0){
		printf("Fork Unsuccessfuln");
		exit(0);
	}
	if (child_pid==0){
		printf("[CHILD] My PID is: %dn", getpid());
		printf("[CHILD] My parent's PID is: %dn", getppid());
		
		printf("[CHILD] I am exitting...n");
		exit(0);
	}
	else{
		printf("[PARENT] My PID is: %dn", getpid());
		printf("[PARENT] My child's PID is: %dn", child_pid);
		
		printf("[PARENT] I'm sleeping for 10 secs...n");
		sleep(10);
		
		printf("[PARENT] My child (pid:%d) has terminated but its entry is in the process table. It's a zombie processn", child_pid);
	}

}
///////////////////////////////////////////////////////////////Orphan process/////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

void main(){
	pid_t child_pid, parent_pid;
	
	child_pid=fork();
	
	if (child_pid<0){
		printf("Fork Unsucessfuln");
		exit(0);
	}
	if (child_pid==0){
		printf("[CHILD] My PID is %dn", getpid());
		printf("[CHILD] My parent's PID is %dn", getppid());
		
		printf("[CHILD] I am sleeping for 10 secs...n");
		sleep(10);
		
		printf("[CHILD] My Parent Process has terminated. I am an orphan process adopted by Init Process(pid:%d)n",getppid());
	}
	else{
		printf("[PARENT] My PID is %dn", getpid());
		printf("[PARENT] My child's PID is %dn", child_pid);
		
		printf("[PARENT] I am exitting...n");
		exit(0);
	}
}

Rich Engineer - Study Materials, Tutorials, Updates and much more!
Leave a Reply