C signal handling: Difference between revisions

Content deleted Content added
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
FrescoBot (talk | contribs)
m Bot: link syntax and minor changes
 
(16 intermediate revisions by 13 users not shown)
Line 1:
{{Short description|Handling of signals in the C programming language}}
{{Use dmy dates|date=JanuaryJuly 20122022}}
{{C_Standard Library}}
 
In the [[C Standard Library]], '''signal processing''' defines how a program handles various [[Signal (computing)|signals]] while it executes. A signal can report some exceptional behavior within the program (''such as [[division by zero]]''), or a signal can report some asynchronous event outside the program (''such as someone striking an [[SIGINT (POSIX)|interactive attention key]] on a keyboard'').
 
==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=httphttps://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf | title=ISO/IEC 9899:1999 specification | at=p. 258, § 7.14 ''Signal handling''}}</ref>
 
*<code>SIGABRT</code> - "abort", abnormal termination.
*<code>SIGFPE</code> - [[Floatingfloating point exception|'''f'''loating '''p'''oint '''e'''xception]].
*<code>SIGILL</code> - "illegal", invalid instruction.
*<code>SIGINT</code> - "interrupt", interactive attention request sent to the program.
*<code>SIGSEGV</code> - "[[segmentation violation|'''seg'''mentation '''v'''iolation]]", invalid memory access.
*<code>SIGTERM</code> - "terminate", termination request sent to the program.
 
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]]) define more than 15 additional signals; see [[Unix signal]].<ref name="sus">{{cite web | url=httphttps://pubs.opengroup.org/onlinepubs/007904975/ | title=The Open Group Base Specifications Issue 6 - signal.h - signals | accessdate=10 January 2012}}</ref>
 
=== Debugging ===
*<code>SIGTRAP</code> for debugging purposes. It's platform-dependent and may be used on [[Unix]]-like operating systems.
 
==Handling==
Line 23 ⟶ 28:
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>.
 
Signal handlers can be set be with <code>signal()</code> or <code>sigaction()</code>. The behavior of <code>signal()</code> has been changed multiple times across history and isits nowuse consideredis deprecateddiscouraged.<ref>httphttps://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).
 
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" style="font-size:0.85em"
! Function
! Description
|-
| {{anchor|raise}}<code>[httphttps://en.cppreference.com/w/c/program/raise raise]</code>
| artificially raisessends a signal to the calling process
|-
| {{anchor|signalraise}}<code>[http://en.cppreference.com/w/c/program/signal signal]kill</code>
|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 46 ⟶ 54:
#include <stdio.h>
#include <stdlib.h>
 
volatile sig_atomic_t status = 0;
 
static void catch_function(int signo) {
status = signo;
puts("Interactive attention signal caught.");
}
 
Line 58 ⟶ 68:
}
puts("Raising the interactive attention signal.");
if (raise(SIGINT) != 0) {
fputs("Error raising the signal.\n", stderr);
return EXIT_FAILURE;
}
if (status == SIGINT) puts("Interactive attention signal caught.");
puts("Exiting.");
return EXIT_SUCCESS;
Line 73 ⟶ 84:
==References==
{{Reflist}}
 
{{Use dmy dates|date=January 2012}}
 
[[Category:C standard library]]