Signal
Syntax
void (*signal(int sig, void (*func)(int)))(int);
Description
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 .
Arguments
- sig
- The signal value to which a handling function is set. The following macro constant expressions identify standard signal values:
macro signal SIGABRT (0x06) (Signal Abort) Abnormal termination, such as is initiated by the abort function. SIGFPE (0x08) (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 (0x04) (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 (0x02) (Signal Interrupt) Interactive attention signal. Generally generated by the application user. SIGSEGV (0x0B) (Signal Segmentation Violation) Invalid access to storage: When a program tries to read or write outside the memory it has allocated. SIGTERM (0x0F) (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.
- func
- A pointer to a function. This may either be a function defined by the programmer or one of the following predefined functions:
macro signal 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 (with C linkage):
void handler_function (int parameter);
Return value
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.
Example
The following code:
/* signal example */
#include <stdio.h> /* printf */
#include <signal.h> /* signal, raise, sig_atomic_t */
sig_atomic_t signaled = 0;
void my_handler (int param)
{
signaled = 1;
}
int main ()
{
void (*prev_handler)(int);
prev_handler = signal (SIGINT, my_handler);
/* ... */
raise(SIGINT);
/* ... */
printf ("signaled is %d.\n",signaled);
return 0;
}
will output
signaled is 1.