C Library Functions -sigpending()
sigpendingA function in the C standard library used to obtain the set of pending signals for the current process. Pending signals are signals that have been blocked but not yet handled. This function is defined in<signal.h>In the header file.
Syntax
int sigpending(sigset_t *set);
Parameter
sigset_t *set: points to asigset_tA variable of type, which will be filled with the set of pending signals for the current process.
Return Value
- Returns 0 on success.
- Returns -1 on failure, and sets
errnoto indicate the type of error.
Example
The following is an example usingsigpendingAn example program of the function retrieving the pending signal set of the current process. The program blocksSIGINTsignal, then sends that signal, and usessigpendingChecks whether that signal is in the pending signal set.
Example
#include <signal.h>
#include <unistd.h>
void handle_sigint(int sig) {
printf("Caught signal %d\n", sig);
}
int main() {
sigset_t new_mask, old_mask, pending;
// Set the handler for SIGINT
signal(SIGINT, handle_sigint);
// Initialize signal set and block SIGINT signal
sigemptyset(&new_mask);
sigaddset(&new_mask, SIGINT);
if (sigprocmask(SIG_BLOCK, &new_mask, &old_mask) == -1) {
perror("sigprocmask");
return 1;
}
// Send SIGINT signal
raise(SIGINT);
// Get the pending signal set
if (sigpending(&pending) == -1) {
perror("sigpending");
return 1;
}
// Check if SIGINT is in the pending signal set
if (sigismember(&pending, SIGINT)) {
printf("SIGINT is pending\n");
} else {
printf("SIGINT is not pending\n");
}
// Restore the original signal mask
if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) {
perror("sigprocmask");
return 1;
}
return 0;
}
After compiling and running the program, the output will be:
SIGINT is pending Caught signal 2
The program first blocksSIGINTsignal, then sends that signal, and usessigpendingChecksSIGINTWhether it is in the pending signal set. Finally, the program restores the original signal mask and catchesSIGINTsignal, executes the signal handler.
Parsing
sigset_t new_mask, old_mask, pending;: defines three signal sets, used to control signal masking and store the pending signal set.- Usage
sigprocmaskFunction blockingSIGINTSignal, and saves the original signal mask. - Usage
raiseFunction sendingSIGINTsignal. - Usage
sigpendingThe function retrieves the pending signal set and checksSIGINTwhether it is in the pending signal set. - Usage
sigprocmaskThe function restores the original signal mask.
Notes
sigpendingThe function retrieves the pending signal set of the current process, including all signals that have been sent but not yet handled.- Signal handler functions should be as short as possible, because other signals are blocked while processing a signal. Long-running signal handler functions may affect the real-time performance of the program.