C++ Signal Handling

WHAT IS SIGNAL HANDLING?

  • Signals are the interrupts which force the operating system to stop the ongoing tasks and attend the task for which the interrupt has been sent.
  • These interrupt can pause a service in any program of an operating system. In C++, various signals are offered which can catch and process in a program.
  • The programmers can generate interrupts by pressing Ctrl+C on a LINUX, UNIX, Windows or Mac OS X. Some of the signals cannot be caught by the program but there is a following list of signals which you can catch in your program and can take proper actions based on the signal.

The <csignal> header file is used to define these signals.

SIGINT:

It produces receipt for an active signal.

SIGABRT:

It terminates the program abnormally.

SIGTERM:

It sends a termination request to the program.

SIGFPE:

It produces the erroneous arithmetic operation such as divide by zero or an operation resulting in an overflow.

SIGSEGV:

It means an invalid access to storage.

SIGILL:

This signal detects the illegal command or instruction.

THE SIGNAL ( ) FUNCTION

In C++, signal handling library provides function signal to trap unexpected events.

SYNTAX FOR THIS FUNCTION IS:

signal(registered signal, signal handler)

  • In the above syntax, the first argument is an integer which represents the signal number and the second argument is the pointer to a signal handling function.
  • The signal which we want to be catching it should be registered and must be associated with a signal handling function. It should be of void type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
 
#include <csignal>
 
using namespace std;
 
void signal_handler( int signalnum ) {
 
cout << “The interrupt signal is (“ << signalnum << “). \n”;
 
// terminate program
 
exit(signalnum);
 
}
 
int main () {
 
signal(SIGABRT, signal_handler);
 
// register signal SIGABRT and signal handler
 
while(true)
 
            cout << “Hello World…” << endl;
 
return 0;
 
}

To interrupt in between the program press Ctrl+C.

THE RAISE( ) FUNCTION

In C++, the raise( ) function is used to generate signals. It takes integer signal number as an argument.

SYNTAX FOR THE FOLLOWING IS:

raise(signal sig);

In the above syntax, sig is the signal number which is used to send any of the signals: SIGABRT, SIGINT, SIGTERM, SIGSEGV, SIGILL, SIGHUP, SIGFPE.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
 
#include <csignal>
 
using namespace std;
 
void signal_handler( int signalnum ) {
 
cout << “Interrupt signal is (“ << signalnum << “).\n”;
 
// terminate program
 
exit(signalnum);
 
}
 
int main () {
 
int count = 0;
 
signal(SIGSEGV, signal_handler);
 
// register signal SIGSEGV and signal handler
 
while(++count) {
 
            cout << “Hello World…” << endl;
 
            if( count == 10 )
 
                        raise(SIGSEGV);
 
}
 
return 0;   }