Professional Documents
Culture Documents
Signals
Signals
Signal Concepts
Signals are software interrupts
Used to handle asynchronous events
Each signal is a positive integer, defined by
standardized names
All the signals start with SIG
SIGKILL = 9
SIGTERM = 15
SIGCHLD = 17
SIGINT = 2
Signal Concepts
Signals are defined in <signal.h>
man 7 signal for complete list of signals
and their numeric values
kill –l for full list of signals on a system
64 signals. The first 32 are traditional
signals, the rest are for real time
applications
Conditions That Can Generate
Signals
Generated by terminal
ctrl-c, ctrl-\, ctrl-z etc
Hardware exceptions can generate signals
Division by zero, bad memory reference, etc
“kill” shell command: ex – kill pid
kill function call
int kill(pid_t pid, int sig);
Software can generate signals
SIGALRM, SIGPIPE, etc
Page 292-298 has descriptions of each signal
Signal Dispositions
Ignore the signal
SIG_IGN
Note: SIGKILL and SIGSTOP can not be ignored
Catch the signal
Register a custom signal handler to be run when
the process is sent the corresponding signal
Allow default action
SIG_DFL
All signals have a default action. See page 292 fig
10.1
Signal Function
void (*signal(int signo, void (*func)(int)))(int);
OR
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
signo is the signal number to handle
func defines how to handle the signal
SIG_IGN
SIG_DFL
Function pointer of a custom handler
Returns previous disposition if ok, or
SIG_ERR on error
Unreliable Signals
In older versions of UNIX, signals could
be lost and the process would never
know
Processes had little control over signals.
It could catch or ignore the signal
Sometimes we want to temporarily
block signals and process them later to
protect a critical section of code
Reentrant Functions
Reentrant functions can be interrupted at any
point and resumed later without error
Reentrant functions are thread and signal
safe
If the signal handler is reentrant and does not
call exit then when it finishes, the process will
continue where it was interrupted without
error
Page 306 lists functions guaranteed by
POSIX.1 to be reentrant
Reliable-Signal Terminology
and Semantics
A signal is said to be generated or raised
for a process when an event happens that
causes the signal to occur
A signal is delivered to a process when it
takes action based on that signal
During the time between generation and
delivery, a signal is said to be pending
A process may block some signals using a
signal mask that defines which signals are
currently blocked for the process
kill and raise functions
int kill(pid_t pid, int sig);
int raise(int sig);
Returns
0 if no previous alarm set, or if previous alarm had less than
1 second left
Number of seconds left before alarm
alarm(0) cancels any pending alarms
alarm and pause
int pause(void);
Suspends a process until it receives a signal
representation of it