next up previous index
Next: The block/3 Predicate Up: Exceptions Previous: Using Tools in

Interrupts

    The execution of a Prolog program may be interrupted, by such events as system action or the user typing ^C. When this happens the ECLiPSe system receives an identifier of the interrupt which is usually a small integer, and in a manner very similar to that in the case of error handling, calls an interrupt handling routine. Just as error handlers may be user defined, so it is possible to define interrupt handlers. The goal

set_interrupt_handler(N, PredSpec)
  assigns the procedure specified by PredSpec as the interrupt handler for the interrupt identified by N. Here, however, N is an operating system interrupt identifier: thus, the definition of interrupt handlers is operating system dependent. The built-in current_interrupt/2 is provided to check and/or generate the valid interrupt numbers and their mnemonic names. Some interrupts can not be caught by the user (e.g. the kill signal), trying to establish a handler for them yields an error message.

To test interrupt handlers, the built-in kill/2 may be used to send a signal to the own process.

The predicate get_interrupt_handler/3   may be used to find the current interrupt handler for an interrupt N, in the same manner as get_error_handler:

get_interrupt_handler(N, PredSpec, HomeModule)
The predicates reset_interrupt_handler/1 and reset_interrupt_handlers/0 are used to reset a particular interrupt handler or all interrupt handlers to their default values.

An interrupt handler has one optional argument, which is the interrupt number. There is no argument corresponding to the error culprit, since the interrupt has no relation to the currently executed predicate. A handler may be defined which takes no argument (such as when the handler is defined for only one interrupt type). If the handler has one argument, the identifier of the interrupt is passed to the handler when it is called.

When an interrupt occurs, the system halts what it is currently doing and calls the interrupt handler. Just as in the case with error handling, the interrupt handler can be any Prolog procedure. However, unlike the situation in the case of error handling, when the handler exits, be it with success or failure, the execution is resumed at the point where it was interrupted, the interrupt handling is in this case completely independentgif. This ``resume and forget'' policy means that to the Prolog program, an interrupt is ``invisible'' --- providing the handler has no side effects, the program continues as if the interrupt had never happened. As a consequence it is not significant whether the handler succeeds or fails. However, again just as in the case of error handlers, a call to the predicate exit_block/1 may be   made in order to escape from within the handler to the corresponding   call of block/3. Obviously, in this case the interrupted execution can no longer be resumed.

There is one special interrupt handler default/0 which performs   the standard UNIX handling of the specified interrupt (signal). Setting this handler is equivalent to calling signal(N, SIG_DFL) on the C level. Thus e.g. specifying

?- set_interrupt_handler(2, default/0)
will exit the ECLiPSe system when ^C is pressed.

A signal is ignored by setting the handler to true/0.   This is equivalent to calling signal(N, SIG_IGN) on the C level. Apart from these two exceptions, all other arguments will result in the specified predicate to be called when the appropriate interrupt occurs.

ECLiPSe provides special support for debugging interrupt handlers. They can be debugged independently of the interrupted program. To achieve this, every interrupt number has a flag that can take one of the values debug, trace, nodebug or notrace (the latter two are synonyms). It specifies if the corresponding interrupt handlers are executed with the debugger in leap mode, in creep mode or without debugger respectively. The flags can be set with the built-in set_interrupt_flag/2 and queried with get_interrupt_flag/2.

The remarks about care in the use of error handlers, especially in the matter of how they affect input and output, are equally applicable to interrupt handlers.



next up previous index
Next: The block/3 Predicate Up: Exceptions Previous: Using Tools in



Micha Meier
Mon Mar 4 12:11:45 MET 1996