Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

signal — configure signal handling

signal from <signal.h> selects a signal's response: default handling, ignoring it, or invoking a handler.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <signal.h>
void (*signal(int sig, void (*func)(int)))(int);

signal takes a signal number and SIG_DFL, SIG_IGN, or a void (*)(int) pointer. It returns the previous setting; failure means SIG_ERR and positive errno.

Registration persistence is implementation-defined. Under N3096, use in a multithreaded program has undefined behavior.

For asynchronous signals, do not call printf or malloc. A simple pattern assigns to volatile sig_atomic_t; ++ is not such an assignment. Returning after a computational exception, such as SIGSEGV, has undefined behavior.

The example uses raise, which waits for the handler to finish. Printing happens outside it.

Examples

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

static volatile sig_atomic_t handled = 0;

static void handler(int sig)
{
    (void)sig;
    handled = 1;
}

int main(void)
{
    if (signal(SIGINT, handler) == SIG_ERR)
        return EXIT_FAILURE;
    if (raise(SIGINT) != 0)
        return EXIT_FAILURE;
    if (handled != 1)
        return EXIT_FAILURE;
    puts("handled");
    return EXIT_SUCCESS;
}

Result

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.

Sources