signal.h is the header file defined in the standard C library to indicate how the program processes signals during its execution. A signal can be either synchronous using the raise() call, or asynchronous.
| Standard library C programming language |
|
Each implementation determines which signal generates and determines their generation.
This part of the library is used to intercept signals - assigning a handler to a specific signal.
A signal handler can only call the following functions: _exit() , _Exit() , abort() , raise() (only if the handler is not called by the abort or raise functions). Calling other library functions leads to undefined behavior, although such calls can be resolved by individual implementations, for example, posix has a list of async-signal-safe functions.
Content
Data Types
The standard declares the sig_atomic_t data sig_atomic_t , access to which is atomic , even in the presence of asynchronous interrupts . [one]
Macros
The standard declares macros SIG_DFL , SIG_ERR , SIG_IGN , which are used as arguments and return value for the signal() function. [1] The SIG_DFL macro is used to set the default behavior for the selected signal, SIG_IGN is used to ignore the signal, and SIG_ERR is used as a return value indicating an error.
In addition, the standard defines the following types of signals: [1]
| Constant | Value |
|---|---|
| Sigint | Receiving an Interactive Signal |
| Sigill | Invalid instruction |
| SIGABRT | Abnormal program termination that may be caused by abort() |
| Sigfpe | Erroneous arithmetic operation, such as division by zero or overflow |
| Sigsegv | Invalid access to an object in memory |
| Sigterm | Termination Request |
and allows others that are platform dependent. When the program starts, part of the signals may be ignored; for the other part, the default behavior is determined.
Functions
The standard declares the following functions:
#include <signal.h>
void ( * signal ( int sig , void ( * func ) ( int ))) ( int );
int raise ( int sig );
The signal function sets the func handler for the sig signal. func can take SIG_IGN and SIG_DFL as arguments. If successful, the function returns the old signal handler; otherwise, SIG_ERR .
If the handler returns control when the signal type is SIGILL , SIGFPE , SIGSEGV or others associated with runtime errors, the program behavior is undefined. In addition, if the handler was not called using abort or raise, it can only change variables of type volatile sig_atomic_t .
The raise function generates a sig signal. [one]
Methods
int raise(int sig). Artificially triggers a signal.psignal (int sig, const char *s), prints to stderr a string containing the signal number. Used in 4.3BSD , Solaris, and Linux , but not specified in POSIX , the standard C library and SUS . On the same systems, string.h contains the non-standardstrsignal (int sig), which works similarly to strerror .void* signal(int sig, void (*func)(int))designates the action taken when the program receives thesigsignal. If func is SIG_DFL, then the default processing for the specified signal occurs. If func is SIG_IGN, then the signal is ignored. In other cases, func points to the called function of the signal handler when the signal is received. The func function may end with a return operation or a call to the self-completion, exit, or transition functions.
Constant Members
| Constant | Value | Standards | |
|---|---|---|---|
| Sighup | Hang up | Posix | |
| Sigint | Interrupt | Ansi | |
| SIGQUIT | Output | Posix | |
| Sigill | Invalid instruction | Ansi | |
| SIGABRT | Self stop | Ansi | |
| SIGTRAP | Event interception | Posix | |
| Sigiot | I / O interception | 4.2 BSD | |
| Sigemt | Emulation capture | 4.2 BSD | |
| Sigfpe | Floating point exception | Ansi | |
| Sigkill | Captured Termination Signal | Posix | |
| Sigbus | Bus error | 4.2 BSD | |
| Sigsegv | Segmentation disorder | Ansi | |
| Sigs | Invalid argument to system call | 4.2 BSD | |
| Sigpipe | Channel violation | Posix | |
| SIGALRM | Expiration time | Posix | |
| Sigterm | Completion | Ansi | |
| SIGUSR1 | Custom Signal 1 | Posix | |
| SIGUSR2 | Custom Signal 2 | Posix | |
| Sigchld | Change the status of a child process | Posix | |
| Sigcld | Similar to SIGCHLD | System v | |
| Sigpwr | Restart after a power problem | System v | |
| SIGXCPU | CPU time limit | Posix |
Notes
- ↑ 1 2 3 4 ISO / IEC 9899: TC3 7.14
Links
signal.h- Basic Definitions, The Single UNIX® Specification , Issue 7 by The Open Group- Dinkumware Guide on signal.h
- XGC Guide to signal.h