C++ Signal Handling

By | March 15, 2020

In C++ we have the concept of signals, which are used to interrupt the ongoing task of the Operating System.  Signals are basically messages given to the Operating System by the program to pause a service that is currently running on the operating system.

C++ Signal Handling

In C++ we can use a signal to pause or completely terminate a running program, C++ offers us with various signal here is the list of those:

Signal Description
SIGABRT It can terminate a program abnormally.
SIGFPE It can terminate a program if there is an error in Arithmetic Operations
SIGILL It can be used to find out the invalid instruction
SIGINT It can be used to show the receipt of an active signal or interactive program interrupt signal
SIGTERM It sends a termination request to the program
SIGHUP It shows the report if the user gets disconnected or terminated
SIGQUIT It is used to terminate the process in order to generate a core dump
SIGTRAP It is used to trace Traps
SIGBUS It stands for Bus error and it is used to indicate a signal to access an invalid address.
SIGUSR1 User-Defined Signal 1
SIGUSR2 User-defined signal 2
SIGALRM It indicates the expiration timer of a process
SIGCOUNT This signal is used to make the process continue.
SIGSTOP It is used to stop a process.

We can also generate signals or interrupt in a running program by pressing Ctrl+C on a UNIX, LINUX, Mac OS X or Windows system.

Every signal has a signal number and when we use the signal() function the signal number pass as an argument to the signal handler.

The signal() function

In C++ we have a signal-handling library that provides a function “signal()” that can be used to trap unexpected even.

signal() is a built-in function of the signal-handling library so to use the signal function in our program we need to import signal library by adding the header file <signal.h>

Syntax

signal(signal, signal_handler)

Here the first argument signal must be one of the registered signals that we have mentioned in the above list, and the second argument signal_handler must be a function that handles the signal if there is an interruption. 

Example:

#include <iostream>
#include <signal.h>
using namespace std;

//signal handler function
//it will execute if there is an interruption in the program
//here s_n is the signal number

void s_h_func( int s_n )                                                                                        
{
      cout << "You have press CTRL+C during the program execution "<< s_n << endl;
      exit(s_n);     //this statement will stop the program and terminal it
}

int main () 
{

   signal(SIGABRT, s_h_func);  
   //infinite loop

   while(1<2)
      cout << "Tech Geek Buzz..." << endl;
   return 0;
}

Output

Tech Geek Buzz...
Tech Geek Buzz...
Tech Geek Buzz...
Tech Geek Buzz...
Tech Geek Buzz...
You have press CTRL+C during the program execution 22

Behind the code

In the above program using the while(1<2) statement we have created an infinite loop but when your press CTRL+C during the program run time then, it will interrupt the program and the signal function catch that interrupt and generate a signal number and send it to the signal handler function s_h_func().

The raise function

The raise function is used to generate the signals, and it takes a signal number as an argument.

Syntax:

raise(signal_number);

Example:

#include <iostream>
#include <csignal>
using namespace std;

void s_h_func( int s_n ) 
{
   cout << "Interruption in the program has been encountered wiht signal number: " << s_n;
   exit(s_n); 
}

int main () 
{
   int raise_point = 0;
   signal(SIGINT, s_h_func); 
   while(1) 
   {
      cout << "Tech Geek Buzz...." << endl;
      raise_point+=1;

      if( raise_point == 6 )
        {
         raise( SIGINT);
        }
   }

   return 0;
}

Output

Tech Geek Buzz....
Tech Geek Buzz....
Tech Geek Buzz....
Tech Geek Buzz....
Tech Geek Buzz....
Tech Geek Buzz....
Interruption in the program has been encountered with signal number: 2

Behind the code

In this example, we do not need to press the CTRL+c command to generate an interrupt here the raise() function did that for us.

When the if(raise_point==6) statement get true the raise(SIGINT) function get triggered and it invoke the signal(SIGINT, signalHandler);  function.

Summary

  • Signals are the interrupts delivered to a process by the operating system.
  • To generate an interrupt, we can press CTRL +C during program execution.
  • In C++ we have a signal-handling library that provides a signal() function to trap unexpected events.
  • The Signal-handling library also consists of a function raise() which is used to generate an interrupt.

Leave a Reply

Your email address will not be published. Required fields are marked *