YouTip LogoYouTip

C Function Sigfillset

# C Library Function - sigfillset() The `sigfillset()` function is a standard C library function used to initialize a signal set so that it contains all system-defined signals. This function is defined in the `` header file and is commonly used in POSIX-compliant systems for signal handling and management. --- ## Description In Unix-like operating systems, signals are asynchronous notifications sent to a process to inform it of an event. A signal set (`sigset_t`) is a data type used to represent a group of signals. The `sigfillset()` function initializes the signal set pointed to by the `set` argument, filling it with every signal supported by the system. This is particularly useful when you want to block or manipulate all signals by default, and then selectively unblock or remove specific ones. --- ## Syntax ```c #include int sigfillset(sigset_t *set); ``` ### Parameters * **`sigset_t *set`**: A pointer to a `sigset_t` variable that will be initialized to include all defined signals. ### Return Value * **`0`**: On success. * **`-1`**: On failure. The global variable `errno` is set to indicate the error. --- ## Code Example The following example demonstrates how to use `sigfillset()` to initialize a signal set with all signals, and then verify whether a specific signal (such as `SIGINT`) is present in the set using `sigismember()`. ```c #include #include int main() { sigset_t set; // Initialize the signal set to include all signals if (sigfillset(&set) == -1) { perror("sigfillset"); return 1; } // Check if the signal set contains SIGINT (Ctrl+C) if (sigismember(&set, SIGINT)) { printf("SIGINT is in the set\n"); } else { printf("SIGINT is not in the set\n"); } return 0; } ``` ### Output When you compile and run the program, the output will be: ```text SIGINT is in the set ``` ### Code Explanation 1. **`sigset_t set;`**: Declares a signal set variable. 2. **`sigfillset(&set)`**: Fills the signal set so that it contains all possible signals. If the operation fails, it prints an error message using `perror` and exits. 3. **`sigismember(&set, SIGINT)`**: Tests whether `SIGINT` is a member of the initialized set. Since `sigfillset` includes all signals, this check returns true (non-zero). --- ## Important Considerations 1. **Uninitialized Signal Sets**: A `sigset_t` variable must always be initialized using either `sigfillset()` or `sigemptyset()` before being passed to other signal functions (such as `sigaddset()`, `sigdelset()`, or `sigprocmask()`). Passing an uninitialized signal set results in undefined behavior. 2. **Non-maskable Signals**: Although `sigfillset()` includes all signals in the set, certain critical signals like `SIGKILL` and `SIGSTOP` cannot be blocked or caught by a process. If you apply a filled signal set to `sigprocmask()` to block signals, the system will silently ignore attempts to block `SIGKILL` and `SIGSTOP`. 3. **Portability**: The `sigfillset()` function is part of the POSIX.1 standard. It is highly portable across Unix, Linux, and macOS environments, but is not available in standard Windows environments (unless using compatibility layers like Cygwin or WSL).
← C Function SigdelsetC Function Sigemptyset β†’