C Standard Library Signal H
## Introduction
`` is a header file in the C standard library used for handling signals.
The **signal.h** header file defines a variable type **sig_atomic_t**, two function calls, and some macros to handle the different signals reported during program execution.
Signals are a form of asynchronous notification mechanism, allowing a process to execute predefined handler functions when specific events occur.
Here is a simple example program demonstrating how to use the signal function to catch the SIGINT signal (typically generated by Ctrl+C).
## Example
#include
#include
#include
// Global variable indicating whether the program should exit
volatile sig_atomic_t stop =0;
void handle_sigint(int sig){
printf("Caught signal %dn", sig);
stop =1;// Set exit flag
}
int main(){
// Set the handler for SIGINT signal to the handle_sigint function
signal(SIGINT, handle_sigint);
while(!stop){// Check if should exit
printf("Running...n");
sleep(1);
}
printf("Exiting...n");
return 0;
}
In the above code, when the program is running, if the user presses Ctrl+C, the SIGINT signal is caught and the handle_sigint function is called, printing the signal number.
Compiling and executing the above code yields the following output:
Running...Running...Running...^CCaught signal 2Exiting...
Code Analysis:
* `volatile sig_atomic_t stop = 0;`: Defines a global variable `stop` used to indicate whether the program should exit. The `volatile` keyword ensures the compiler does not optimize away accesses to this variable, as it may be modified within the signal handler. The `sig_atomic_t` type guarantees that access to this variable is atomic.
* Inside the `handle_sigint` signal handler function, `stop` is set to 1, indicating the program should exit.
* In the main loop, the value of the `stop` variable is checked. If it is set to 1, the loop is broken out of, ending the program.
## Library Variables
Below is the variable type defined in the signal.h header file:
| S.No. | Variable & Description |
| --- | --- |
| 1 | **sig_atomic_t** This is an **int** type, used as a variable within signal handlers. It is an integer type of an object that can be accessed as an atomic entity, even when asynchronous signals are present. |
| 2 | **sigset_t** A data type used to represent a signal set. |
## Library Macros
<
In the C standard library, macros with the `SIG_` prefix are used together with the `signal` function to define and handle signal behavior. Below are common `SIG_` macros and their purposes:
| S.No. | Macro & Description |
| --- | --- |
| 1 | **SIG_DFL** Represents the default handler for a signal. Using this macro restores a signal to its default behavior. |
| 2 | **SIG_ERR** Represents the return value for signal handling function errors. |
| 3 | **SIG_IGN** Ignore the signal. |
Signal Types:
| Constant | Description |
| --- | --- |
| `SIGABRT` | Signal generated by the `abort` function, indicating abnormal termination |
| `SIGALRM` | Signal generated when a timer set by the `alarm` function expires |
| `SIGBUS` | Illegal memory access (e.g., accessing misaligned memory addresses) |
| `SIGCHLD` | Child process status changed (exited or stopped) |
| `SIGCONT` | Continue a paused process |
| `SIGFPE` | Arithmetic error (e.g., division by zero, floating-point exception) |
| `SIGHUP` | Hangup signal (often used to detect terminal disconnection) |
| `SIGILL` | Illegal instruction |
| `SIGINT` | Interrupt signal (typically generated by Ctrl+C) |
| `SIGKILL` | Immediate termination signal (cannot be caught or ignored) |
| `SIGPIPE` | Writing to a pipe with no reading process |
| `SIGQUIT` | Terminal quit signal (typically generated by Ctrl+), generates a core dump |
| `SIGSEGV` | Segmentation fault (illegal memory access) |
| `SIGSTOP` | Stop process execution (cannot be caught or ignored) |
| `SIGTERM` | Termination signal |
| `SIGTSTP` | Suspend process (typically generated by Ctrl+Z) |
| `SIGTTIN` | Signal generated when a background process reads from the terminal |
| `SIGTTOU` | Signal generated when a background process writes to the terminal |
| `SIGUSR1` | User-defined signal 1 |
| `SIGUSR2` | User-defined signal 2 |
| `SIGPOLL` | I/O event (replaces SIGIO) |
| `SIGPROF` | Timer expiration signal (profiling timer set by `setitimer`) |
| `SIGSYS` | Illegal system call |
| `SIGTRAP` | Breakpoint or trap instruction |
| `SIGURG` | Socket urgent condition signal |
| `SIGVTALRM` | Virtual clock timer expiration signal |
| `SIGXCPU` | Exceeded CPU time limit |
| `SIGXFSZ` | Exceeded file size limit |
## Library Functions
Below are the functions defined in the signal.h header file:
| Function | Description |
| --- | --- |
| `void (*signal(int sig, void (*func)(int)))(int);` | Sets a signal handler. |
| `int raise(int sig);` | Sends a signal to the current process. |
| `int kill(pid_t pid, int sig);` | Sends a signal to a specified process. |
| `int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);` | Examines or changes the blocked signal set. |
| `int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact);` | Examines or changes the signal handler. |
| `int sigsuspend(const sigset_t *mask);` | Temporarily replaces the current signal mask and suspends the process until a signal is caught. |
| `int sigpending(sigset_t *set);` | Examines the pending signal set. |
| `int sigemptyset(sigset_t *set);` | Initializes the signal set to empty. |
| `int sigfillset(sigset_t *set);` | Initializes the signal set to contain all signals. |
| `int sigaddset(sigset_t *set, int signum);` | Adds the specified signal to the signal set. |
| `int sigdelset(sigset_t *set, int signum);` | Removes the specified signal from the signal set. |
| `int sigismember(const sigset_t *set, int signum);` | Checks if the specified signal is in the signal set. |
| `void abort(void);` | Generates the `SIGABRT` signal, causing abnormal termination of the process. |
| `unsigned int alarm(unsigned int seconds);` | Sends the `SIGALRM` signal to the calling process after the specified number of seconds. |
| `int pause(void);` | Suspends the process until a signal is caught. |
| `void psignal(int sig, const char *s);` | Prints signal description information. |
| `char *strsignal(int sig);` | Returns a string describing the signal. |
| `int sigwait(const sigset_t *set, int *sig);` | Blocks and waits for a signal to handle. |
YouTip