Question

Using either a UNIX or a Linux system, write a C program that forks a child...

Using either a UNIX or a Linux system, write a C program that forks a child process that ultimately becomes a zombie process. 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. 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 process id of the parent is 4884, you would enter kill -9 4884

i need the code (with comments if possible) and the output (preferably as screenshots it would be clearer). Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Any doubt please comment me. I use linux for this and instead of ps -l I use ps -al. I define the concept in the comments in the program.

Answer)

code

#include <stdlib.h>
int main()
{
//Zombie process is a process which is terminated but the entry remains
//in the process table. And we can delete a zombie process by deleting its
//parent process. Here the program is the parent process and we exceute the child
//process by the fork() function.

pid_t cpid = fork(); //pid_t is used to hold the process ID.
//fork() is used to create a child process and send
//the value to the cpid
//The parent process is the program itself

// checking whether child process is 0. For checking this
// the parent process goto sleep for 500 seconds. In between
// the child process exit. Now that child process cannot be
//deleted from process table because the parent process is alive,
//so the child process become a zombie or defunct.
if (cpid != 0)
sleep(500);

// Child process exit
else
exit(0);

}

save the file and follow the screenshot.

First I compile the program.

Then I execute and send the program in background by &

Now I run the ps -al, which show me zombie by Z in the stat column

Then I kill the parent process ID of the Zombie process, so it automatically deleted.

As the child process is deleted but the parent is still alive, so the child process still remains in the process table, which is a zombie process.

Add a comment
Know the answer?
Add Answer to:
Using either a UNIX or a Linux system, write a C program that forks a child...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT