Using Linux, write a C program that forks a child process that ultimately become
ID: 3752003 • Letter: U
Question
Using Linux, write a C program that forks a child process that ultimately becomes a zombie process.
This zombie process must remain in the system for at least 10 seconds. Process states can be obtained from the command ps -l
The process states are shown below the S column; processes with a state of Z are zombies.
The process identifier (pid) of the child process is listed in the PID column, and that of the parent is listed in the PPID column.
Perhaps the easiest way to determine that the child process is indeed a zombie is to run the program that you have written in the background
(using the &) and then run the command ps l to determine whether the child is a zombie process.
Because you do not want too many zombie processes existing in the system, you will need to remove the one that you have created.
The easiest way to do that is to terminate the parent process using the kill command.
For example, if the pid of the parent is 4884, you would enter kill -9 4884
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t id;
//fork system call returns child process id in parent process
id = fork();
// Parent process
if (id > 0)
{
printf("Parent will sleep");
sleep(10);
}
//Child process
if (id == 0)
{
printf("I am child");
exit(0);
}
}
Explanation / Answer
//When a parent process exists before child process , it will not be there to collect the status from child process. In this situation child process becomes zombie when it finishes its execution. Below is the program to demonstaite that.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int pid;
pid = fork(); //creates a child process
if( pid > 0 ) //parent process
{
printf("Parent will sleep ");
sleep(10);
printf("Parent exiting ");
}
else //child process
{
printf("I am in child ");
sleep(10);
printf("Child process exiting ");
}
}
---------------------------------------------------------
//compile above progrem as below
$ gcc zombie.c
//execute above program
$ ./a.out &
// using & puts the above process in background, you get commamnd prompt.
//type ps -l
$ ps -l
//when you first type , u get two a.out with their pid's
//After some time , you can notice , the output of the ps -l as shown below which shows child process has become zombie process
-------------------------------------------------
//you can see z in the S column after you run ps -l
1 Z 24140 59 1 0 80 0 - 0 exit pts/1 00:00:00 a.out <defunct>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.