Question: Assignment 4 : Write a parent program to fork ( 2 ) n processes where n is passed to the program on the command line,

Assignment 4: Write a parent program to fork (2) n processes where n is passed to the program on the command line, and n can be from 1 to 10. 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 (3) function.
pid_t pid;
...
pid = fork();
if (pid <0){
//error exit
}
if (pid ==0){
// put child code here
}
else {
// put parent code here
}
...
In the forked program (recall fork (2) returns 0 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 1 and 10(seconds). Instead of srand (3) and rand_r (3), use srandom (3) and random (3) to generate these random numbers, using a seed equal to: the current time (returned time_t)* the process ID (pid_t); see time(2) and getpid(2). This guarantees that the seed is different in every child. The child then sleeps for the random number of seconds (see sleep (3)) and returns using the exit(3) call with the exit status equal to the number of seconds slept.
In the parent (recall fork(2) 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 (2)) in the order in which they were started.
To get and display the current time use time (3) with a NULL or 0 parameter, and then call ctime (3) with the value returned by the time (3) call.
Use the WIFEXITED and WEXITSTATUS macros (defined in stdlib.h) on the status returned by waitpid (2) to determine the return status of children with normal returns (see the text section 8.6). 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$ assign045
Parent: pid 8606 forked
Parent: child number 0 with pid 8606 exec'd at Sat Jan 3107:58:312015
Child: pid 8606 executing with sleep 2 at Sat Jan 3107:58:312015
Parent: pid 8607 forked
Parent: child number 1 with pid 8607 exec'd at Sat Jan 3107:58:312015
Parent: pid 8608 forked
Parent: child number 2 with pid 8608 exec'd at Sat Jan 3107:58:312015
Child: pid 8607 executing with sleep 1 at Sat Jan 3107:58:312015
Parent: pid 8609 forked
Parent: child number 3 with pid 8609 exec'd at Sat Jan 3107:58:312015
Child: pid 8608 executing with sleep 6 at Sat Jan 3107:58:312015
Parent: pid 8610 forked
Parent: child number 4 with pid 8610 exec'd at Sat Jan 3107:58:312015
Child: pid 8609 executing with sleep 5 at Sat Jan 3107:58:312015
Child: pid 8610 executing with sleep 8 at Sat Jan 3107:58:312015
Parent: child pid 8606 exited normally with status 2 at Sat Jan 3107:58:332015
Parent: child pid 8607 exited normally with status 1 at Sat Jan 3107:58:332015
Parent: child pid 8608 exited normally with status 6 at Sat Jan 3107:58:372015
Parent: child pid 8609 exited normally with status 5 at Sat Jan 3107:58:372015
Parent: child pid 8610 exited normally with status 8 at Sat Jan 3107:58:392015

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!