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 : 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 second intervals to delay before exiting; the delay will be NOT done with sleep but rather with alarm which sends the child SIGALRM when the timer expires. Before execing each child you will have to alter argv to contain the number as a character of second intervals to wait.
Because the parent will be forking and then execing a number of children each child will open 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 to an out. file instead of the standard output. This avoids having multiple processes competing for the standard output.
The parent will have signal handlers see p in text for an example of how to set this up one for SIGCHLD the signal sent when a child exits and one for SIGUSRsent by each child before it exits The child will have 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 SIGUSR signal to the parent. In Assignment this line should be commented out; for Exercise you put it back in
Parent pseudocode:
Comment: in order to use execve the parent must declare
extern char environ;
convert argv to number print error message and exit if argc
set up for receiving SIGCHLD and SIGUSR signals using signal
set up full pathname to child executable use getcwd
for to number do
fork new process; fork returns a pid
if pid you are in the child, then
change argv to be the number
comment: argv must be in character format
execve the child use execve pathname argv, environ;
comment: if execve returns use perror and exit
end if
end for
comment: in parent here
for to number do
wait for signals using pause
end for
exit
Child pseudocode:
set numseconds to argv; argv must be converted to an int
open file: out. using getpid to get pid of child
open output file: use open with OCREAT OWRONLY, and mode
set up SIGALRM signal handler see pseudocode below
start alarm timer for numseconds, using alarm
wait for the signal to arrive using pause
close output file
exit
Child alarm signal handler pseudocode:
get parent pid using getppid
send the parent SIGUSR using kill see testing suggestion below
Suggestion: write the child process first see Assignment and test it but DO NOT include sending the SIGUSR signal to the parent as executing the child process directly will send the SIGUSR 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 SIGUSR signal to the parent.
Sample parent output for command: $ parent
Parent forks child pid
Parent forks child pid
Parent received child user signal
Parent received child death signal
Parent received child user signal
Parent received child death signal
Sample child output in file out:
Child pid starts alarm timer seconds long
Child pid received timer signal
Child sent SIGUSR to parent
Sample child outpExercise : 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 second intervals to delay before exiting; the delay will be NOT done with sleep but rather with alarm which sends the child SIGALRM when the timer expires. Before execing each child you will have to alter argv to contain the number as a character of second intervals to wait.
Because the parent will be forking and then execing a number of children each child will open 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 to an out. file instead of the standard output. This avoids having multiple processes competing for the standard output.
The parent will have signal handlers see p in text for an eut in file out:
Child pid starts alarm timer seconds long
Child pid received timer signal
Child sent SIGUSR to parent
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
