Topic :Signal Handling in C++ Programming Language:

What is Signal?

Signals are the interrupts delivered to a process by the operating system which can Possibly terminate Our program prematurely. We can generate an interrupt by pressing Ctrl+C on the variety of the operating systems such as LINUX, UNIX, Mac OS X or Windows system. Signals are of various types; some could be caught by the program, and some not. Let’s see the list of the signals we can catch in the program and can perform the valid actions based on the signal. to handle signal in c++, we need to include < csignal > header file:



TYPESIGNALNAMEDESCRIPTION
Int (signals)SIGABRTSignal Abort Abnormal termination, such as is initiated by the abort function:
SIGFPESignal Floating-point ExceptionAn erroneous arithmetic operation, such as an operation resulting in overflow or a divide by zero.
SIGILLSignal Illegal InstructionInvalid function image, such as an illegal instruction. This is generally due to a corruption in the code or to an attempt to execute data.
SIGINTSignal Interrupt Interactive attention signal. Generally generated by the application user.
SIGSEGVSignal Segmentation Violation Invalid access to storage: When a program tries to read or write outside the memory it has allocated.
SIGTERMSignal TerminateA request sent to the program for its termination.
function (handlers)SIG_DFLDefault Handling The signal is handled by the default action for that particular signal.
SIG_IGNIgnore SignalIgnored the signal.
SIG_ERRSignal ErrorSpecial return value indicating failure.


Signal() Function:

The signal() function is provided by the C++ signal-handling library for the purpose to trap the unexpected and unwanted events. Let’s check the general form of the signal() function:



void (*signal (int sig, void (*func)(int)))(int);

This function need two arguments. The integer argument specifies the signal number and the pointer argument specifies the signal-handling function.

Let's have an simple example, where we will catch an signal Interrupt (SIGINT) signal using Signal() function. Whatever signal we want to catch in our program, we must register that signal using Signal function and associate it with a signal handler.



/*Example: Signal Handling: InfoBrother:*/
 
#include <iostream>
#include <csignal>//required for signal Handling:
 
using namespace std;
 
void signalHandler( int signum )
{
   cout << "Interrupt signal (" << signum << ") received.\n";
 
   // cleanup and close up stuff here 
   // terminate program 
 
   exit(signum); 
}
 
main () 
{
   // register signal SIGINT and signal handler 
   signal(SIGINT, signalHandler); 
 
   while(1)
   {
      cout << "Sleeping...." << endl;
      sleep(1);
   }
 
   return 0;
}

When we compile and execute our Program, it work Normally. then we press Ctrl+C to interrupt the execution of program. our program catch the signal and came out of the loop.



raise() Function

We can generate Signals by the raise() functions. This function need an argument, which is an integer signal number.


int raise (signal sig);


Here, sig is the signal number to send any of the signals: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP. Following is the example where we raise a signal internally using raise() function as follows:


/*Example: raise() Function : InfoBrother*/
 
#include <iostream>
#include <csignal>//required for signal handling:
 
using namespace std;
 
void signalHandler( int signum ) 
{
   cout << "Interrupt signal (" << signum << ") received.\n";
 
   // cleanup and close up stuff here 
   // terminate program 
 
   exit(signum); 
}
 
main () 
{
   int i = 0;
   // register signal SIGINT and signal handler 
   signal(SIGINT, signalHandler); 
 
   while(++i)
   {
      cout << "Sleeping...." << endl;
 
      if( i == 3 )
      {
         raise( SIGINT);
      }
 
      sleep(1);
   }
 
   return 0;
}






I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement