Question: Modify the following program by replacing the final fprintf statement with a loop that reads nchars characters from standard input, one character at a time,

Modify the following program by replacing the final fprintf statement with a loop that reads "nchars" characters from standard input, one character at a time, and puts them in an array called "mybuf". The values of "n" and "nchars" should be passed as command-line arguments. After the loop, put a "'\0'" character in entry "nchars" of the array so that it contains a string. Output to standard error in a single "fprintf" the process ID followed by a colon followed by the string in "mybuf". Run the program for several values of "n" and "nchars". Observe the results. Press the Return key often and continue typing at the keyboard until all of the processes have exited.

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())

break;

fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld ", i, (long)getpid(), (long)getppid(), (long)childpid);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!