4.11 Handling signals

As of version 3.1.0, SWI-Prolog is able to handle software interrupts (signals) in Prolog as well as in foreign (C) code (see section 10.4.13).

Signals are used to handle internal errors (execution of a non-existing CPU instruction, arithmetic domain errors, illegal memory access, resource overflow, etc.), as well as for dealing with asynchronous interprocess communication.

Signals are defined by the POSIX standard and part of all Unix machines. The MS-Windows Win32 provides a subset of the signal handling routines, lacking the vital functionality to raise a signal in another thread for achieving asynchronous interprocess (or interthread) communication (Unix kill() function).

on_signal(+Signal, -Old, :New)
Determines the reaction on Signal. Old is unified with the old behaviour, while the behaviour is switched to New. As with similar environment control predicates, the current value is retrieved using on_signal(Signal, Current, Current).

The action description is an atom denoting the name of the predicate that will be called if Signal arrives. on_signal/3 is a meta-predicate, which implies that <Module>:<Name> refers to <Name>/1 in module <Module>. The handler is called with a single argument: the name of the signal as an atom. The Prolog names for signals are explained below.

Two predicate names have special meaning. throw implies Prolog will map the signal onto a Prolog exception as described in section 4.10. default resets the handler to the settings active before SWI-Prolog manipulated the handler.

Signals bound to a foreign function through PL_signal() are reported using the term $foreign_function(Address).

After receiving a signal mapped to throw, the exception raised has the following structure:

error(signal(<SigName>, <SigNum>), <Context>)

The signal names are defined by the POSIX standard as symbols of the form SIG<SIGNAME>. The Prolog name for a signal is the lowercase version of <SIGNAME>. The predicate current_signal/3 may be used to map between names and signals.

Initially, some signals are mapped to throw, while all other signals are default. The following signals throw an exception: fpe, alrm, xcpu, xfsz and vtalrm.

current_signal(?Name, ?Id, ?Handler)
Enumerate the currently defined signal handling. Name is the signal name, Id is the numerical identifier and Handler is the currently defined handler (see on_signal/3).

4.11.1 Notes on signal handling

Before deciding to deal with signals in your application, please consider the following: