Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::signal

      From cppreference.com
      <cpp‎ |utility‎ |program
       
       
      Utilities library
       
       
      Defined in header<csignal>
      /* signal-handler */* signal(int sig,/* signal-handler */* handler);
      (1)
      extern"C"using/* signal-handler */=void(int);
      (2)(exposition only*)

      Changes handling of the signalsig. Depending onhandler, the signal can be ignored, set to default, or handled by a user-defined function.

      When signal handler is set to a function and a signal occurs, it is implementation defined whetherstd::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 callstd::signal(sig,SIG_IGN) at the startup of the program. For the rest, the implementation must callstd::signal(sig,SIG_DFL).

      (Note: POSIX introducedsigaction to standardize these implementation-defined behaviors)

      Contents

      [edit]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.
      • A pointer to a function. The signature of the function must be equivalent to the following:
      extern"C"void fun(int sig);


      [edit]Return value

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

      [edit]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 ofstd::abort orstd::raise (asynchronous signal), the behavior is undefined if

      • the signal handler calls any function within the standard library, except
      • std::abort
      • std::_Exit
      • std::quick_exit
      • std::signal with the first argument being the number of the signal currently handled (async handler can re-register itself, but not other signals).
      (until C++17)

      Aplain lock-free atomic operation is an invocation of a functionf from<atomic> or<stdatomic.h>(since C++23), such that:

      The behavior is undefined if any signal handler performs any of the following:

      • call to any library function, except for plain lock-free atomic operations and the followingsignal-safe functions (note, in particular, dynamic allocation is not signal-safe):
      • access to an object with thread storage duration
      • adynamic_cast expression
      • athrow expression
      • entry to atry block
      • initialization of a static variable that performsdynamic non-local initialization (including delayed until first ODR-use)
      • waits for completion of initialization of any variable with static storage duration due to another thread concurrently initializing it
      (since C++17)

      If the user defined function returns when handlingSIGFPE,SIGILL,SIGSEGV or any other implementation-defined signal specifying a computational exception, the behavior is undefined.

      If the signal handler is called as a result ofstd::abort orstd::raise (synchronous signal), the behavior is undefined if the signal handler callsstd::raise.

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

      (since C++11)

      On return from a signal handler, the value of any object modified by the signal handler that is notvolatilestd::sig_atomic_t or lock-freestd::atomic is indeterminate.

      (until C++14)

      A call to the functionsignal()synchronizes-with any resulting invocation of the signal handler.

      If a signal handler is executed as a result of a call tostd::raise (synchronously), then the execution of the handler issequenced-after the invocation ofstd::raise andsequenced-before the return from it and runs on the same thread asstd::raise. Execution of the handlers for other signals isunsequenced with respect to the rest of the program and runs on an unspecified thread.

      Two accesses to the same object of typevolatilestd::sig_atomic_t do not result in a data race if both occur in the same thread, even if one or more occurs in a signal handler. For each signal handler invocation, evaluations performed by the thread invoking a signal handler can be divided into two groups A and B, such that no evaluations in Bhappen-before evaluations in A, and the evaluations of suchvolatilestd::sig_atomic_t objects take values as though all evaluations in Ahappened-before the execution of the signal handler and the execution of the signal handlerhappened-before all evaluations in B.

      (since C++14)

      [edit]Notes

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

      Signal handlers are expected to haveC linkage and, in general, only use the features from the common subset of C and C++. However, common implementations allow a function with C++ linkage to be used as a signal handler.

      [edit]Example

      Run this code
      #include <csignal>#include <iostream> namespace{volatilestd::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: 0Sending signal: 2SignalValue: 2

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 17.13.5 Signal handlers [support.signal]
      • C++20 standard (ISO/IEC 14882:2020):
      • 17.13.5 Signal handlers [support.signal]
      • C++17 standard (ISO/IEC 14882:2017):
      • 21.10.4 Signal handlers [support.signal]

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3756C++17it was unclear whetherstd::atomic_flag is signal-safeit is

      [edit]See also

      runs the signal handler for particular signal
      (function)[edit]
      fence between a thread and a signal handler executed in the same thread
      (function)[edit]
      C documentation forsignal
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/program/signal&oldid=172239"

      [8]ページ先頭

      ©2009-2025 Movatter.jp