Slides: Sysprog: Signals¶
Signals are poor notifications to a process
Number between 1 and 31
Sent to a process from another process
Sent to a process by the kernel
Example Signals¶
SIGINT
Ctrl-C
termination
SIGTSTP
Ctrl-Z
suspend
SIGTERM
kill <pid>
termination
SIGALRM
Timer expiration
termination
Many more
Beware of
SIGALRM
. There better ways of timer management, nowadays.
Terminology¶
Generate. A signal is sent.
Deliver. The signal is received by a process (delivered by the kernel). The signal handler is run.
Pending. A signal is pending on a process until it is delivered.
Blocked. A process refuses to get a signal delivered (he “blocks” the signal).
Signal Mask. The set of signals that are blocked by a process.
Default Actions¶
All signals have a predefined default action
The signal is ignored. E.g.
SIGCHLD
.Process termination. Abnormal Process Termination, as opposed to
exit()
. With or without core dump.The process is stopped or continued.
Important Signals¶
Signal |
Default Action |
Reason |
---|---|---|
|
Terminate(core) |
E.g. |
|
Terminate(core) |
Access violation |
|
Terminate(core) |
Access violation |
|
Terminate(core) |
Bogus function pointer |
|
Terminate(core) |
Floating point |
|
Terminate |
|
|
Terminate |
Explicit |
|
Terminate |
Write to pipe/socket |
|
Ignore |
Child death |
Sending, Commandline¶
$ kill -l
... signals here ...
$ kill 1234 # to PID
$ kill -SEGV 1234 # kill as if segfaulted
$ killall firefox
Sending Signals, Programmatically¶
int kill(pid_t pid, int sig);
pid
specifies where the signal goes topid > 0
: processpid == -1
: Broadcast; every process the sender has permissions to. Exceptinit
and the sender itself.pid == 0
orpid < -1
: process group
Warning!¶
Signals are no toy
Signals are no communication medium
Signal handlers are executing in a context that has nothing to do with normal program context -> asynchronous
One does not install a signal handler for e.g.
SIGSEGV
One does not ignore
SIGSEGV
One does not block
SIGSEGV
…
Blocking Signals: Signal Mask¶
Signal Mask
Process attribute (more exactly: thread)
Specifies which signals are blocked
Signals that have been sent to a process but which are blocked remain pending
Pending signals are delivered as soon as they are unblocked
Signals of the same type don’t pile up at the receiver
two
SIGINT
are only delivered once
Signal Mask Manipulation¶
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
Blocks all signal from
set
Returns previously blocked signals in
oldset
Behavior unspecified in multithreaded programs (use
pthread_sigmask()
)
Pending Signals¶
int sigpending(sigset_t *set);
Boring …
Signal Set: sigset_t
¶
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int signum);
int sigdelset(sigset_t *set, int signum);
int sigismember(const sigset_t *set, int signum);
Signal Set: set of signals (obviously). Signals are numbered 1 through 31
sigset_t
is anint
, actually. A bitmask.
Signal Handlers¶
Default action is sufficient in most cases
SIGSEGV
crashes and dumps coreSIGINT
(Ctrl-C) terminates
Customizing signal reception: install signal handler
Pointer to C function
void handler(int sig);
Installing a Signal Handler (1)¶
struct sigaction {
void (*sa_handler)(int);
sigset_t sa_mask;
int sa_flags;
};
int sigaction(int signum,
const struct sigaction *act,
struct sigaction *oldact);
Installing a Signal Handler (2)¶
Special sa_handler values
SIG_IGN
: ignore the signalSIG_DFL
: restore default action
Historical baggage
sighandler_t signal(int signum, sighandler_t handler);
Unclear semantics
Not portable
Effects of Signal Delivery¶
E.g. terminate a program based upon the value of a flag (by dropping out of a loop) that is set in a signal handler. Use …
volatile sig_atomic_t flag;
Blocking system calls (e.g.
read()
orwrite()
) return an error when they have been interrupted by a signalerrno
isEINTR
Last Warning!¶
Signals are delivered asynchronously
Much like hardware interrupts (only in software)
Literally nothing is legal
Only async-signal-safe functions can be used
Practically only system calls
WTF Async Signal Safe?¶
The following functions (among many others) are not async-signal-safe
printf()
,sprintf()
(everything fromstdio.h
andiostream
, respectively)malloc()
,free()
etc.exit()
(_exit()
is safe because a system call)Everything from
pthread.h
Exercise: Signals¶
Write a program that …
… reads from
STDIN_FILENO
in a loop, and outputs what was read toSTDOUT_FILENO
. Imagine that this is a replacement for an immensely important work which can block - the program blocks onSTDIN_FILENO
.On program termination, the program has to do important cleanup work - it has to catch at least
SIGINT
andSIGTERM
.Our cleanup work is to safely - not in the signal handler - write “Goodbye!” to standard output.