Node:Basic Signal Handling, Next:Advanced Signal Handling, Up:Signal Actions
The signal
function provides a simple interface for establishing
an action for a particular signal. The function and associated macros
are declared in the header file signal.h
.
sighandler_t | Data Type |
This is the type of signal handler functions. Signal handlers take one
integer argument specifying the signal number, and have return type
void . So, you should define handler functions like this:
void handler (int
The name |
sighandler_t signal (int signum, sighandler_t action) | Function |
The signal function establishes action as the action for
the signal signum.
The first argument, signum, identifies the signal whose behavior you want to control, and should be a signal number. The proper way to specify a signal number is with one of the symbolic signal names (see Standard Signals)--don't use an explicit number, because the numerical code for a given kind of signal may vary from operating system to operating system. The second argument, action, specifies the action to use for the signal signum. This can be one of the following:
If you set the action for a signal to The If
|
Compatibility Note: A problem encountered when working with the
signal
function is that it has different semantics on BSD and
SVID systems. The difference is that on SVID systems the signal handler
is deinstalled after signal delivery. On BSD systems the
handler must be explicitly deinstalled. In the GNU C Library we use the
BSD version by default. To use the SVID version you can either use the
function sysv_signal
(see below) or use the _XOPEN_SOURCE
feature select macro (see Feature Test Macros). In general, use of these
functions should be avoided because of compatibility problems. It
is better to use sigaction
if it is available since the results
are much more reliable.
Here is a simple example of setting up a handler to delete temporary
files when certain fatal signals happen:
#include <signal.h> void termination_handler (int signum) { struct temp_file *p; for (p = temp_file_list; p; p = p->next) unlink (p->name); } int main (void) { ... if (signal (SIGINT, termination_handler) == SIG_IGN) signal (SIGINT, SIG_IGN); if (signal (SIGHUP, termination_handler) == SIG_IGN) signal (SIGHUP, SIG_IGN); if (signal (SIGTERM, termination_handler) == SIG_IGN) signal (SIGTERM, SIG_IGN); ... }
Note that if a given signal was previously set to be ignored, this code avoids altering that setting. This is because non-job-control shells often ignore certain signals when starting children, and it is important for the children to respect this.
We do not handle SIGQUIT
or the program error signals in this
example because these are designed to provide information for debugging
(a core dump), and the temporary files may give useful information.
sighandler_t sysv_signal (int signum, sighandler_t action) | Function |
The sysv_signal implements the behavior of the standard
signal function as found on SVID systems. The difference to BSD
systems is that the handler is deinstalled after a delivery of a signal.
Compatibility Note: As said above for |
sighandler_t ssignal (int signum, sighandler_t action) | Function |
The ssignal function does the same thing as signal ; it is
provided only for compatibility with SVID.
|
sighandler_t SIG_ERR | Macro |
The value of this macro is used as the return value from signal
to indicate an error.
|