Question: Write a C program called runsim.c. The program takes exactly one command-line argument that specifies the maximum number of simultaneous execution. The runsim program runs
Write a C program called runsim.c. The program takes exactly one command-line argument that specifies the maximum number of simultaneous execution. The runsim program runs up to pr_limit processes at a time. The executable must be called runsim. 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_limit from the command line. The pr_limit variable specifies the maximum number of children allowed to execute at a time.
The pr_count variable 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: o If pr_count is pr_limit, wait for a child to finish and decrement pr_count.
o Read a line from standard input (fgets) of up to MAX_BUF characters and execute a program corresponding to that command line by forking a child and execute the file. o Increment pr_count to track the number of active children.
o Check to see if any of the children have finished (**). Decrement pr_count for 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 pid of the terminated child.
In this exercise, use runsim to run multiple copies of the testsim program. The testsim program is given below. It takes two command-line arguments: the sleep time and the repeat factor. The testsim program loops repeat factor times. In the loop, testsim sleeps for the specified sleep time and then output a message with its process ID to standard output.
/* testsim.c */
#include
int main(int argc, char *argv[]) {
int n;
int sec;
if (argc <= 2) {
fprintf(stderr, Usage: %s sleep_time repeat_factor , argv[0]);
exit(1); }
sscanf(argv[1], %d, &sec);
sscanf(argv[2], %d, &n);
while (n-- > 0) sleep(sec);
printf(Process ID = %d , getpid());
return sec; }
To run simulation: Create a test file called testing.data that contains commands to run. For example, the file might contain the following lines.
testsim 5 10
testsim 8 10
testsim 4 10
testsim 13 6
testsim 1 12
Run the program by entering a command such as the following.
runsim 2 < testing.data
Below program creates a fan of n processes where n is passed as a command-line argument. You can expand on this to create the runsim program.
#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;
}
!!!!!!!!!!! PLEASE INCLUDE A SAMPLE OUTPUT OF YOUR PROGRAM & DO NOT COPY THE OTHER CODE POSTED ON CHEGG (IT DOES NOT WORK) !!!!!!!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
