Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <signal.h>
int sigprocmask(int option, const sigset_t *__restrict__ new_set,
sigset_t *__restrict__ old_set);
Examines, changes, or examines and changes the signal mask of the calling thread. If there is only one thread, it does the same for the calling process.
The signal set manipulation functions: sigemptyset(), sigfillset(), sigaddset(), and sigdelset() must be used to establish the new signal set pointed to by new_set.
If there are any pending unblocked signals, either at the process level or at the current thread's level after sigprocmask() has changed the signal mask, then at least one of those signals is delivered to the thread before sigprocmask() returns.
The signals SIGKILL, SIGSTOP, or SIGTRACE cannot be blocked. If you attempt to use sigprocmask() to block these signals, the attempt is simply ignored. sigprocmask() does not return an error status.
SIGFPE, SIGILL, and SIGSEGV signals that are not artificially generated by kill(), killpg(), raise(), sigqueue(), or pthread_kill() (that is, were generated by the system as a result of a hardware or software exception) will not be blocked.
If an artificially raised SIGFPE, SIGILL, or SIGSEGV signal is pending and blocked when an exception causes another SIGFPE, SIGILL, or SIGSEGV signal, both the artificial and exception-caused signals may be delivered to the application.
If sigprocmask() fails, the signal mask of the thread is not changed.
The use of the SIGTHSTOP and SIGTHCONT signal is not supported with this function.
If successful, sigprocmask() returns 0.
⁄* CELEBS23
This example changes the signal mask.
*⁄
#define _POSIX_SOURCE
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
void catcher(int signum) {
puts("inside catcher");
}
main() {
time_t start, finish;
struct sigaction sact;
sigset_t new_set, old_set;
double diff;
sigemptyset(&sact.sa_mask);
sact.sa_flags = 0;
sact.sa_handler = catcher;
if (sigaction(SIGALRM, &sact, NULL) != 0)
perror("sigaction() error");
else {
sigemptyset(&new_set);
sigaddset(&new_set, SIGALRM);
if (sigprocmask(SIG_BLOCK, &new_set, &old_set) != 0)
perror("1st sigprocmask() error");
else {
time(&start);
printf("SIGALRM signals blocked at %s", ctime(&start));
alarm(1);
do {
time(&finish);
diff = difftime(finish, start);
} while (diff < 10);
if (sigprocmask(SIG_SETMASK, &old_set, NULL) != 0)
perror("2nd sigprocmask() error");
else
printf("SIGALRM signals unblocked at %s", ctime(&finish));
}
}
}
SIGALRM signals blocked at Fri Jun 16 12:24:19 2001
inside catcher
SIGALRM signals unblocked at Fri Jun 16 12:24:29 2001