void (*signal(int sig, void (*func)(int)))(int);
Set function to handle signal
Specifies a way to handle the signals with the signal number specified by sig.
Parameter func specifies one of the three ways in which a signal can be handled by a program:
- Default handling (SIG_DFL): The signal is handled by the default action for that particular signal.
- Ignore signal (SIG_IGN): The signal is ignored and the code execution will continue even if not meaningful.
- Function handler: A specific function is defined to handle the signal.
Either SIG_DFL or SIG_IGN is set as the default signal handling behavior at program startup for each of the supported signals .
When a function handler is specified, the behavior is implementation-dependent (non-portable).
- The signal value to which a handling function is set. The following macro constant expressions identify standard signal values:
|SIGABRT||(Signal Abort) Abnormal termination, such as is initiated by the abort function.|
|SIGFPE||(Signal Floating-Point Exception) Erroneous arithmetic operation, such as zero divide or an operation resulting in overflow (not necessarily with a floating-point operation).|
|SIGILL||(Signal Illegal Instruction) Invalid function image, such as an illegal instruction. This is generally due to a corruption in the code or to an attempt to execute data.|
|SIGINT||(Signal Interrupt) Interactive attention signal. Generally generated by the application user.|
|SIGSEGV||(Signal Segmentation Violation) Invalid access to storage: When a program tries to read or write outside the memory it has allocated.|
|SIGTERM||(Signal Terminate) Termination request sent to program.|
Each library implementation may provide additional signal value macro constants that can be used with this function.
Notice that not all running environments are required to generate automatic signals, not even in the specific cases described above, although all running environments must deliver signals generated by a explicit call to the raise function.
- A pointer to a function. This may either be a function defined by the programmer or one of the following predefined functions:
|SIG_DFL||Default handling: The signal is handled by the default action for that particular signal.|
|SIG_IGN||Ignore Signal: The signal is ignored.|
If a function, it should follow the following prototype:
void handler_function (int parameter);
The return type is the same as the type of parameter func.
If the request is successful, the function returns a pointer to the particular handler function which was in charge of handling this signal before the call, if any. Or either SIG_DFL or SIG_IGN if before the call the signal was being handled by the default handler or was being ignored, respectivelly.
If the function was not successful in registering the new signal handling procedure, it returns SIG_ERR and errno may be set to a positive value.
/* signal example */
#include <stdio.h> /* printf, tmpnam, L_tmpnam, remove */
#include <stdlib.h> /* exit */
#include <signal.h> /* signal */
char tmpfilename [L_tmpnam];
void my_terminate (int param)
printf ("Terminating program...\n");
int main ()
prev_fn = signal (SIGTERM,my_terminate);
if (prev_fn==SIG_IGN) signal (SIGTERM,SIG_IGN);
/* code here */
This program defines a my_terminate function that removes a previously created temporal file. This function is associated to the SIGTERM signal, except if this signal is currently being ignored, in which case it is reset to SIG_IGN so that the behavior does not change. [Note: this program is not portable]
Undefined: calling this function in a multi-threaded program causes undefined behavior.
No-throw guarantee: this function never throws exceptions.
- Generates a signal (function