Question: Assignment 4 : Write a parent program to fork ( 2 ) n processes where n is passed to the program on the command line,
Assignment : Write a parent program to fork n processes where n is passed to the program on the command line, and n can be from to All output should be made with fprintf to the stdout or stderr followed immediately by an fflush Follow these directions exactly!
Flag ALL errors, including those returned from any functions, with output to the stderr, a call to perror if applicable and then exit with the exit function.
pidt pid;
pid fork;
if pid
error exit
if pid
put child code here
else
put parent code here
In the forked program recall fork returns to the child process get the process ID and current time; using the current time as a seed to generate a random wait times between and seconds Instead of srand and randr use srandom and random to generate these random numbers, using a seed equal to: the current time returned timet the process ID pidt; see time and getpid This guarantees that the seed is different in every child. The child then sleeps for the random number of seconds see sleep and returns using the exit call with the exit status equal to the number of seconds slept.
In the parent recall fork returns to the parent the process id of the child save the process IDs in the order returned in an array of process IDs. The parent then loops through the process ID array and waits for each process ID to exit using waitpid in the order in which they were started.
To get and display the current time use time with a NULL or parameter, and then call ctime with the value returned by the time call.
Use the WIFEXITED and WEXITSTATUS macros defined in stdlib.h on the status returned by waitpid to determine the return status of children with normal returns see the text section Do NOT use the WNOHANG option with the waitpid call.
Both the parent and child processes will output information to the stdout and stderr. Following is an example of invoking this program and the kind of output required:
rcm$ assign
Parent: pid forked
Parent: child number with pid exec'd at Sat Jan ::
Child: pid executing with sleep at Sat Jan ::
Parent: pid forked
Parent: child number with pid exec'd at Sat Jan ::
Parent: pid forked
Parent: child number with pid exec'd at Sat Jan ::
Child: pid executing with sleep at Sat Jan ::
Parent: pid forked
Parent: child number with pid exec'd at Sat Jan ::
Child: pid executing with sleep at Sat Jan ::
Parent: pid forked
Parent: child number with pid exec'd at Sat Jan ::
Child: pid executing with sleep at Sat Jan ::
Child: pid executing with sleep at Sat Jan ::
Parent: child pid exited normally with status at Sat Jan ::
Parent: child pid exited normally with status at Sat Jan ::
Parent: child pid exited normally with status at Sat Jan ::
Parent: child pid exited normally with status at Sat Jan ::
Parent: child pid exited normally with status at Sat Jan ::
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
