In: Computer Science
unix question
please write a program and then show how to use top command that shows the zombie process, give
snapshot of top command that shows the zombie process
The main process will create a child.
The child prints something like: “I am the child with pid ….. and my parent has ppid ….” Next, the child will sleep for 1 second.
Child exits.
The parent will print: I am the parent and my id is… Next, the parent sleeps for 30 seconds.
Since the child ends first, and the parent didn’t do wait( ), the child will be for a while in the zombie state. Run the parent in the background, so you can use the top command and identify the zombie, before the parent terminates.
Note: even if the parent terminates, the child is still a zombie. However the the init process reaps the zombies frequently.
Zombie.c
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
#include <unistd.h>
int main()
{
// fork() creates child process identical to parent
int pid = fork();
// if pid is greater than 0 than it is parent process
// if pid is 0 then it is child process
// if pid is -ve , it means fork() failed to create child process
if(pid<0)
{
//exit(1);
printf("failed to fork\n");
return -1;
}
//Parent Process
if(pid ==0)
{
sleep(1);
printf("I am the child process with pid %d and my parent has ppid %d\n ",getpid(),getppid());
exit(1);
}
else
{
printf("\nI am the parent and my id %d ",getpid());
sleep(30);
exit(0);
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main () {
system("./Assignment2a &");
system("ps -l");
sleep(3);
system("kill -9 $(ps -l|grep -w Z|tr -s ' '|cut -d ' ' -f 5)");
sleep(7);
printf("\n\nupdated list of processes with their states\n\n");
system("ps -l");
return(0);
}