From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/program%2Fsignal.html | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 devdocs/c/program%2Fsignal.html (limited to 'devdocs/c/program%2Fsignal.html') diff --git a/devdocs/c/program%2Fsignal.html b/devdocs/c/program%2Fsignal.html new file mode 100644 index 00000000..4dfb42f4 --- /dev/null +++ b/devdocs/c/program%2Fsignal.html @@ -0,0 +1,78 @@ +

signal

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 a 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 of signals from occurring while the signal handler runs.

+

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)
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 handler.

+

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 (in other words, the signal handler is asynchronous), the behavior is undefined if

+

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.

+

Besides abort and raise, POSIX specifies that kill, pthread_kill, and sigqueue generate synchronous signals.

+

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

+

Example

#include <signal.h>
+#include <stdio.h>
+ 
+volatile sig_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: 0
+Sending signal: 2
+SignalValue: 2

References

See also

+ +
runs the signal handler for particular signal
(function)
C++ documentation for signal
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/program/signal +

+
-- cgit v1.2.3