You are on page 1of 6

Signals and Signal Handling Where can signals come from?

Ref: Chapter 6 of [HGS]. • User.

• Kernel: Sends a signal to a process
What is a signal?
(a) if the process does something “bad” (e.g.
• A “one-word” message. bus error, floating point exception, segmen-
tation fault) or
• Examples: Green light, stop sign, a referee’s
gestures in a game. (b) to notify the process of certain events
(e.g. an alarm, resizing a window) or
• A primitive form of inter-process communica-
tion in Unix. (c) when the system is being shut down.
• The signal is itself the message. • Other processes: A process may send the “kill”
signal to another.
Signals at the shell command level:

• CTRL-C to interrupt a command. (This sends Terminology:

a signal to the command that is running as
well as the shell process.) • Synchronous signal: Caused by something
done by a process (bus error, etc.)
• CTRL-Z to stop a process.
• Asynchronous signal: Caused by events out-
• The kill command.
side the process (e.g. CTRL-C).

17–1 17–2
What can a process do with a signal? Notes:

• Accept the default action (usually abnormal • For some signals (e.g. SIGUSR1 and SIGUSR2),
termination). the default action is to ignore the signal.
• Ignore the signal (i.e., wear a ”signal proof • Signals SIGKILL and SIGSTOP cannot be
vest”). ignored or caught.
• “Catch” the signal (i.e., execute a
Signal sets:
signal handler).

Some signals: (a) Data structure:

• Each signal is a symbolic constant denoting • sigset t defined in <signal.h>.

an integer value. (This header is needed for all signal-related
• Complete list on pages 127–130 of [HGS].
• Guaranteed to be large enough to hold all the
#define SIGHUP 1 system defined signals.
#define SIGINT 2
#define SIGQUIT 3 • A signal set is used to specify which signals
#define SIGILL 4 should be blocked from delivery to a process.
#define SIGFPE 8
#define SIGKILL 9
#define SIGSEGV 11
#define SIGTERM 15
17–3 17–4
(b) Permitted operations: Sample code segment:

• Create an empty signal set.

#include <signal.h>
int sigemptyset (sigset_t *sg); sigset_t set1, set2;
• Create a signal set containing all the signals.
int sigfillset (sigset_t *sg); sigaddset(&set1, SIGINT);
• Add a signal to a signal set. sigaddset(&set1, SIGILL);

int sigaddset (sigset_t *sg, sigfillset(&set2);

int signo); sigdelset(&set2, SIGHUP);
• Delete a signal from a signal set. sigdelset(&set2, SIGSEGV);

int sigdelset (sigset_t *sg, if (sigismember(&set2, SIGSEGV))

int signo); printf("Yes\n");
• All the four functions above return 0 if no error else
occurred and -1 otherwise. printf("NO\n");

• Check membership of a signal in a signal set.

(Returns 1 if true and 0 if false.)
int sigismember(const sigset_t *sg,
int signo);

17–5 17–6
Blocking a signal: Structure sigaction:

• Putting a signal on hold while another signal struct sigaction {

is being handled. void (*sa_handler) (int);
• Signals can’t be “stacked” (i.e., only one void (*sa_sigaction) (int,
signal of each type can be outstanding at any siginfo_t *, void *);
time). sigset_t sa_mask;
int sa_flags;
• Signals may be lost.
System call sigaction:
(a) Data member sa handler:
int sigaction (int signo,
• Function pointer – specifies action to be taken
const struct sigaction *act,
for a signal.
struct sigaction *oact);
• Possible values: SIG DEF, SIG IGN or
• Header: <signal.h>. the name of a user-defined function. (This
function will be executed when the specified
• Parameter act specifies how the signal given signal is received.)
by signo is handled.
• SIG IGN cannot be used for SIGSTOP
• Fills in the structure pointed to by oact with or for SIGKILL.
the current setting for the signal.
• The user-defined function gets the signal value
• Returns 0 if successful and -1 otherwise. as the value parameter.

17–7 17–8
(b) Data member sa sigaction: (ii) SA NODEFER: Turn off automatic block-
ing of the signal being handled.
• Also a function pointer. (The corresponding (iii) SA RESTART: Restart system calls upon
function gets additional inputs.) return from the handler.
• Only one of sa handler and (iv) SA SIGINFO: Use the function speci-
sa sigaction may be used. (The data fied for sa sigaction as the signal han-
member sa flags can be used to specify which dler (rather than sa handler).
one is used.)
Program Example: Handout 17.1.
• Not commonly supported; it is best to avoid
using this data member.
Other actions:
(c) Data member sa mask:
(a) To ignore SIGINT:
• Specifies the set of signals to be blocked when
the current signal is handled. act.sa_handler = SIG_IGN;
• Normally, when the signal handler is entered, (b) To restore previous action:
the current signal is also added to the set.
static struct sigaction act, oact;
(d) Data member sa flags:
/* Save old action. */
• Some possible values are: sigaction(SIGINT, NULL, &oact);
(i) SA RESETHAND: Reset the handler to
SIG DFL upon return from the handler.
17–9 17–10
/* New action. */
act.sa_handler = SIG_IGN;
sigaction(SIGINT, &act, NULL);
/* Restore old action. */
sigaction(SIGINT, &oact, NULL);

(c) To exit gracefully when interrupted:

– Define an appropriate function and specify
that function as sa handler.