Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
SIG_ |
Maximum value of sig_ |
H | M | <stdint.h> |
C99 | C11 | |||||
SIG_ |
Minimum value of sig_ |
H | M | <stdint.h> |
C99 | C11 |
Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
raise() |
Generate signal | (·) | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIG_ |
Default signal handler | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIG_ |
Signal-handler change error | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIG_ |
Signal should be ignored | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIGABRT |
Execution aborted | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIGFPE |
Floating-point exception | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIGILL |
Illegal instruction | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIGINT |
User interruption | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIGSEGV |
Illegal memory access | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
SIGTERM |
Environment requests termination | M | <signal.h> |
C89 | C90 | C95 | C99 | C11 | |||
sig_ |
Type for use by signal handlers | H | T | <signal.h> |
C89 | C90 | C95 | C99 | C11 | ||
signal() |
Set signal handler | (·) | <signal.h> |
C89 | C90 | C95 | C99 | C11 |
Signals are indicators of exceptional
circumstances during a program's execution. Each signal has a
type indicating its cause. Signal types are identified by
names beginning with SIG
and an
upper-case letter. Six are specified in the standard, but
most implementations will define many more:
Macros with names matching ^SIG[A-Z]
might be added to <signal.h>
.
SIGINT
indicates that the user attempted to stop a program, usually
through its terminal. SIGTERM
indicates that some part of the system in which the program
is running has asked the program to terminate.
SIGABRT
is raised by the abort
function, and by assert
.
SIGILL
indicates that an illegal machine-code instruction was
encountered. This typically happens when an invalid function
pointer is invoked.
SIGFPE
indicates that a floating-point exception occurred, for
example, a division by zero.
SIGSEGV
indicates some fault relating to memory access. It commonly
happens when mistakenly dereferencing a null
pointer.
#include <signal.h>
int raise(int s);
You can cause a signal to occur artificially by calling
the raise
function with the signal type. If it returns at all, it
returns zero on success and non-zero otherwise.
Many signals will cause a program to terminate by default. A C program can change the behaviour by specifying a signal handler, a function to be called when the signal occurs.
#include <signal.h>
void (*signal(int s, void (*handler)(int)))(int);
The function signal
sets a new signal handler for signal type s
. The second argument of signal
is a pointer to the new signal handler. The return value is a
pointer to the previous handler, or SIG_
if signal
could not change the current handler for the specified signal
type.
Two special values can be given instead of a function
pointer. SIG_
means that the default behaviour for the signal type is to be
followed. SIG_
means that the signal should be ignored.
Macros with names matching SIG_[A-Z]
might be added to <signal.h>
.
A signal handler should have the following form:
void handler(int s);
When a signal of type s
occurs,
handler(s)
is invoked.
Very little is specified about the behaviour that a signal handler can do. To be completely portable, it should limit itself to:
- calling
signal(s, . . .)
, which it might have to do becausesignal(s, SIG_
is sometimes called just before invoking the handler (in fact C99 guarantees this[ Check!]),DFL ) - assigning to variables with static
storage and of type
volatile sig_
, andatomic_t - calling
_Exit
orquick_
, or executing aexit return
statement.
sig_
is an integer
type with the range SIG_
to SIG_
,
which is at least [−127, +127]