signal -- Handle Interrupt Signals

Format

#include <signal.h>
void ( *signal(int sig, void (*sig_handler)(int)) )(int);

Language Level: ANSI, XPG4, Extension
signal function assigns the signal handler sig_handler to handle the interrupt signal sig. Signals can be reported as a result of a machine interrupt (for example, division by zero) or by an explicit request to report a signal by using the raise function.

The sig argument must be one of the signal constants defined in <signal.h>:

Value Meaning
SIGABRT Abnormal termination signal sent by abort. Default action: end the program.
SIGBREAK Ctrl-Break signal. Default action: end the program.
SIGFPE Floating-point exceptions that are not masked, such as overflow, division by zero, and invalid operation. Default action: end the program and provide an error message. If machine-state dumps are enabled (with the /Tx compiler option), a dump is also provided.
SIGILL Instruction not allowed. Default action: end the program and provide an error message. If machine-state dumps are enabled (with the /Tx compiler option), a dump is also provided.
SIGINT Ctrl-C signal. Default action: end the program.
SIGSEGV Access to memory not valid. Default action: end the program and provide an error message. If machine-state dumps are enabled (with the /Tx compiler option), a dump is also provided.
SIGTERM Program termination signal sent by the user. Default action: end the program.
SIGUSR1 Defined by the user. Default action: ignore the signal.
SIGUSR2 Defined by the user. Default action: ignore the signal.

For sig_handler, you must specify either the SIG_DFL or SIG_IGN constant (also defined in <signal.h>), or the address of a function that takes an integer argument (the signal).

The action taken when the interrupt signal is received depends on the value of sig_handler:

Value Meaning
SIG_DFL Perform the default action. This is the initial setting for all signals. The default actions are described in the list of signals above. All files controlled by the process are closed, but buffers are not flushed.
SIG_IGN Ignore the interrupt signal.
sig_handler Call the function sig_handler, which you provide, to handle the signal specified.

Your signal handler function (sig_handler) must take two integer arguments. The first argument is always the signal identifier. The second argument is 0, unless the signal is SIG_FPE. For SIG_FPE signals, the second argument passed is a floating-point error signal as defined in <float.h>. If your sig_handler returns, the calling process resumes running immediately following the point at which it received the interrupt signal.

After a signal is reported and the sig_handler is called, signal handling for that signal is reset to the default. Depending on the purpose of the signal handler, you may want to call signal inside sig_handler to reestablish sig_handler as the signal handler. You can also reset the default handling at any time by calling signal and specifying SIG_DFL.

In both OS/2 and Windows, synchronous signals and signal handlers are not shared between threads. If you do not establish a handler for a specific signal within a thread, the default signal handling is used regardless of what handlers you may have established in other concurrent threads. However, asynchronous signals are global for the process.

Note: If an exception occurs in a math or critical library function, it is handled by the exception handler. Your sig_handler will not be called.

Return Value
All calls to signal return the address of the previous handler for the re-assigned signal.

A return value of SIG_ERR (defined in <signal.h>) indicates an error, and errno is set to EINVAL. The possible causes of the error are an incorrect sig value or an undefined value for sig_handler.

Example



Handling Signals and Exceptions


atexit -- Record Program Termination Function
exit -- End Program
_exit -- End Program
_fpreset -- Reset Floating-Point Unit
raise -- Send Signal
_spawnl - _spawnvpe -- Start and Run Child Processes
<signal.h>