C Library Function - sigpending()
sigpending The function is a function in the C standard library, used to get the pending (unhandled) signal set of the current process. A pending signal is one that has been blocked but not yet handled. This function is defined in header.
Syntax
int sigpending(sigset_t *set);
Parameters
sigset_t *set: points to asigset_tvariable of type , which will be filled with the pending signal set of the current process.
Return Value
- Returns 0 on success.
- Returns -1 on failure and sets
errnoto indicate the error type.
Example
The following is an example of using sigpending Example program demonstrating the function getting the pending signal set of the current process. The program blocks SIGINT signal, then sends the signal, and uses sigpending checks whether the signal is in the pending signal set.
Example
#include
#include
#include
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 the signal set and block the SIGINT signal
sigemptyset
(
&
new_mask
)
;
sigaddset
(
&
new_mask
,
SIGINT
)
;
if
(
sigprocmask
(
SIG_BLOCK
,
&
new_mask
,
&
old_mask
)
==
1
)
{
perror
(
"sigprocmask"
)
;
return
1
;
}
// send the SIGINT signal
raise
(
SIGINT
)
;
// get the pending signal set
if
(
sigpending
(
&
pending
)
==
1
)
{
perror
(
"sigpending"
)
;
return
1
;
}
// check whether 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 blocks SIGINT signal, then sends the signal, and uses sigpending Checks SIGINT whether it is in the pending signal set. Finally, the program restores the original signal mask, and catches the SIGINT signal and executes the signal handler.
Parsing
sigset_t new_mask, old_mask, pending;: defines three signal sets, used to control the signal mask and store the pending signal set.- Use
sigprocmaskThe function blocksSIGINTsignal and saves the original signal mask. - Use
raiseThe function sendsSIGINTsignal after 5 seconds. - Use
sigpendingThe function gets the pending signal set and checksSIGINTwhether it is in the pending signal set. - Use
sigprocmaskThe function restores the original signal mask.
Notes
sigpendingThe function gets the pending signal set of the current process, including all signals that have been sent but not yet processed.- A signal handler should be kept as short as possible, because other signals are blocked while handling a signal. A long-running signal handler may affect the real-time performance of the program.
C Standard Library -