Question
#include #include #include using namespace std; int main() { int count = 0; for(int i=0; i <4; i++) { pid_t pid; pid = fork(); if
#include#include #include using namespace std; int main() { int count = 0; for(int i=0; i<4; i++) { pid_t pid; pid = fork(); if (pid == 0) { //child process continues here while(1) { cout << "I am process " << count << endl; sleep(10); } } else { cout << "Child " << ++count << "'PID is " << pid << endl; sleep(0.5); } } }
Modify the c++ program above so that instead of producing 10 children, it produces an endless number of children. The parent process should quit when the pid returned by fork() is -1, indicating that it is no longer able to create new processes.
Before running this program, log onto 3 different terminals (refer to Lab 1 for instructions on how to do this on a non-GUI terminal). On terminal 1, start the top program to monitor running processes. Note the number of running processes reported by top (this is reported as total tasks at the top of the top screen).
Now, run your program on terminal 2. Be sure that you have logged onto terminal 3 prior to running your program on terminal 2.
Make notes about what you observe after running the program:
How many total processes is top now reporting?
Approximately how many processes were forked?
How many are sleeping?
Describe the responsiveness of the system. Go to terminal 3. Are you able to start other processes like ls, ps, kill ,top, etc.? Are you able to start compound processes like:
ls l | more or
ls l | sort | grep 'a'
What , if any, errors are reported? Once you are done, quit top by pressing q. If the name of your executable was a.out, for example, you can kill all the processes with the pkill command: pkill a.out
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started