CKTIS / C
<signal.h> — signal handling
The <signal.h> header provides signal, raise, and sig_atomic_t. It supports signal handling, whose handlers require special care.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <signal.h>
void (*signal(int sig, void (*func)(int)))(int);
int raise(int sig);signal selects handling: SIG_DFL for default behavior, SIG_IGN to ignore, or a function void handler(int). It returns the previous setting or SIG_ERR on failure. raise triggers a signal and returns zero on success. If raise invokes a handler, it does not return until the handler returns. If the signal is set to SIG_IGN, raise can return zero without invoking a handler.
Signal numbers: SIGABRT — abnormal termination; SIGFPE — arithmetic exception; SIGILL — illegal instruction; SIGINT — interactive interrupt; SIGSEGV — invalid memory access; SIGTERM — termination request.
A safe asynchronous-handler pattern assigns to a volatile sig_atomic_t flag; leave printing and allocation outside the handler. sig_atomic_t provides indivisible access despite asynchronous interrupts, not general thread synchronization.
N3096 makes using signal in a multithreaded program undefined behavior. Do not return from handlers for SIGFPE, SIGILL, or SIGSEGV: returning also has undefined behavior.
Examples
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static volatile sig_atomic_t received = 0;
static void handle_signal(int sig)
{
(void)sig;
received = 1;
}
int main(void)
{
if (signal(SIGTERM, handle_signal) == SIG_ERR)
return EXIT_FAILURE;
if (raise(SIGTERM) != 0)
return EXIT_FAILURE;
if (received != 1)
return EXIT_FAILURE;
puts("Signal handled");
return EXIT_SUCCESS;
}
Result
Signal handled
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.