Home‎ > ‎CIS 98‎ > ‎

Lab 10: Signal Handling

The purpose of this lab is to build upon your game script and utilize signal trapping to protect itself from abnormal termination and to manage temporary file cleanup. 

Programming Assignment

Step 1
The game script aquires a random number from 1 to 32768 and then asks the user to guess the number. Each time the user guesses a number, the script should record the guess in a temporary file, and tell the user whether the number guessed was too low or too high. The script will continue to prompt the user for another guess until the correct number is entered. When the correct number is entered, the script will terminate with a message displaying the correct number and all the wrong guesses.

Step 2
Once you have the above script working, add three trap statements:
  1. A trap statement that will cause the script to ignore SIGINT
  2. A trap statement that will execute the exit command if it gets SIGQUIT or SIGTERM
  3. A trap statement that will remove the temporary file when the shell exits

Step 3, Extra Credit
For extra credit, write another script, called timer, that will be launched by your game script. This timer script will require a single argument which must be the PID of the parent script. If it doesn't get this, it should exit with a usage message. Otherwise it should be written to go into an infinite loop of sleeping for 10 seconds and then sending SIGUSR1 to its parent.
This means you have to go back and modify the parent script, game and write a timer_handler function to catch the SIGUSR1 and increment a timer variable by 10 each time it gets the call.
For proper cleanup, the game script should terminate the timer script whenever it exits, and it should report to the user how long it took for him/her to guess the number.

To Submit

When you are done, copy your new game script to a directory called lab10.

Grading Rubric

4 points - Timliness
An executable shell script named game is turned in by midnight Nov. 19.
4 points - Syntax and Grammar
The scripts run to completion with no errors.
4 points - Correctness
The script does what it is supposed to do
4 points - Generality
The script works just as well when run by root as it does run by a regular user regardless where on the system the command is executed from.
4 points - Style
The code is well documented and the complexity of the code matches the complexity of the task.