signal_handle(signum, fn) registers a callback for an OS signal. signal_reset restores the default behavior. This recipe sends SIGUSR1 to itself and catches it.
66-signal-handling.nxSource →
// Signal handling — reacting to OS signals // Manejo de señales — reaccionar a señales del SO var got_signal: int = 0 fn on_sigusr1() -> int { got_signal = 1 return 0 } fn main() -> int { // Register a handler for SIGUSR1 (signal 10) signal_handle(10, on_sigusr1) print("handler registered for SIGUSR1") // Send SIGUSR1 to ourselves let my_pid: int = getpid() print("my pid: " + int_to_string(my_pid)) kill_process(my_pid, 10) // Give time for signal delivery sleep(10) if got_signal == 1 { print("signal received!") } else { print("no signal yet") } // Reset signal to default behavior signal_reset(10) print("handler reset to default") return 0 }
Illustrative outputstdout
handler registered for SIGUSR1 my pid: 12345 signal received! handler reset to default
How it works
signal_handle(signum, fn) installs a callback function for the given signal number. SIGUSR1 is signal 10 on Linux. The handler function must have the signature fn() -> int.
The program uses kill_process(pid, signal) to send SIGUSR1 to itself. After a brief sleep to allow delivery, it checks the global flag that the handler set.
signal_reset(signum) restores the default OS behavior for the signal. For SIGUSR1, the default action is to terminate the process, so you would not want to send it again after resetting.