The Linux Programming Interface

(nextflipdebug5) #1

408 Chapter 20


The GNU C library implements three nonstandard functions that perform
tasks that are complementary to the standard signal set functions just described.

These functions perform the following tasks:

z sigandset() places the intersection of the sets left and right in the set dest;
z sigorset() places the union of the sets left and right in the set dest; and
z sigisemptyset() returns true if set contains no signals.

Example program
Using the functions described in this section, we can write the functions shown in
Listing 20-4, which we employ in various later programs. The first of these, printSigset(),
displays the signals that are members of the specified signal set. This function uses
the NSIG constant, which is defined in <signal.h> to be one greater than the highest
signal number. We use NSIG as the upper bound in a loop that tests all signal numbers
for membership of a set.

Although NSIG is not specified in SUSv3, it is defined on most UNIX implemen-
tations. However, it may be necessary to use implementation-specific compiler
options to make it visible. For example, on Linux, we must define one of the
feature test macros _BSD_SOURCE, _SVID_SOURCE, or _GNU_SOURCE.

The printSigMask() and printPendingSigs() functions employ printSigset() to display,
respectively, the process signal mask and the set of currently pending signals. The
printSigMask() and printPendingSigs() functions use the sigprocmask() and sigpending()
system calls, respectively. We describe the sigprocmask() and sigpending() system calls
in Sections 20.10 and 20.11.

Listing 20-4: Functions for displaying signal sets
––––––––––––––––––––––––––––––––––––––––––––––––– signals/signal_functions.c
#define _GNU_SOURCE
#include <string.h>
#include <signal.h>
#include "signal_functions.h" /* Declares functions defined here */
#include "tlpi_hdr.h"

/* NOTE: All of the following functions employ fprintf(), which
is not async-signal-safe (see Section 21.1.2). As such, these

#define _GNU_SOURCE
#include <signal.h>

int sigandset(sigset_t *set, sigset_t *left, sigset_t *right);
int sigorset(sigset_t *dest, sigset_t *left, sigset_t *right);
Both return 0 on success, or –1 on error
int sigisemptyset(const sigset_t *set);
Returns 1 if sig is empty, otherwise 0
Free download pdf