Your program should accept 1 command-line argument, an integer for the number of seconds of the “snooze” alarm. Remember, that to accept command-line arguments, you will need to change the signature of your main function to

computer science

Description

In this assignment, you will develop a C program that will implement an alarm using a parent and child process along with signal handling. In particular, you will implement the following: 

• Your program should accept 1 command-line argument, an integer for the number of seconds of the “snooze” alarm. Remember, that to accept command-line arguments, you will need to change the signature of your main function to: int main(int argc, char *argv[]) { … } You may assume that the user enters an integer along with the command, though the number of command-line arguments may be less than or greater than one. 

• Define a user-defined function called signal_alarm, passing in 1 integer for the signal number. Your function should contain only 1 line of code to display the text "buzz buzz buzz”. 

• Inside your main function: o You should check that exactly 1 argument is passed in (this means that argc should be 2, since the command itself counts as 1). If the user does not pass exactly 1 argument, display a usage statement and terminate the program. o Generate a seeded, random number (integer) between 5 and 10, inclusively. o Install the signal_alarm signal handler for the type 


SIGALRM. o Use the fork system call to create a child process: § Inside the child process, loop through the number of times as the integral value passed in as the command-line argument, where inside this loop you will sleep for 1 second and then print a counter with the words “tick-tock”. Then, outside the loop, if the integral value of the command-line argument is less than the seeded, random number generated earlier, you will send a SIGALRM signal to the child’s parent process using the kill system call; otherwise, you will send a SIGALRM signal to this (i.e., the child) process using the raise system call. 2 § Inside the parent process, schedule the alarm using the random number generated earlier. This can be done using the alarm system call, passing in the random number. Then, suspend this (i.e., the parent) process using the pause system call and finally, use the wait system call to wait for the child process to finish. § If there is an error creating the child process, use the perror system call, passing the string “fork". Now, run the program and notice the results from the SAMPLE OUTPUT below. If the value of the snooze value (i.e., the command-line argument) is less than the random number generated, your child process will send a SIGALRM signal to the parent to print "buzz buzz buzz” and then terminate. Otherwise, the scheduled alarm will signal from the parent process causing the “buzz buzz buzz”, but the child process will continue to execute beyond that, ultimately terminating with a second “buzz buzz buzz” (i.e., the snooze alarm). Note that the number after the “alarm application starting:” text is the random number that was generated. 


SAMPLE OUTPUT

 (user input shown in bold green): $ ./a.out 8 alarm application starting: 10 1: tick-tock 2: tick-tock 3: tick-tock 4: tick-tock 5: tick-tock 6: tick-tock 7: tick-tock 8: tick-tock buzz buzz buzz $ ./a.out 8 alarm application starting: 7 1: tick-tock 2: tick-tock 3: tick-tock 4: tick-tock 5: tick-tock 6: tick-tock buzz buzz buzz 7: tick-tock 8: tick-tock buzz buzz buzz


Related Questions in computer science category