Namespaces
Variants
Views
Actions

signal

From cppreference.com
< c‎ | program
Revision as of 07:03, 19 April 2014 by D41D8CD98F (Talk | contribs)

Defined in header <signal.h>
void (*signal( int sig, void (*handler) (int))) (int);

Sets the error handler for signal sig. The signal handler can be set so that default handling will occur, signal is ignored, or an user-defined function is called.

When signal handler is set to a function and a signal occurs, it is implementation defined whether signal(sig, SIG_DFL) will be executed immediately before the start of signal handler. Also, the implementation can prevent some implementation-defined set af signals from occurring while the signal handler runs.

Contents

Parameters

sig - the signal to set the signal handler to. It can be an implementation-defined value or one of the following values:
defines signal types
(macro constant) [edit]


handler - the signal handler. This must be one of the following:
  • SIG_DFL macro. The signal handler is set to default signal handler.
  • SIG_IGN macro. The signal is ignored.
  • pointer to a function. The signature of the function must be equivalent to the following:
void fun(int sig);

Return value

Previous signal handler on success or SIG_ERR on failure (setting a signal handler can be disabled on some implementations).

Signal handler

The following limitations are imposed on the user-defined function that is installed as a signal handle

If the user defined function returns when handling SIGFPE, SIGILL or SIGSEGV, the behavior is undefined.

If the signal handler is called as a result of abort or raise, the behavior is undefined if the signal handler calls raise.

If the signal handler is called NOT as a result of abort or raise, the behavior is undefined if

  • the signal handler calls any function within the standard library, except abort, _Exit, quick_exit, or signal with the first argument not being the number of the signal currently handled.
  • the signal handler refers to any object with static or thread-local(since C11) storage duration that is not lock-free atomic(since C11) or volatile std::sig_atomic_t.

On entry to the signal handler, the state of the floating-point environment and the values of all objects is unspecified, except for

On return from a signal handler, the value of any object modified by the signal handler that is not volatile sig_atomic_t or lock-free atomic(since C11) is undefined.

The behavior is undefined if signal is used in a multithreaded program. It is not required to be thread-safe.

Notes

POSIX requires that signal is thread-safe, and specifies a list of async-signal-safe library functions that may be called from any signal handler.

Example

#include <stdio.h>
#include <signal.h>
 
/* A user-defined signal handler can process one or more signals. */
static void signal_handler(int signo)
{
    switch(signo)
    {
        case SIGABRT : puts("Process SIGABRT signal here."); break;
        case SIGFPE  : puts("Process SIGFPE  signal here."); break;
        case SIGILL  : puts("Process SIGILL  signal here."); break;
        case SIGINT  : puts("Process SIGINT  signal here."); break;
        case SIGSEGV : puts("Process SIGSEGV signal here."); break;
        case SIGTERM : puts("Process SIGTERM signal here."); break;
    }
 
    /* some cleanup here */
    /* exit(signo);      */
}
 
int main(void)
{
    /* Register a user-defined signal handler. */
    signal(SIGABRT, signal_handler);
    signal(SIGFPE,  signal_handler);
    signal(SIGILL,  signal_handler);
    signal(SIGINT,  signal_handler);
    signal(SIGSEGV, signal_handler);
    signal(SIGTERM, signal_handler);
 
    /* Raise a signal. */
    raise(SIGABRT);
    raise(SIGFPE);
    raise(SIGILL);
    raise(SIGINT);
    raise(SIGSEGV);
    raise(SIGTERM);
 
    return 0;
}

Output:

Process SIGABRT signal here.
Process SIGFPE  signal here.
Process SIGILL  signal here.
Process SIGINT  signal here.
Process SIGSEGV signal here.
Process SIGTERM signal here.

See also

runs the signal handler for particular signal
(function) [edit]