410 Chapter 20
20.10 The Signal Mask (Blocking Signal Delivery)
For each process, the kernel maintains a signal mask—a set of signals whose delivery
to the process is currently blocked. If a signal that is blocked is sent to a process,
delivery of that signal is delayed until it is unblocked by being removed from the
process signal mask. (In Section 33.2.1, we’ll see that the signal mask is actually a
per-thread attribute, and that each thread in a multithreaded process can indepen-
dently examine and modify its signal mask using the pthread_sigmask() function.)
A signal may be added to the signal mask in the following ways:
z When a signal handler is invoked, the signal that caused its invocation can be
automatically added to the signal mask. Whether or not this occurs depends on
the flags used when the handler is established using sigaction().
z When a signal handler is established with sigaction(), it is possible to specify an
additional set of signals that are to be blocked when the handler is invoked.
z The sigprocmask() system call can be used at any time to explicitly add signals to,
and remove signals from, the signal mask.
We delay discussion of the first two cases until we examine sigaction() in Section 20.13,
and discuss sigprocmask() now.
We can use sigprocmask() to change the process signal mask, to retrieve the existing
mask, or both. The how argument determines the changes that sigprocmask() makes
to the signal mask:
SIG_BLOCK
The signals specified in the signal set pointed to by set are added to the sig-
nal mask. In other words, the signal mask is set to the union of its current
value and set.
SIG_UNBLOCK
The signals in the signal set pointed to by set are removed from the signal
mask. Unblocking a signal that is not currently blocked doesn’t cause an
error to be returned.
SIG_SETMASK
The signal set pointed to by set is assigned to the signal mask.
In each case, if the oldset argument is not NULL, it points to a sigset_t buffer that is
used to return the previous signal mask.
If we want to retrieve the signal mask without changing it, then we can specify
NULL for the set argument, in which case the how argument is ignored.
#include <signal.h>
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
Returns 0 on success, or –1 on error