C++ - SPLessons

C++ Signal Handler

Chapter 27

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Signal Handler

C++ Signal Handler

shape Introduction

If a programs causes any problem during execution, signals are sent and the operating system terminates the programs. The program does not rectify most of the error causing signals. But, single header file includes all these Signal Header file and precautions are applied as per the signal occurred. The header file to be included in C++ is <csignal>.

The "CPP Signal Handler" clears the generated signals. It provides an alternative solution to the problem or terminates the entire program.

shape Signals

Signal Description
SIGFPE Error in mathematical calculations
SIGABRT Terminates the program abnormally
SIGINT Occurrence of interaction signal
SIGILL Implies instruction given is illegal
SIGTERM Request terminated
SIGSEGV The storage accessed is invalid

The above six signals are the basic csignal header files.

shape Syntax

void (*signal (int signal_name , type (*function)(data_type)))(data_type);

The above syntax implies a signal function with two arguments in which the
first argument is the number of signal and
second denotes the pointer to handler of signal occurred.

shape Example

#include <iostream>
#include <csignal>

using namespace std;

void sig_handler( int signal_number )
{
    cout << "Signal interrupt" << signal_number << " occurred" << endl;
 
//Terminates the program as signal occurred
   exit(signal_number);  

}

int main ()
{
    // register signal SIGINT and signal handler  
    signal(SIGILL, sig_handler);  

    while(1)
    {
       cout << "Shutting down...!! " << endl;
    }

    return 0;
}

Output

Shutting down...!!
Shutting down...!!
Shutting down...!!
.
.
.
.

raise() function

shape Description

The other function called Raise can be used to create and implement these signals. It sends signal sig to the current executing program. If none of the function handlers defines signal to handle the raised signal, the function never throws exceptions.

shape Syntax

int raise (signal signal_number);

shape Example

#include <iostream>
#include <csignal>

using namespace std;

void sig_handler( int signal_number )
{
    cout << "Signal interrupt" << signal_number << " has occured"; 
    exit(signal_number);  

}

int main ()
{
    int x = 0;
    // registration of signal SIGILL and signal handler  
    signal(SIGILL, sig_handler);  

    while(++x)
    {
       cout << "Shutting down...!!" << endl;
       
       if( x == 10 )
       {
          raise( SIGINT);
       }
     
    }

    return 0;
}

Output

Shutting down...!!
Shutting down...!!
Shutting down...!!
Shutting down...!!
Shutting down...!!
Shutting down...!!
Shutting down...!!
Shutting down...!!
Shutting down...!!
Shutting down...!!

Summary

shape Key Points

  • CPP Signal Handler can be used by placing <csignal> header file
  • Raise function helps to create the error handling signals

shape Programming
Tips

The other way to abort the flow of program is to press Ctrl+C.

When these two keys are pressed, the proper Signal Handler is used to generate SIGNIT signal and abort.