C Library Function - sigismember()
Description
sigismember The function is a function in the C standard library, used to check whether a specific signal is in a signal set. This function is defined in header.
Syntax
int sigismember(const sigset_t *set, int signo);
Parameters
const sigset_t *set: points to asigset_tsignal set variable of type .int signo: the signal number to check.
Return Value
- If the signal is in the signal set, return 1.
- If the signal is not in the signal set, return 0.
- If an error occurs, return -1 and set
errnoto indicate the error type.
Example
The following is an example of using sigismember Example program demonstrating the function checking whether a signal set contains a specific signal. The program initializes an empty signal set, adds SIGINT signal to the signal set, and checks whether the signal set contains SIGINT signal after 5 seconds.
Example
#include
#include
int
main
(
)
{
sigset_t set
;
// initialize the signal set to empty
if
(
sigemptyset
(
&
set
)
==
1
)
{
perror
(
"sigemptyset"
)
;
return
1
;
}
// add SIGINT to the signal set
if
(
sigaddset
(
&
set
,
SIGINT
)
==
1
)
{
perror
(
"sigaddset"
)
;
return
1
;
}
// check whether the signal set contains SIGINT
if
(
sigismember
(
&
set
,
SIGINT
)
)
{
printf
(
"SIGINT is in the set\n"
)
;
}
else
{
printf
(
"SIGINT is not in the set\n"
)
;
}
// check whether the signal set contains SIGTERM
if
(
sigismember
(
&
set
,
SIGTERM
)
)
{
printf
(
"SIGTERM is in the set\n"
)
;
}
else
{
printf
(
"SIGTERM is not in the set\n"
)
;
}
return
0
;
}
Running the above example program may produce the following output:
SIGINT is in the set
SIGTERM is not in the set
The program initializes an empty signal set, adds SIGINT signal to the signal set, and checks whether the signal set contains SIGINT and SIGTERM signal, and the result shows that SIGINT signal is in the signal set, while SIGTERM signal is not in the signal set.
Explanation
sigset_t set;: defines a signal set variable.- Use
sigemptysetThe function initializes the signal set to empty. - Use
sigaddsetfunction to installSIGINTadds the signal to the signal set. - Use
sigismemberThe function checks whether the signal set containsSIGINTandSIGTERMsignal after 5 seconds. - The output shows that
SIGINTsignal is in the signal set, whileSIGTERMsignal is not in the signal set.
Notes
sigismemberThe function is used to check whether a specific signal is in a signal set.- If the signal number is invalid,
sigismemberthe function will return -1 and seterrnoto indicate the error type.
C Standard Library -