Example 1 — A flag set by a signal handler
raise triggers the handler synchronously.
#include <signal.h>
#include <stdio.h>
static volatile sig_atomic_t flag = 0;
static void handler(int sig) {
(void)sig;
flag = 1;
}
int main(void) {
signal(SIGINT, handler);
raise(SIGINT);
printf("handler ran: flag=%d\n", (int)flag);
return 0;
} How It Works
signalinstallshandlerforSIGINT.raisesends the signal, so the handler runs immediately.volatileensuresmainre-reads the flag instead of assuming it is still 0.
