Detailed analysis of C++ signal processing

  • 2020-10-23 20:13:45
  • OfStack

A signal is an interruption from the operating system to a process that terminates a program prematurely. On UNIX, LINUX, Mac OS X or Windows systems, interrupts can be generated by pressing Ctrl+C.

Some signals cannot be captured by a program, but the signals listed in the following table can be captured in a program and appropriate actions can be taken based on the signals. These signals are defined in the C++ header file < csignal > In the.

信号 描述
SIGABRT 程序的异常终止,如调用 abort。
SIGFPE 错误的算术运算,比如除以零或导致溢出的操作。
SIGILL 检测非法指令。
SIGINT 程序终止(interrupt)信号。
SIGSEGV 非法访问内存。
SIGTERM 发送到程序的终止请求。

signal () function

The C++ signal processing library provides signal functions to capture emergencies. Here is the syntax for the signal() function:

[

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

]

This function takes two arguments: the first argument is an integer representing the number of the signal; The second argument is a pointer to the signal handler.

Let's write a simple C++ program that USES the signal() function to capture the SIGINT signal. Whatever signal you want to capture in your program, you must use the signal function to register the signal and associate it with the signal handler. Consider the following example:


#include <iostream>
#include <csignal>
#include <unistd.h>
 
using namespace std;
 
void signalHandler( int signum )
{
  cout << "Interrupt signal (" << signum << ") received.\n";
 
  //  Clean up and close 
  //  To terminate the program  
 
  exit(signum); 
 
}
 
int main ()
{
  //  Register signal  SIGINT  And signal handlers 
  signal(SIGINT, signalHandler); 
 
  while(1){
    cout << "Going to sleep...." << endl;
    sleep(1);
  }
 
  return 0;
}

When the above code is compiled and executed, it produces the following results:

[

Going to sleep....
Going to sleep....
Going to sleep....

]

Now, press Ctrl+C to interrupt the program, you will see the program capture signal, the program prints the following and exits:

[

Going to sleep....
Going to sleep....
Going to sleep....
Interrupt signal (2) received.

]

raise () function

You can generate a signal using the function raise(), which takes an integer signal number as an argument, with the syntax as follows:

[

int raise (signal sig);

]

In this case, sig is the number of the signal to be sent, which includes: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP. Here is an example of the signal we generated inside the raise() function:


#include <iostream>
#include <csignal>
#include <unistd.h>
 
using namespace std;
 
void signalHandler( int signum )
{
  cout << "Interrupt signal (" << signum << ") received.\n";
 
  //  Clean up and close 
  //  To terminate the program  
 
  exit(signum); 
 
}
 
int main ()
{
  int i = 0;
  //  Register signal  SIGINT  And signal handlers 
  signal(SIGINT, signalHandler); 
 
  while(++i){
    cout << "Going to sleep...." << endl;
    if( i == 3 ){
     raise( SIGINT);
    }
    sleep(1);
  }
 
  return 0;
}

When the above code is compiled and executed, it produces the following results and exits automatically:

[

Going to sleep....
Going to sleep....
Going to sleep....
Interrupt signal (2) received.

]

The above is the detailed analysis of C++ signal processing content, more information about C++ signal processing please pay attention to other related articles on this site!


Related articles: