next up previous contents index
Next: Notes on signal handling Up: Built-in predicates Previous: Printing messages   Contents   Index


Handling signals

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

Signals are used to handle internal errors (execution of a non-existing CPU intruction, arithmetic domain errors, illegal memory access, resource overflow, etc.), as well as for dealing asynchronous inter-process 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 funtionality to raise a signal in another thread for achieving asynchronous inter-process (or inter-thread) communication (Unix kill() function).

on_signal3+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_signal3 is a meta predicate, which implies that Module:Name refers the Name/1 in the module Module.

Two predicate-names have special meaning. throw implies Prolog will map the signal onto a Prolog exception as described in exception. 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_functionAddress.

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

error(signal(SigName, SigNum), Context)

One possible usage of this is, for example, to limit the time spent on proving a goal. This requires a little C-code for setting the alarm timer (see foreign):


\begin{code}
...

Next, we can define the following Prolog code:


\begin{code}
:- load_foreign_library(alarm).
\par :- on_signal(alrm, throw).
\pa...
... fail), !,
alarm(0).
call_with_time_limit(_, _) :-
alarm(0),
fail.
\end{code}

The signal names are defined by the C-Posix standards as symbols of the form SIG_SIGNAME. The Prolog name for a signal is the lowercase version of SIGNAME. The predicate current_signal3 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: ill, fpe, segv, pipe, alrm, bus, xcpu, xfsz and vtalrm. current_signal3?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_signal3).



Subsections
next up previous contents index
Next: Notes on signal handling Up: Built-in predicates Previous: Printing messages   Contents   Index
Dr. Richard Botting 2001-12-12