Home‎ > ‎CIS 98‎ > ‎

Shell Signal Handling

UNIX has built into the operating system a means for asynchronous communication - either between running process, or between a user and a running process. Signals provide the form of communication. Signals are asynchronous messages that can be captured and handled by the receiving process. Signals are sent using the kill command.

  1. Objectives
    • Define the term signal.
    • Identify sources of signals
    • Use the shell trap command
  2. Asynchronous Events
    An asynchronous event is one whose timing cannot be predicted. As a result, one cannot know:
    • When they will occur
    • Which process will be executing
    • Where in the code the process will be executing
    Sources of interrupts:
    • Interrupts from external devices
    • Exceptions, traps, and faults from the CPU
    The kernel informs a process that an event has occurred by sending the process a signal.
  3. Signals
    • In software, signals are denoted by a number.
    • The default action upon receipt of a signal is for the process to terminate
    • Some signals cause a core file to be generated when the process terminates
  4. Generating Signals
    • Keyboard signals
      Two signals may be generated by the keyboard: SIGINT and SIGQUIT
      The keys used to generate these signals may be configured using the stty command. The defaults on Linux are typically:
      1. SIGINT ^C (Control-C)
      2. SIGQUIT ^\ (Control-\)
    • The kill command
      Any signal can be sent to a process using the kill command, however, for the signal to have any effect the uid of the process must match the uid of the user using the kill command.
      • Syntax:   kill PID ...
              kill -n PID ...
      • PID is the process id number of the process to receive the signal
      • n is the number of the signal being sent, the default is to send signal 15
  5. The trap Command
    The shell has a builtin trap command which is used to handle signals within a shell script.
    • Syntax:
      trap command-list n ...
    • Catching signals
      The first argument to the trap command is the name of a function, or list of commands, that will be executed when the specified signal(s) is received. Use quote marks to deal with arguments to the command-list.
      trap 'rm /tmp/temp$$; exit 1' 2 3 15
    • Ignoring signals
      To ignore a signal, just provide a null argument 1:
      trap '' 2
    • Resetting signals
      Reset a signal to its default behavior:
      trap 2 3
    Exercise 4

Exercise 1: Signals

Run the kill command with the -l option
	 Name		#	Description
	SIGHUP		1	Hangup
	SIGINT		2	Interrupt (rubout)
	SIGQUIT		3	Quit
	SIGILL		4	Illegal Instruction
	SIGFPE		8	Floating Point Exception (Divide by 0)
	SIGKILL		9	Kill - cannot be caught or ignored
	SIGTERM		15	Software termination signal

Exercise 2: Generating Signals

  1. Run the command: stty -a
    and find the currently configured keys that perform the following operations:
    1. Interrupt (intr)
    2. Quit (quit)
    3. Erase (erase)
    4. kill (kill)
    5. End of File (eof)
    6. Suspend (susp)
    Only two of these generate signals
  2. Run the command: sleep 30
    and interrupt it by pressing the Interrupt key
  3. Run the command: sleep 30
    and interrupt it by pressing the Quit key. Any difference in behavior?
  4. Does the End of File key intterrupt a sleep command?

Exercise 3: Using the kill command

  1. Run a sleep 30 command in the background
  2. Send signal 1 to that process and observe what happens.
    Note: you may have to press the Enter key to view the shell's buffer
  3. Repeat steps 1 and 2, but use different signal numbers.
  4. Run the command gotcha and find a way to restore the shell session.

Exercise 4: Catching Signals

#!/bin/sh
# Template which shows characteristics of trap command
# At this point, any incoming signal has the default action
touch tmp$$
...
# Now set a trap for SIGINT (2) signal
trap 'rm tmp$$; exit 1' 2
# Also set a "cleanup" trap
trap 'rm tmp$$; exit 0' 0
...
# Critical code coming up which should not be interrupted.
# Completely ignore the Interrupt signal
trap '' 2
...
# Out of the critical section.  It no longer matters what happens if
# the interrupt signal comes, so set the handler back to default.
trap 2
...
# Now at the end of the program, the trap for signal 0 is still in
# effect, so the temporary file will be removed.
exit 0

Comments