Content deleted Content added
Katharineamy (talk | contribs) added Category:C standard library; removed {{uncategorized}} using HotCat |
m Bot: link syntax and minor changes |
||
(43 intermediate revisions by 35 users not shown) | |||
Line 1:
{{Short description|Handling of signals in the C programming language}}
{{C_Standard Library}}
In the [[C Standard Library]],
==Standard signals==
{{see also|Unix signal}}
The C standard defines only 6 signals. They are all defined in <code>signal.h</code> header (<code>csignal</code> header in [[C++]]):<ref name=c99>{{cite book | url=
*<code>SIGABRT</code>
*<code>SIGFPE</code>
*<code>SIGILL</code>
*<code>SIGINT</code>
*<code>SIGSEGV</code>
*<code>SIGTERM</code>
Additional signals may be specified in the <code>signal.h</code> header by the implementation. For example, Unix and [[Unix-like]] operating systems (such as [[Linux]])
=== Debugging ===
*<code>SIGTRAP</code> for debugging purposes. It's platform-dependent and may be used on [[Unix]]-like operating systems.
==Handling==
A signal can be generated by calling <code>raise()</code> or <code>kill()</code> system calls. <code>raise()</code> sends a signal to the current process, <code>kill()</code> sends a signal to a specific process.
Signal handlers can be set with <code>signal()</code> or <code>sigaction()</code>. The behavior of <code>signal()</code> has been changed multiple times across history and its use is discouraged.<ref>https://man7.org/linux/man-pages/man2/signal.2.html Signal(2) manpage</ref> It is only portable when used to set a signal's disposition to SIG_DFL or SIG_IGN. Signal handlers can be specified for all but two signals ([[SIGKILL]] and [[SIGSTOP]] cannot be caught, blocked or ignored).
▲A signal handler can be specified for all but two signals ([[SIGKILL]] and [[SIGSTOP]] cannot be caught, blocked or ignored). A signal handler is a [[Function (computer science)|function]] which is called by the target environment when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls <code>longjmp()</code>. For maximum portability, an asynchronous signal handler should only:
If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling <code>abort()</code>, <code>exit()</code>, or <code>longjmp()</code>.
Line 29 ⟶ 34:
==Functions==
{| class="wikitable
! Function
! Description
|-
| {{anchor|raise}}<code>[
| artificially
|-
|
|artificially sends a signal to a specified process
|-
| {{anchor|signal}}<code>[https://en.cppreference.com/w/c/program/signal signal]</code>
| sets the action taken when the program receives a specific signal
|}
Line 42 ⟶ 50:
==Example usage==
<
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
volatile sig_atomic_t status = 0;
static void catch_function(int signal) {▼
puts("Interactive attention signal caught.");▼
status = signo;
}
int main(void) {
// Set above function as signal handler for the SIGINT signal:
if (signal(SIGINT, catch_function) == SIG_ERR) {
fputs("An error occurred while setting a signal handler.\n", stderr);
Line 57 ⟶ 68:
}
puts("Raising the interactive attention signal.");
if (raise(SIGINT)
fputs("Error raising the signal.\n", stderr);
return EXIT_FAILURE;
}
▲ if (status == SIGINT) puts("Interactive attention signal caught.");
puts("Exiting.");
return
// exiting after raising signal
}
</syntaxhighlight>
==See also==
Line 71 ⟶ 84:
==References==
{{Reflist}}
▲{{Use dmy dates|date=January 2012}}
[[Category:C standard library]]
|