Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Create account
Namespaces
Variants
    Views
    Actions

      signal

      From cppreference.com
      <c‎ |program
       
       
      Program support utilities
      Program termination
      Unreachable control flow
      Communicating with the environment
      Memory alignment query
      Signals
      Signal types
      Non-local jumps
      Types
       
      Defined in header<signal.h>
      void(*signal(int sig,void(*handler)(int)))(int);

      Sets the error handler for signalsig. 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 whethersignal(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.

      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.
      • pointer to a function. The signature of the function must be equivalent to the following:
      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 user defined function returns when handlingSIGFPE,SIGILL orSIGSEGV, the behavior is undefined.

      If the signal handler is called as a result ofabort orraise, the behavior is undefined if the signal handler callsraise.

      If the signal handler is called NOT as a result ofabort orraise (in other words, the signal handler isasynchronous), the behavior is undefined if

      • the signal handler calls any function within the standard library, except
      • abort
      • _Exit
      • quick_exit
      • signal with the first argument being the number of the signal currently handled (async handler can re-register itself, but not other signals).
      • atomic functions from<stdatomic.h> if the atomic arguments are lock-free
      • atomic_is_lock_free (with any kind of atomic arguments)
      • the signal handler refers to any object with staticor thread-local(since C11)storage durationthat is not a lock-freeatomic(since C11) other than by assigning to a staticvolatilesig_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 notvolatilesig_atomic_t or lock-free atomic(since C11) is undefined.

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

      [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.

      Besidesabort andraise, POSIX specifies thatkill,pthread_kill, andsigqueue generate synchronous signals.

      POSIX recommendssigaction instead ofsignal, due to its underspecified behavior and significant implementation variations, regarding signal delivery while a signal handler is executed.

      [edit]Example

      Run this code
      #include <signal.h>#include <stdio.h> volatilesig_atomic_t gSignalStatus; void signal_handler(int signal){  gSignalStatus= signal;} int main(void){  signal(SIGINT, signal_handler); printf("SignalValue: %d\n", gSignalStatus);printf("Sending signal: %d\n",SIGINT);raise(SIGINT);printf("SignalValue: %d\n", gSignalStatus);}

      Output:

      SignalValue: 0Sending signal: 2SignalValue: 2

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.14.1.1 The signal function (p: 193-194)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.14.1.1 The signal function (p: 266-267)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.14.1.1 The signal function (p: 247-248)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.7.1.1 The signal function

      [edit]See also

      runs the signal handler for particular signal
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/program/signal&oldid=140330"
      Navigation
      Toolbox
      • In other languages

      [8]ページ先頭

      ©2009-2025 Movatter.jp