Signal and Handler function
Signal and Handler function

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
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
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:

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:

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:

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:

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:

"Ctrl+<yoastmark

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: