Ada Programming/Libraries/Ada.Interrupts

is a unit of the Predefined Language Environment since Ada 95.

Introduction
Sometimes it's necessary for your program to be able to catch and handle interrupts such as,   or  , and for this we have the  package. Attaching handlers to interrupts can be done both statically and dynamically and, luckily, both methods are easy to grasp. The following program will show you both methods.

Example
I've commented the program, so it should be fairly easy to understand what's going on. You can grab the entire program from this Git repository:

https://github.com/kjseefried/Catch_Signals

Git is available for all Linux distros. The commands to fetch  using Git and then compile it are:

$ git clone https://github.com/kjseefried/Catch_Signals $ cd Catch_Signals $ gnatmake -P catch_signals.gpr

The executable can now be found in the  directory.

If you don't feel like using Git, the full source code can be copied from below. I will of course do my best to make sure that the code on this page matches the code in the Git repository, but since this is a wiki where anybody can make changes, I cannot guarantee the safety of running the code on this page, so use it with caution.

The program attach signal handlers to these interrupts:


 * SIGHUP
 * SIGINT
 * SIGPWR
 * SIGTERM

You can send those interrupts to the running program like this:

kill -SIGHUP pid

Where  is the Process Id of the program.

catch_signals.gpr
This file sets up compile options. It informs the compiler of the location of the source files and where to put the object and the executable files. You must, of course, create the  and   directories, if you haven't cloned the Git repository.

catch_signals.adb
This is the main program file. Not much happens here. We simply wait for an interrupt at the  statement.

src/process_control.ads
In this file we define our interrupt handlers. For an example on how to dynamically attach an interrupt handler, take a look at the  code.

src/process_control.adb
Pay special attention to the  procedure where the   interrupt is attached to the   procedure, and later re-attached to the   procedure. This means that in order to kill the program using, you have to do   twice.

Specification
Interrupt_ID Implementation_Defined Parameterless_Handler Is_Reserved Interrupt  Interrupt_ID  Boolean Is_Attached Interrupt  Interrupt_ID  Boolean Current_Handler Interrupt  Interrupt_ID Parameterless_Handler Attach_Handler New_Handler  Parameterless_Handler Interrupt    Interrupt_ID Exchange_Handler Old_Handler     Parameterless_Handler New_Handler      Parameterless_Handler Interrupt        Interrupt_ID Detach_Handler Interrupt  Interrupt_ID Reference Interrupt  Interrupt_ID  SystemAddress AdaInterrupts

Wikibook

 * Ada Programming/Libraries/Ada