interrupt â Graceful POSIX Signal Trapping for Long-Running Processes
Overview & Purpose
The interrupt module (code/subr/interrupt.c and code/subr/interrupt.h) provides a lightweight POSIX signal-trapping mechanism originally written by Guy Carpenter at James Cook University of North Queensland (JCUNQ) on February 9, 1990. In the Ocean Mapping Group (OMG) architecture, batch gridding and sidescan mosaicking programs maintain large in-memory LRU tile caches (code/subr/cache.c) backed by temporary disk files. If a user presses Ctrl-C (SIGINT) or disconnects a terminal session (SIGHUP) during a multi-hour run, immediate process termination would leave cached grid tiles unflushed and output .r4 files corrupted. interrupt traps these signals and sets a pollable global flag (interrupt_flag) so processing loops can finish the current ping or cleanly flush and close output files before exiting.
Global State Variables (interrupt.h / interrupt.c)
extern int interrupt_flag;
interrupt_flag: Initialized to0(false) wheninterrupt_trap()is called. Asynchronously set to1(true) byinterrupt_handler()wheneverSIGINTorSIGHUPis delivered to the process.oldHUPhandler,oldINThandler: Internalvoid *pointers ininterrupt.cthat preserve the previous signal dispositions returned bysignal(SIGHUP, ...)andsignal(SIGINT, ...)so they can be restored when critical sections complete.
Public Functions & Signatures
1. Enable Signal Trapping
void interrupt_trap(void);
- Purpose: Installs
interrupt_handleras the active signal handler for bothSIGHUP(terminal hangup) andSIGINT(keyboard interruptCtrl-C), saves the previous handlers inoldHUPhandlerandoldINThandler, and resetsinterrupt_flag = 0. - Error Handling: If
signal()returns-1(SIG_ERR), callserror("interrupt_trap: failed to trap signal ...")viacode/subr/error.c.
2. Restore Original Signal Handlers
void interrupt_untrap(void);
- Purpose: Restores the saved signal handlers (
oldHUPhandlerandoldINThandler) forSIGHUPandSIGINT. Typically called once a batch ping loop finishes and the program is ready to flush its cache and exit normally.
3. Asynchronous Signal Callback
void interrupt_handler(int sig, int code, int scp);
- Purpose: Invoked by the OS kernel when
SIGINTorSIGHUParrives. Setsinterrupt_flag = trueand emits a non-fatal diagnostic message (warning("Interrupt (%d) trapped", sig)) tostderrso the user knows the interrupt was acknowledged and graceful shutdown is underway.
How It Fits Into the OMG Architecture
interrupt is used in batch processing engines and interactive X11/Motif viewers that perform heavy disk or graphics operations:
- Sidescan Mosaicking (
mos2): Callsinterrupt_trap()before iterating over survey lines; checksif (interrupt_flag)after each ping so an interrupted mosaic still flushes all modified cache blocks (cache_flush()) and writes a valid.r4/.bmosaic file. - Weighted Bathymetric Gridding (
weigh_grid): TrapsSIGINT/SIGHUPduring multi-file swath accumulation so partial grids can be normalized and written to disk if the user aborts early. - Interactive Swath & Real-Time Viewers (
swathed,RT): Used inSE_WaterColumn.candRT_unravel.cto allow users to cancel long water-column stacking or real-time playback loops cleanly without crashing the GUI session.