C signal handling
In the C Standard Library, signal processing defines how a program handles various 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 interactive attention key on a keyboard). Standard signalsThe C standard defines only 6 signals. They are all defined in
Additional signals may be specified in the Debugging
HandlingA signal can be generated by calling A signal handler is a 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 Signal handlers can be set with If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling Functions
Example usage#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
volatile sig_atomic_t status = 0;
static void catch_function(int signo) {
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);
return EXIT_FAILURE;
}
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 EXIT_SUCCESS;
// exiting after raising signal
}
See alsoReferences
|
Portal di Ensiklopedia Dunia