Home Signal Handling: The Unix Asynchronous Interrupt System Nobody Gets Right First Time

    Signal Handling: The Unix Asynchronous Interrupt System Nobody Gets Right First Time

    0
    27
    blank

    Unix signals are a mechanism for notifying processes of events asynchronously — SIGTERM to request termination, SIGKILL to demand it, SIGHUP historically for hangups, SIGSEGV for segfaults, SIGUSR1 and SIGUSR2 for whatever you fancy. They arrive between any two instructions, which means a signal handler can interrupt a running function at any point, including in the middle of a malloc. This makes signal handlers extraordinarily difficult to write correctly: they can only safely call “async-signal-safe” functions, a small and not immediately obvious list. Code that works perfectly for months can fail mysteriously when a signal arrives at exactly the wrong moment. The correct modern approach — writing signals to a self-pipe and processing them in the main event loop — is well documented, widely recommended, and not what most signal handlers actually do.