Question: Exercise 3 : Using fork, exec, and signals: create two programs, a parent and a child. The parent accepts one command line argument: the number

Exercise 3: 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. Before execing each child you will have to alter argv[1] to contain the number (as a character) of 5-second intervals to wait.
Because the parent will be forking and then execing a number of children each child will open (2) an output file named out., where is the childs 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 in the output file, it will also send a SIGUSR1 signal to the parent. In Assignment 6 this line should be commented out; for Exercise 3 you put it back in!
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 (use getcwd(3))
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);
comment: if execve returns use perror(3) and exit
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; argv[1] must be converted to an int
open file: out. using getpid (2) to get pid of child
open output file: 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 (see Assignment 6), 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 10
Parent received child death signal 17
Parent received child user signal 10
Parent received child death signal 17
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 outpExercise 3: 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. Before execing each child you will have to alter argv[1] to contain the number (as a character) of 5-second intervals to wait.
Because the parent will be forking and then execing a number of children each child will open (2) an output file named out., where is the childs 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 eut (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

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 Programming Questions!