0% found this document useful (0 votes)
10 views

Process Orphan ZombieProcess

Uploaded by

bsbuhazra2021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Process Orphan ZombieProcess

Uploaded by

bsbuhazra2021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROCESS

( Part 2 )

Examples of fork()

1).
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t id;
id=fork();
printf(“ID=%d PID=%d PPID=%d\n”,id,getpid(),getppid());
return 0;
}

Output
ID=6772 PID=6771 PPID=2355
ID=0 PID=6772 PPID=1

Explanation of the above output :


a). First line shows the output of the parent process where ID
shows the id of the child process and PID shows its own
process id and PPID shows the parent process id ie shell
id.
b). Second line shows the output of the child process. For this
reason it shows 0 in ID and shows its own process id in
PID which is same as the output of ID in the first
line.
c). PPID of the second line becomes 1 which is the id of init.

Note :- If parent process execute first and terminate before


completion of child process then init becomes the
immediate parent of that child 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.
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()
{
// Fork returns process id in parent process
pid_t id = fork();
// Parent process didn't use sleep and finished before child
// so the child becomes an orphan process

// Parent process
if (id > 0) {
printf(“\nParent PID=%d PPID=%d\n”,getpid(),getppid());
}
else // Child process
if (id == 0) {
printf(“\nChild PID=%d PPID=%d\n”,getpid(),getppid());
sleep(10); //sleep for 10 second
printf(“\nChild PID=%d PPID=%d\n”,getpid(),getppid());
}
return 0;
}
Output
Parent PID=1231 PPID=1229
Child PID=1232 PPID=1231
Child PID=1232 PPID=1

Zombie Process:
A process which has finished the execution but still has entry in the
process table to report to its parent process is known as a zombie
process. A child process always first becomes a zombie before being
removed from the process table. The parent process reads the exit
status of the child process which reaps off the child process entry
from the process table.
In the following code, the child finishes its execution while the parent
sleeps for 10 seconds, while the child process’s entry still exists in the
process table.

// A C program to demonstrate Zombie Process.


// Child becomes Zombie as parent is not waiting when child process
exits.

#include <stdio.h> //printf


#include <stdlib.h> //exit
#include <sys/types.h> //fork
#include <unistd.h> //fork and sleep

int main()
{
pid_t id = fork();

// Parent process didn't use wait


// so the child becomes a zombie process
// Parent process
if (child_pid > 0){
printf(“\nParent PID=%d PPID=%d\n”,getpid(),getppid());
sleep(10); //sleep for 10 second
}
else // Child process
if(child_pid == 0){
printf(“\nChild PID=%d PPID=%d\n”,getpid(),getppid());
printf(“\nChild PID=%d PPID=%d\n”,getpid(),getppid());
}

return 0;
}

Output
Parent PID=1231 PPID=1229
Child PID=1232 PPID=1231
Child PID=1232 PPID=1231

You might also like