Interrupts
Masking interrupts
See also
Normally, when you type Ctrl-C, you interrupt the foreground activity. But what if you were in the midst of a complex script and needed to do some kind of cleanup before you exited? What if you wanted to be sure you had a chance to delete any temporary files you might have littered around?
The solution is the onintr statement, which allows you to define the action to be taken when an interrupt is received. It causes whatever’s running to be interrupted all the way back up to the block in which the onintr routine was defined and for the interrupt routine to be run in that current thread. Within that interrupt routine, you could, for example, remove all your temporary files and goto the end of the script or return a special value from a procedure or whatever else might be appropriate.
Here’s another example, returning from a procedure. Note how the value returned (and printed) is the one produced by the onintr statement.
When execution leaves the block in which an onintr is defined, the previous onintr (if any) again takes effect. Note that a null onintr routine does not mean that interrupts are ignored, merely that after processing bubbles back up to the level where that onintr was defined, that it will continue with the next statement. Notice how, in this example, when the Ctrl-C is received when obviously execution is stuck in the infinite loop inside bar, that the onintr goto xx causes a branch to xx in the same block in which the onintr was defined, not the xx in the block where execution was going on. Also, notice that once both procedures have been exited, we’re back to the same onintr routine we defined a few statements earlier.
In cases where you’d like to simply turn off interrupts or defer processing them, use the irqmask variable. By default, it’s set to 0, meaning interrupts will be accepted immediately. Setting it to 1 means interrupts will be deferred until the mask is cleared again. Setting it to 2 means interrupts will be totally ignored.
irqmask is a per-thread variable, meaning each thread can independently decide how it will respond to interrupts. Each new thread always starts out with irqmask = 0 (interrupts enabled).
Miscellaneous statements
Order of evaluation
Tutorial: Scheduling