signals -
ANSI C signal handling
Common usage imply 5 steps:
- Implement the
SignalHandler
interface redef receive_signal_unsafe
to handlesigsegv
redef receive_signal
to handle other signals safely 4, Notify what signals to handle withhandle_signal
- If using the safe handler method, routinely call
check_signals
Usage example:
class MyReceiver
super SignalHandler
redef fun receive_signal(signal)
do
print "received safely {signal}"
if signal == sigalarm then print "Alarm!"
end
redef fun receive_signal_unsafe( signal ) do print "received unsafely {signal}"
end
var r = new MyReceiver
# Handle `sigsegv` signal unsafely (the only way for this one)
r.handle_signal(sigsegv, false)
# Handle `sigint` and `sigalarm` safely
r.handle_signal(sigint, true)
r.handle_signal(sigalarm, true)
# Ask system to receive a `sigalarm` signal in 1 second
set_alarm(1)
loop
# Check signals and callback `receive_signal`
var hit = check_signals
if hit then break
end
Content
- signals: ANSI C signal handling (lib/signals/signals.nit)