<signal.h>

<signal.h>


Include the standard header <signal.h> to specify how the program handles signals while it executes. A signal can report some exceptional behavior within the program, such as division by zero. Or a signal can report some asynchronous event outside the program, such as someone striking an interactive attention key on a keyboard.

You can report any signal by calling raise. Each implementation defines what signals it generates (if any) and under what circumstances it generates them. An implementation can define signals other than the ones listed here. The standard header <signal.h> can define additional macros with names beginning with SIG to specify the values of additional signals. All such values are integer constant expressions >= 0.

You can specify a signal handler for each signal. A signal handler is a function that the target environment calls when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls longjmp. For maximum portability, an asynchronous signal handler should only:

  • make calls (that succeed) to the function signal
  • assign values to objects of type volatile sig_atomic_t
  • return control to its caller

Furthermore, in C++, a signal handler should:

  • have extern "C" linkage
  • use only language features common to C and C++

If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling abort, exit, or longjmp.

    /* MACROS */
#define SIGABRT <integer constant expression >= 0>
#define SIGFPE <integer constant expression >= 0>
#define SIGILL <integer constant expression >= 0>
#define SIGINT <integer constant expression >= 0>
#define SIGSEGV <integer constant expression >= 0>
#define SIGTERM <integer constant expression >= 0>
#define SIG_DFL <address constant expression>
#define SIG_ERR <address constant expression>
#define SIG_IGN <address constant expression>

    /* TYPES */
typedef i-type sig_atomic_t;

    /* FUNCTIONS */
int raise(int sig);
void (*signal(int sig, void (*func)(int)))(int);

raise

int raise(int sig);

The function sends the signal sig and returns zero if the signal is successfully reported.

sig_atomic_t

typedef i-type sig_atomic_t;

The type is the integer type i-type for objects whose stored value is altered by an assigning operator as an atomic operation (an operation that never has its execution suspended while partially completed). You declare such objects to communicate between signal handlers and the rest of the program.

SIGABRT

#define SIGABRT <integer constant expression >= 0>

The macro yields the sig argument value for the abort signal.

SIGFPE

#define SIGFPE <integer constant expression >= 0>

The macro yields the sig argument value for the arithmetic error signal, such as for division by zero or result out of range.

SIGILL

#define SIGILL <integer constant expression >= 0>

The macro yields the sig argument value for the invalid execution signal, such as for a corrupted function image.

SIGINT

#define SIGINT <integer constant expression >= 0>

The macro yields the sig argument value for the asynchronous interactive attention signal.

signal

void (*signal(int sig, void (*func)(int)))(int);

The function specifies the new handling for signal sig and returns the previous handling, if successful; otherwise, it returns SIG_ERR.

  • If func is SIG_DFL, the target environment commences default handling (as defined by the implementation).
  • If func is SIG_IGN, the target environment ignores subsequent reporting of the signal.
  • Otherwise, func must be the address of a function returning void that the target environment calls with a single int argument. The target environment calls this function to handle the signal when it is next reported, with the value of the signal as its argument.

When the target environment calls a signal handler:

  • The target environment can block further occurrences of the corresponding signal until the handler returns, calls longjmp, or calls signal for that signal.
  • The target environment can perform default handling of further occurrences of the corresponding signal.
  • For signal SIGILL, the target environment can leave handling unchanged for that signal.

SIGSEGV

#define SIGSEGV <integer constant expression >= 0>

The macro yields the sig argument value for the invalid storage access signal, such as for an erroneous lvalue expression.

SIGTERM

#define SIGTERM <integer constant expression >= 0>

The macro yields the sig argument value for the asynchronous termination request signal.

SIG_DFL

#define SIG_DFL <address constant expression>

The macro yields the func argument value to signal to specify default signal handling.

SIG_ERR

#define SIG_ERR <address constant expression>

The macro yields the signal return value to specify an erroneous call.

SIG_IGN

#define SIG_IGN <address constant expression>

The macro yields the func argument value to signal to specify that the target environment is to henceforth ignore the signal.


See also the Table of Contents and the Index.

Copyright © 1992-2006 by P.J. Plauger and Jim Brodie. All rights reserved.

Last modified: 2013-12-21



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus