The Signals structure provides a basic interface to the Unix signal system. It defines four basic signals that can be simulated on most operating systems.
On Unix, this corresponds to the SIGINT signal.
This is used by the interval timer in the IntervalTimer structure. You can use this to generate periodic interrupts in your program.
On Unix, this corresponds to the SIGTERM signal.
This is a pseudo-signal generated internally when a garbage collection has been completed.
In addition the UnixSignal structure provides a few more signals found on Unix systems. These are sigPIPE, sigQUIT, sigUSR1, sigUSR2, sigCHLD, sigCONT, sigTSTP, sigTTIN and sigTTOU. See the source code in the boot/Unix/unix-signals*.sml files for more details.
There may be more signals available on your platform than appear in UnixSignal. In particular SIGHUP has missed out on appearing in either structure. You can fetch it yourself like this:
val sighup = valOf(Signals.fromString "HUP") |
A complete list of the available signals can be printed with this code which you can type to the top-level SML prompt.
app (fn s => print(concat[Signals.toString s, "\n"])) (Signals.listSignals()); |
Here is a program to demonstrate setting up a simple interrupt handler. It just prints a message and lets the program, which is an infinite loop, continue.
fun int_handler(signal, n, cont) = let in print "interrupt\n"; cont end |
fun main(arg0, argv) = let fun loop() = (Signals.pause(); loop()) in Signals.setHandler(Signals.sigINT, Signals.HANDLER int_handler); loop(); OS.Process.success end |
The handler function must return a continuation. This will normally be the third argument to the function. Continuations are described in more detail in the section called Continuations in Chapter 6.
A signal handler function does not execute in the same kind of interrupted state as a signal handler in C. The C-level handler queues the signal and completes. A little later the SML-level handler is called as part of the normal execution of the program. So there are no restrictions on what you can do in an SML signal handler. Also by manipulating the returned continuation you can perform the equivalent of a C longjmp to anywhere else in the program.