Tuesday, February 20, 2024

SIGNAL HANDLING IN C++

 C++ signal handling

  •  WHAT IS SIGNAL HANDLING
  • SIGNAL AND DESCRIPTION
  • THE SIGNAL FUNCTIONS
  • THE RAISE FUNCTIONS 

Signals are interrupts sent to a process by the operating system that can terminate the program prematurely. To create breaks, press Ctrl+C on UNIX, LINUX, Mac OS X, or Windows.

 There are signals that the program cannot catch, but the following is a list of signals that you can catch in your program and perform the necessary actions based on the signal. These signals are defined in the C++ header file <csignal>.

 

Sr. No

Signal & Description

1

SIGABRT

Abnormal termination of the program, such as a call to abort.

2

SIGFPE

An erroneous arithmetic operation, such as a divide by zero or an operation resulting in overflow.

3

SIGILL

Detection of an illegal instruction.

4

SIGINT

Receipt of an interactive attention signal.

5

SIGSEGV

An invalid access to storage.

6

SIGTERM

A termination request was sent to the program

 

The signal functions

C++ signal-handling library provides a function signal to trap unexpected events. Following is the syntax of the signal() function.

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

For simplicity, this function accepts two arguments: the first argument is an integer representing the signal number, and the second argument is a pointer to the signal-handling function.

Let's write a simple C++ program where we catch a SIGINT signal with the signal() function. Whatever signal you want to receive in your program, you must register it with the signal function and connect it to the signal handler.

 Let us try this with a C++ program

 #include <iostream>

#include <csignal>

 using namespace std;

 void signal_Handler(int signum )

{

   cout << "Interrupt signal (" << signum << ") received.\n";

 

   // clean up and close up stuff here 

   // terminate program 

 

   exit(signum); 

}

 

int main ()

{

   // register signal SIGINT and signal handler 

   signal(SIGINT, signalHandler); 

 

   while(1) {

      cout << "Going to sleep...." << endl;

      sleep(1);

   }

 

   return 0;

}

 

Output

Going to sleep

Going to sleep

Going to sleep

 

The raise() function

 Signals can be created using the raise() function, which takes an integer signal number as an argument and has the following: -

 Syntax: - int raise (signal sig);
 

Here is the sig signal number to send any signal: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP. The following is an example where we raise the signal internally using the raise() function as follows: -

 #include <iostream>

#include <csignal>

 using namespace std;

 void signalHandler( int signum ) 

{

   cout << "Interrupt signal (" << signum << ") received.\n";

    // clean up and close up stuff here 

   // terminate program 

    exit(signum); 

}

 

int main () 

{

   int i = 0;

   // register signal SIGINT and signal handler 

   signal(SIGINT, signalHandler); 

 

   while(++i) 

{

      cout << "Going to sleep...." << endl;

      if( i == 3 ) 

{

         raise( SIGINT);

      }

      sleep(1);

   }

   return 0;

}

 Output

Going to sleep

Going to sleep

Going to sleep 

No comments:

Post a Comment

Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular