C library functions -sigprocmask()
C Standard Library - <signal.h>
Description
In the C language,sigprocmaskThe function is used to check or modify the signal mask of the current process. The signal mask determines which signals are blocked during masking, i.e., temporarily not processed.
Syntax
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
Parameter
int how: operation flags that determine how the signal mask is modified:SIG_BLOCK: takesetThe signals in the pointed-to signal set are added to the current signal mask.SIG_UNBLOCK: remove from the current signal masksetthe signals in the signal set pointed to.SIG_SETMASK: usesetThe pointed-to signal set replaces the current signal mask.
const sigset_t *set: a pointer to the new signal set to be used for modification.sigset_t *oldset: if it is notNULL, then store the previous signal mask.
Return Value
- Returns 0 on success.
- Returns -1 on failure, and sets
errnoto indicate the type of error.
Example
The following is a simple example program demonstrating how to usesigprocmaskfunction to blockSIGINTa signal, then unblock it and check the previous signal mask:
Example
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main() {
sigset_t new_mask, old_mask;
// Initialize a new signal set, containing only SIGINT
sigemptyset(&new_mask);
sigaddset(&new_mask, SIGINT);
// Set the new signal mask to block the SIGINT signal
if (sigprocmask(SIG_BLOCK, &new_mask, &old_mask) == -1) {
perror("sigprocmask");
return 1;
}
printf("SIGINT signal is blocked. Sleeping for 5 seconds...\n");
sleep(5);
// Unblock SIGINT
if (sigprocmask(SIG_UNBLOCK, &new_mask, NULL) == -1) {
perror("sigprocmask");
return 1;
}
printf("SIGINT signal is unblocked.\n");
return 0;
}
#include <signal.h>
#include <unistd.h>
int main() {
sigset_t new_mask, old_mask;
// Initialize a new signal set, containing only SIGINT
sigemptyset(&new_mask);
sigaddset(&new_mask, SIGINT);
// Set the new signal mask to block the SIGINT signal
if (sigprocmask(SIG_BLOCK, &new_mask, &old_mask) == -1) {
perror("sigprocmask");
return 1;
}
printf("SIGINT signal is blocked. Sleeping for 5 seconds...\n");
sleep(5);
// Unblock SIGINT
if (sigprocmask(SIG_UNBLOCK, &new_mask, NULL) == -1) {
perror("sigprocmask");
return 1;
}
printf("SIGINT signal is unblocked.\n");
return 0;
}
Output result
The possible output of running the above example program is as follows:
SIGINT signal is blocked. Sleeping for 5 seconds...
During this period, the SIGINT signal is blocked, so the program waits for 5 seconds.
SIGINT signal is unblocked.
Then, the program unblocksSIGINTUnblock the signal and restore normal processing of the signal:
Notes
- Usage
sigprocmaskWhen using the function, be careful to ensure that critical signals are not blocked for a long time (such asSIGKILLorSIGSTOP), these signals cannot be blocked or caught. sigprocmaskThe function can be used to control signal handling in critical sections, such as avoiding the processing of interrupt signals within a critical section.