Process Orphan ZombieProcess
Process Orphan ZombieProcess
( 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
#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.
int main()
{
pid_t id = fork();
return 0;
}
Output
Parent PID=1231 PPID=1229
Child PID=1232 PPID=1231
Child PID=1232 PPID=1231