Introduction To Signals: Operating System Lab
Introduction To Signals: Operating System Lab
Introduction to Signals
You already know that you can stop the execution of most running programs by
interrupting them from the keyboard, usually with Ctrl-C.
What actually happens is that the part of the Linux kernel software that controls
the keyboard, sends a message to the running process through CPU. This
message is called a signal. Infect there are about 30 different signal types that can
be sent to a Linux process, each of which is generated by some specific hardware
or software condition. In addition to the normal mechanisms for generating and
sending signals, the kill command can also be used to send any signal to a process
at will.
Each of the signal is given a small integer value which is used as command line
switch to kill, along with process ID of the process to which the signal should be
sent. The general format of the kill command is:
Kill [-sig] pid
Where sig is the signal to send and pid is the process ID to which it should be
sent. The square brackets just indicate that the specification of the pid is optional
– the brackets themselves should not be type. If a signal number is not specified
to the kill command then the terminate signal is used by default.
1
Operating System Lab Lab – 13
Spring- 2019 Signal Handling
Some of the more common signals and their numeric values are:
Signal Value Notes
SIGHUP 1 Used to kill your processes when you logout
SIGINT 2 Generated from keyboard (Ctrl-C)
SIGQUIT 3 Generated from keyboard (Ctrl-\)
SIGILL 4 Illegal Instruction
SIGFPE 8 Floating point exception
SIGKILL 9 Sure process termination – cannot be ignored
SIGALRM 14 Generated at the end of an alarm() system call
SIGTERM 15 Default signal sent by kill command
When a process receives a signal, it can normally do one of three different things;
1. Ignore the signal;
2. Accept the signal’s default action; defined in OS.
3. Execute some code to deal with the signal.
Trap a Signal:
From inside a shell script it is possible to trap a signal and then to get a sequence
of commands executed when that signal is received. This is done with the trap
command. There are three basic forms for the trap command, one for each of the
tree different signal response types:
Trap "commands" signal_list
Trap will execute the commands in the quotes when any of the signals
corresponding to the numbers in the signal_list is received by the script.
To restore signals to their default action, use the trap command in the form:
Trap signal_list
2
Operating System Lab Lab – 13
Spring- 2019 Signal Handling
Exercise # 1:
Please read above first and then try to execute following example;
This script starts by setting itself up to trap signals 2 and 3 (Ctrl-c and Ctrl-\ from
the keyboard). If either of these two signals occurs, it will usually be because the
user is trying to break into the script and abort its operating. After the trap has
executed, signal 2 and 3 will cause execution of the commands:
Signal Handling in C:
Exercise 2:
Type the following and execute it.
3
Operating System Lab Lab – 13
Spring- 2019 Signal Handling
Press <ctrl-c> while the program is running and see what happens.
Let’s discuss the program. The first question that comes to mind is… why is there
a call to the signal function inside the signal handler (this is what we will be
calling the catcher function)? When the signal is caught once, the function which
handles the signal is reset, and needs to be set again. If we don’t do this, the
default action will occur. Try commenting out the function and see what happens.
The working of the program is simple. It will run normally and print working
after every second. When the signal is caught, the normal flow of the program is
interrupted. The catcher function is executed and then the normal flow of the
program resumes.
You will notice that commenting the signal line made no difference. Does that
make everything I just mentioned wrong? No. The original UNIX signal () would
reset the handler and System V (and the old Linux kernel and libc4,5) do the
same. On the other hand, BSD does not reset the handler. But remember, that if
you’re working on UNIX and Linux (before version 6.0, not sure about 6.1), the
function has to be set again. All our code will assume that this is how the signal
function works.
4
Operating System Lab Lab – 13
Spring- 2019 Signal Handling
Exercise 3:
Execute following code.