SysTutorials

Linux Manual Pages › Section 3

stdsignal(3)

std::signal - std::signal

Synopsis

Defined in header<csignal>
    /*signal-handler*/ *signal(int sig, /*signal-handler*/ *handler);
(1)
    extern "C" using /*signal-handler*/
    = void(int);     // exposition-only (2)

Sets the handler for signal sig. The signal handler can be set so that default handling will occur, signal is ignored, or a user-defined function is called. When signal handler is set to a function and a signal occurs, it is implementation defined whether std::signal(sig, SIG_DFL) will be executed immediately before the start of signal handler. Also, the implementation can prevent some implementation-defined set of signals from occurring while the signal handler runs. For some of the signals, the implementation may call std::signal(sig, SIG_IGN) at the startup of the program. For the rest, the implementation must call std::signal(sig, SIG_DFL). (Note: POSIX introduced sigaction to standardize these implementation-defined behaviors)

Parameters

          the signal to set the signal handler to. It can be an implementation-defined value or one of the following values:

          SIGABRT           SIGFPE defines signal types sig - SIGILL (macro constant)           SIGINT           SIGSEGV           SIGTERM

          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. handler - * pointer to a function. The signature of the function must be equivalent to the following:

          extern "C" 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 handler. If the signal handler is called NOT as a result of std::abort or std::raise (asynchronous signal), the behavior is undefined if

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. Signal handlers are expected to have C_linkage and, in general, only use the features from the common subset of C and C++. It is implementation-defined if a function with C++ linkage can be used as a signal handler.

Example

// Run this code

#include <csignal>
#include <iostream>
namespace
{
volatile std::sig_atomic_t gSignalStatus;
}
void signal_handler(int signal)
{
gSignalStatus = signal;
}
int main()
{
// Install a signal handler
std::signal(SIGINT, signal_handler);
std::cout << "SignalValue: " << gSignalStatus << ‘\n’;
std::cout << "Sending signal " << SIGINT << ‘\n’;
std::raise(SIGINT);
std::cout << "SignalValue: " << gSignalStatus << ‘\n’;
}

Possible output:

  SignalValue: 0   Sending signal 2   SignalValue: 2

See also

                    runs the signal handler for particular signal raise (function) atomic_signal_fence fence between a thread and a signal handler executed in the same thread                     (function) (C++11)