Using fork, exec, and signals: create two programs, a parent and a child. The parent accepts one com

Using fork, exec, and signals: create two programs, a parent and a child. The parent accepts one command line argument: the number of children to exec. The child accepts one command line argument: the number of 5-second intervals to delay before exiting; the delay will be NOT done with sleep(3), but rather with alarm(3), which sends the child SIGALRM when the timer expires. Because the parent will be forking and then exec’ing a number of children each child will open(2) an output file named “out.”, where is the child’s pid (in character form, not in int form!). Because we would have several processes writing to the standard output, we will only let the parent do that; each child will write(2) to an out. file instead of the standard output. This avoids having multiple processes competing for the standard output. The parent will have 2 signal handlers (see p. 324 in text for an example of how to set this up), one for SIGCHLD (the signal sent when a child exits), and one for SIGUSR1 (sent by each child before it exits). The child will have 1 signal handler for SIGALRM. The parent signal handlers should merely write that they received a signal to the standard output (see sample output below). The child SIGALRM signal handler, however, will not only write that it received a signal, it will also send a SIGUSR1 signal to the parent. Parent pseudocode: Comment: in order to use execve(2) the parent must declare extern char **environ; convert argv[1] to number (print error message and exit if argc !=2) set up for receiving SIGCHLD and SIGUSR1 signals (using signal(3)) set up full pathname to child executable for 1 to number do fork new process; fork returns a pid if (pid = 0) you are in the child, then change argv[1] to be the 1, 2, 3, … number comment: argv[1] must be in character format execve(2) the child – use execve(pathname, argv, environ); end if end for comment: in parent here for 1 to number*2 do wait for signals using pause(3) end for exit Child pseudocode: set numseconds to argv[1] * 5 open file: out. using getpid(2) to get pid of child comment: use open(2) with O_CREAT | O_WRONLY, and mode 0644 set up SIGALRM signal handler (see pseudocode below) start alarm timer for numseconds, using alarm(3) wait for the signal to arrive using pause(3) close output file exit Child alarm signal handler pseudocode: get parent pid using getppid(2) send the parent SIGUSR1 using kill(2) – see testing suggestion below Suggestion: write the child process first, and test it, but DO NOT include sending the SIGUSR1 signal to the parent (as executing the child process directly will send the SIGUSR1 signal to your shell – which will cause the shell to die!). Once you have written and debugged the parent amend the child code to send the SIGUSR1 signal to the parent. Sample parent output for command: $ parent 2 Parent forks child 1 (pid 54174) Parent forks child 2 (pid 54175) Parent received child user signal 30 Parent received child death signal 20 Parent received child user signal 30 Parent received child death signal 20 Sample child output (in file out.54174): Child 1 (pid 54174) starts alarm timer 5 seconds long Child pid 54174 received timer signal 14 Child sent SIGUSR1 to parent 54173 Sample child output (in file out.54175): Child 1 (pid 54174) starts alarm timer 10 seconds long Child pid 54174 received timer signal 14 Child sent SIGUSR1 to parent 54173

 

Looking for a Similar Assignment? Let us take care of your classwork while you enjoy your free time! All papers are written from scratch and are 100% Original. Try us today! Use Code FREE15