C++ signal processing
A signal is an interrupt passed to the process by the operating system, which will terminate a program early. On UNIX, LINUX, Mac OS X, or Windows systems, you can generate an interrupt by pressing Ctrl+C.
Some signals cannot be captured by the program, but the signals listed in the following table can be captured by the program and appropriate actions can be taken based on the signals. These signals are defined in the C++ header file <csignal>.
Signal | Description |
---|---|
SIGABRT | Abnormal termination of the program, such as calling abort. |
SIGFPE | Incorrect arithmetic operation, such as dividing by zero or causing an overflow. |
SIGILL | Detect illegal instructions. |
SIGINT | Interactive attention signal received. |
SIGSEGV | Illegal access to memory. |
SIGTERM | Termination request sent to the program. |
signal() function
The C++ signal processing library provides the signal function to capture unexpected events. The following is the syntax of the signal() function:
void (*signal (int sig, void (*func)(int)))(int);
This function receives two parameters: the first parameter is an integer, representing the number of the signal; the second parameter is a pointer to the signal processing function.
Let us write a simple C++ program to capture the SIGINT signal using the signal() function. No matter what signal you want to catch in your program, you must use the signal function to register the signal and associate it with a signal handler. Take a look at the following example:
#include <iostream> #include <csignal> using namespace std; void signalHandler( int signum ) { cout << "Interrupt signal (" << signum << ") received.\n"; // 清理并关闭 // 终止程序 exit(signum); } int main () { // 注册信号 SIGINT 和信号处理程序 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 and you will see The program catches the 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 The signal number is used as a parameter, and the syntax is as follows:
int raise (signal sig);
Here, sig is the number of the signal to be sent. These signals include: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP . The following is an example where we use the raise() function to generate a signal internally:
#include <iostream> #include <csignal> using namespace std; void signalHandler( int signum ) { cout << "Interrupt signal (" << signum << ") received.\n"; // 清理并关闭 // 终止程序 exit(signum); } int main () { int i = 0; // 注册信号 SIGINT 和信号处理程序 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 will produce the following results and will automatically exit:
Going to sleep.... Going to sleep.... Going to sleep.... Interrupt signal (2) received.