1. C++ Signal and Raise – An Introduction
C++ device driver programs can use the
signal()
and
raise()
in combination to perform interrupts using SIGINT type. When a program is running, one can interrupt it by making a call to the Raise function. Before we make a call to the raise function, we should register the Signal handling function. The function signal() registers the Signal Handling Function.
The signal function will register the signal type with the Signal Handler Function. The below screen capture shows Signal Types:

C++ Signal Types
In our example, we are going to use
SIGINT
(Signal Interrupt) and its handler function.
2. The C++ Signal Function & Handler
The signal function registers the Signal Type with a handler function. The below picture shows the signature of the signal and its handler function:

Signal and Handler function
In the above picture, we can observe that the first parameter is the signal type and the second parameter is the actual handler function. The handler functions should accept an integer as a parameter and return nothing. This means, it returns void.
3. Raise Function
One can use the
raise()
function to raise the signal by pointing out the signal type number as an input parameter. The below code shows the signature of the Raise function:
1 |
raise( int); |
Remember, in the previous section, we saw the Signal function which registers a signal number with a handler function. When we call the raise function, C++ calls the registered handler function. Now let us explore the signal number C++
SIGINT
which is used to specify program interrupted. The handler will get called when the Raise function is called with SIGINT.
4. Example Program
The Signal handling functions are available in the
CSignal.h
and hence this header file is included in the program. The code is below:
1 2 3 4 |
//Sample 01: Include statements #include <iostream> #include <csignal> using namespace std; |
After including the required header, we will write a delay method. This method will print periods in the console output window. The delay for each period depends on the processor speed. The delay function is below:
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 02: The delay function which makes the program a long running one void Delay(int delayNo) { for (int i = 1; i < (delayNo * 100000000)+1; i++) { if (i % 100000000 == 0) printf("."); else continue; } } |
In the main function, using the call to a signal function, we register the interrupt handler function with signal type. The first parameter
SIGINT
passed-in makes an association of the signal
SIGINT
with the Interrupt handler function. After this, the main program makes a call to the delay, and thereby prints periods twenty times in the console window. Finally, we make a call to the function raise by passing the C++ SIGINT as a parameter. This will make the program get interrupted. Note, the program never does not reach final
printf
message. Code is below:
The interrupt handler function will print the message stating the program is interrupted and halting. Also, it makes a call to the exit and thereby removing the process from the Operating System. The Interrupt Handler Function is below:
1 2 3 4 5 6 7 |
//Sample 03: Signal Handler function void InterruptHandler( int IntNumber ) { printf("\nProgram Interrupted. Interrupt No: %d\n", IntNumber); printf("Quitting...\n"); exit(IntNumber); } |
Now when we run the program you will get the following output:
………………..
Program Interrupted. Interrupt No: 2
Quitting…
In the above output, you can see all the 20 periods and after that, we are explicitly making program interrupt call to quit the application.
5. Closing Notes
Here we saw the example for the Signal
SIGINT
and processing that signal with a signal, raise and the corresponding handler function. We can also process other signals (Shown in Picture 1) the same way we did it for C++ SIGINT.
You can also test the program by interrupting it through ctrl+c key strokes when the program is printing the periods. Pressing ctrl+c when the program is running is also a kind of interrupt (Not an interrupt through raise call) and this interrupt is triggered by the user. Here, in this case also, we can make sure that interrupt handler gets called. The below screen shot shows the user-interrupt and output:
Categories: C++
Tags: C++ raise(), C++ SIGINT, C++ signal()