C library functions -sigaddset()
C Standard Library - <signal.h>
Description
sigaddsetThe function is a function in the C standard library, used to add a specific signal to a signal set. This function is defined in<signal.h>In the header file.
Syntax
int sigaddset(sigset_t *set, int signo);
Parameter
sigset_t *set: points to asigset_tA signal set variable of type.int signo: the signal number to be added to the signal set.
Return Value
- Returns 0 on success.
- On failure, returns -1 and sets
errnoto indicate the error type.
Example
The following is an example usingsigaddsetAn example program of the function adding a specific signal to a signal set. The program initializes an empty signal set andSIGINTadds the signal to the signal set, and checks whether the signal set containsSIGINTsignal.
Example
#include <stdio.h>
#include <signal.h>
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");
}
return 0;
}
#include <signal.h>
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");
}
return 0;
}
The possible output of running the above example program is as follows:
SIGINT is in the set
The program initializes an empty signal set andSIGINTadds the signal to the signal set, and checks whether the signal set containsSIGINTsignal, the results show thatSIGINTthe signal is in the signal set.
Explanation
sigset_t set;: Defines a signal set variable.- Usage
sigemptysetThe function initializes the signal set to empty. - Usage
sigaddsetThe function willSIGINTAdds the signal to the signal set. - Usage
sigismemberThe function checks whether the signal set containsSIGINTsignal. - The output shows
SIGINTthe signal in the initialized signal set.
Notes
sigaddsetThe function is used to add a specific signal to a signal set.- If the signal number is invalid,
sigaddsetthe function will return -1 and seterrnoto indicate the error type.