POSIX Signals
Signals are a way to notify running programs of events to trigger a response, such as quitting or error handling. They are used in Unix and POSIX-compliant operating systems and represent a form of inter-process communication (IPC).
Setting a signal handler in C
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> void sig_segv_handler(int signum) { printf("SEGV ignored.\n"); } int main(int argc, char** argv) { /* int signal () (int signum, void (*func)(int)) */ signal(SIGSEGV, sig_segv_handler); // cause intentional segmentation fault (also gets picked up by gcc as stack-smashing) float* arr = (float*) malloc(sizeof(float) * 3); arr = (float[3]) {0.4f, 0.3f, 3.2f}; arr[3] = 11.0f; printf("%.1f %.1f %.1f\n", arr[0], arr[1], arr[2]); }
Compile with gcc -O0 handler.c -o handler -fno-stack-protector
.