function
<csignal>

signal

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 .

Parameters

sig
The signal value to which a handling function is set. The following macro constant expressions identify standard signal values:

macrosignal
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.
func
A pointer to a function. This may either be a function defined by the programmer or one of the following predefined functions:

SIG_DFLDefault handling: The signal is handled by the default action for that particular signal.
SIG_IGNIgnore Signal: The signal is ignored.

If a function, it should follow the following prototype (with C linkage):
1
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

/* 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;
}
signaled is 1.

Data races

Undefined: calling this function in a multi-threaded program causes undefined behavior.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

See also