Signals and Signal Processing
It is always good practice to write several
small programs that do specific things and
combine them to do a task than write a large
Ideally you would like several cooperating
UNIX provides a rich environment for IPCs
some of these are
We will focus on
in this chapter and
Suppose you’re running a UNIX command
If the command does not terminate in a
reasonable period of time, one typically
terminates this by pressing ctrl
The command is terminated and the shell
What actually happens?
The part of the
responsible for the
keyboard input sees the interrupt character.
then sends a signal called
SIGINT to all the processes that recognize
the terminal as their controlling terminal.
This includes the invocation of
receives the signal, it performs the
default action associated with SIGINT and
What actually happens contined
process also receives the signal.
It sensibly ignores the signal !
Programs can elect to “catch” SIGINT.
When they catch the signal they execute a
special interrupt routine (aka interrupt
service routine (ISR)).
Signals and the kernel
Signals are also used by the kernel to deal
with certain kinds of severe error.
Suppose a corrupt program containing
illegal machine instructions is executed
(accidentally or intentionally).
A process begins execution of the program.
Signal and the kernel
The kernel will detect the attempt to execute
the illegal instructions and send the process
the signal SIGILL (ILL stands for illegal) to
terminate it. It may look something like this
Illegal instruction. Core dumped.
Process to process signals
Signals can also be sent between processes.
Consider the following:
$ gcc verybigprog.c &
$ // after a long time
sends a SIGTERM signal.
SIGTERM will terminate the process.
Process response to signals
A process can do three things with signals:
Choose the way it responds when it receives
a particular signal (signal handling).
Block out signals (that is, leave them for
later) for a specific piece of critical code.
Send a signal to another process.
Signals are given mnemonic names.
They are defined in <signal.h>, with the
They stand for small positive integers.
Most are intended for the kernel, although a
few are provided to be sent from process to
Process abort signal
(sent by the
kernel after timer has expired).
(sent if a hardware
fault is detected
Child process terminated
(whenever a child process stops or
terminates, the kernels sends this to the
Continue executing if stopped
(This is a job control signal which will
continue if the process is stopped, else
ignored, inverse of SIGSTOP)
point exception (AT).
The hangup signal
. (The kernel
sends this to all processes attached to a
when the terminal is
applies to sessions as well).
Illegal instruction. (AT)
(special signal sent from
one process to another to terminate the
cannot be ignored).
Write on pipe or socket when
recipient has terminated.
generated by the kernel when an open file
descriptor is ready for input or output).
Profiling time expired.
is generated when timer expires and can be
used by interpreters to profile execution).
(similar to SIGINT)
Invalid memory reference. (AT)
(segmentation violation, generated when a
process attempts to access invalid memory)
signal, cannot be ignored)
Invalid system call. (AT)
kernel when a process attempts to execute a
machine instruction which is not a sys call)
Software termination signal
(used to terminate a process)
used by debuggers such as sdb and adb,
Terminal stop signal.
generated by the user typing ctrl
z, can be
Background process attempting
(when a bg process attempts to read
from the controlling terminal, this signal is
sent and the process is typically stopped)
(generated when the bg
process attempts to write to the controlling
High bandwidth data is
available at a socket.
(signal tells a process
that urgent or out of band data has been
received on a network connection)
SIGUSR1 and SIGUSR2
Virtual timer expired.
mode time expires).
CPU time limit exceeded.
(signal generated when the process exceeds
its maximum CPU time limit).
File size limit exceeded. (AT)
(generated when a process exceeds its
maximum file size limit)
For most signals normal termination occurs
when a signal is received.
The exit status returned to the parent in this
circumstance tells the parent what happened
There are macros defined in <sys/wait.h>
which allow the parent to determine the
cause of termination and in this particular
case the value of the signal which was
SIGABRT, SIGBUS, SIGSEGV,
SIGQUIT, SIGILL, SIGTRAP, SIGSYS,
SIGXCPU, SIGXFSZ and SIGFPE
The memory dump of the process is written
to a file called
file will include the values of the
program variables, h/w registers and control
Once a signal is received the process may:
Take the default action, which is to terminate
the process. SIGSTOP will stop the process.
Ignore the signal and carry on processing. In
signals may be
Take a specified user
defined action. Whenever
a program exits, whatever the cause, a
programmer might want to perform clean
operations such as removing work files.
Signal sets are one of the main parameters
passed to system calls that deal with signals
They simply specify a list of signals you
want to do something with.
Signal sets are defined of the type
You can empty the set, or remove a few,
keep only the ones that interest you.
int sigempty(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(segset_t *set, int signo);
int sigdelset(segset_t *set, int signo);
Setting the signal action
Once you have defined a signal set, you can
choose a particular method of handling a
int sigaction(int signo, const struct sigaction *act,
const struct sigaction *oact);
structure contains a signal set.
The first parameter
signal for which we want to specify action
SIGSTOP and SIGKILL are exempt
The second parameter
gives the action
you want to set for
The third parameter
will be filled out
with the current settings.
With an interrupt
Restoring a previous action
sigsetjmp and siglongjmp
A program position is saved in in an object of
type sigjmp_buf (defined in <setjmp.h>
sigsetjmp and siglongjmp signals
Sometimes it is useful to go back to a
certain part of a program if a signal occurs
saves the mask of the current
signal set and the current position in the
restores the mask if
encountered (like a long
goto statement) and returns control to the
point in the program where it was saved.
If a program is performing a sensitive task,
it may well need to be protected from
Rather than ignore the signals, a process can
block the signals
The signals will be handled after the
completion of the task.
int sigprocmak(int how, const sigset_t *set,
parameter tells the system call what
specific action to take. For example it coul be
set to SIG_SETMASK, which means block out
the signals in the second parameter
now on. The third parameter is simply filled
with the current mask of blocked signals.
Sending signals to other processes
int kill(pid_t pid, int sig);
Sending signals using raise and alarm
simply sends a signal to the executing
is a simple and useful call that sets up
a process alarm clock. Signals are used to tell
the process that the clock’s timer has expired.
is not like
, which suspends the
, however the alarm clock is turned
off in a child process.
is a companion to
suspends the calling process, in a
way that will not waste CPU cycles, until
any signal, such as SIGALRM, is received.
If the process ignores the signal, the
also ignores the signal.
If the signal is caught, when the appropriate
interrupt routine is finished,
and other IPCs