Advanced Programming in the UNIX Environment: Second Edition [Electronic resources]

W. Richard Stevens; Stephen A. Rago

نسخه متنی -صفحه : 369/ 180
نمايش فراداده

10.12. sigprocmask Function

Recall from Section 10.8 that the signal mask of a process is the set of signals currently blocked from delivery to that process. A process can examine its signal mask, change its signal mask, or perform both operations in one step by calling the following function.

#include <signal.h> int sigprocmask(int

how , const sigset_t *restrict

set , sigset_t *restrict

oset );

Returns: 0 if OK, 1 on error

First, if

oset is a non-null pointer, the current signal mask for the process is returned through

oset .

Second, if

set is a non-null pointer, the

how argument indicates how the current signal mask is modified. Figure 10.13 describes the possible values for

how . SIG_BLOCK is an inclusive-OR operation, whereas SIG_SETMASK is an assignment. Note that SIGKILL and SIGSTOP can't be blocked.

Figure 10.13. Ways to change current signal mask using sigprocmask

how

Description

SIG_BLOCK

The new signal mask for the process is the union of its current signal mask and the signal set pointed to by

set . That is,

set contains the additional signals that we want to block.

SIG_UNBLOCK

The new signal mask for the process is the intersection of its current signal mask and the complement of the signal set pointed to by

set . That is,

set contains the signals that we want to unblock.

SIG_SETMASK

The new signal mask for the process is replaced by the value of the signal set pointed to by

set .

If

set is a null pointer, the signal mask of the process is not changed, and

how is ignored.

After calling sigprocmask, if any unblocked signals are pending, at least one of these signals is delivered to the process before sigprocmask returns.

The sigprocmask function is defined only for single-threaded processes. A separate function is provided to manipulate a thread's signal mask in a multithreaded process. We'll discuss this in Section 12.8.

Example

Figure 10.20 and Figure 10.22.

Figure 10.1. (See Exercise 10.9.)

Figure 10.14. Print the signal mask for the process
#include "apue.h" #include <errno.h> void pr_mask(const char *str) { sigset_t sigset; int errno_save; errno_save = errno; /* we can be called by signal handlers */ if (sigprocmask(0, NULL, &sigset) < 0) err_sys("sigprocmask error"); printf("%s", str); if (sigismember(&sigset, SIGINT)) printf("SIGINT "); if (sigismember(&sigset, SIGQUIT)) printf("SIGQUIT "); if (sigismember(&sigset, SIGUSR1)) printf("SIGUSR1 "); if (sigismember(&sigset, SIGALRM)) printf("SIGALRM "); /* remaining signals can go here */ printf("\n"); errno = errno_save; }