Question
Write a Cprogram called runsim.c. The program takes exactly onecommand-line argument that specifies the maximum number of simultaneous execution. Using implementation of fork(). The runsimprogram
Write a Cprogram called runsim.c. The program takes exactly onecommand-line argument that specifies the maximum number of simultaneous execution. Using implementation of fork(). The runsimprogram runs up to pr_limit processes at a time.
Follow the outline below for implementing runsim.
Check the appropriate command-line argument and output a usage message if the command line is incorrect
.Initialize pr_limitfrom the command line. The pr_limitvariable specifies the maximum number of children allowed to execute at a time.
The pr_countvariable holds the number of active children. Initialize it to 0.
Execute the following main loop until the end-of-file is reached on standard input:
*If pr_countis pr_limit, wait for a child to finish and decrement pr_count.
*Read a line from standard input (fgets) of up to MAX_BUFcharacters and execute a program corresponding to that command line by forking a child and execute the file.oIncrement pr_countto track the number of active children.
*Check to see if any of the children have finished (**). Decrement pr_countfor each child that has completed.
After encountering end-of-file on standard input, wait for all of the remaining children to finish and then exit.
For each terminated child, prints out its exit code value.** A parent process can check if one of its children terminated without blocking itself by using waitpid()system call as follow waitpid(-1, &status, WNOHANG). The system call returns 0 if there are still children to be waited for, or the pidof the terminated child.
I have seen similar problems on Chegg with the answer just copied and pasted from the other, and that code does not work!!!! No use of fork() or any of the desired variables.
Below program creates a fan of nprocesses where nis passed as a command-line argument. You can expand on this to create the runsimprogram.
#include
#include
#include
int main (int argc, char *argv[]) {
pid_t childpid = 0;
int i, n;
if (argc != 2){
/* check for valid number of command-line arguments */
fprintf(stderr, "Usage: %s processes ", argv[0]);return 1;
}
n = atoi(argv[1]);
for (i = 1; i < n; i++)
if ((childpid = fork()) <= 0)
break;
fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld ",i, getpid(), getppid(), childpid);
return 0;
}
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