Cplusplus 简明教程
C++ Signal Handling
信号是由操作系统发送到进程中的中断,它可以使程序提前终止。你可以在 UNIX、LINUX、Mac OS X 或 Windows 系统上按下 Ctrl+C 来生成中断。
Signals are the interrupts delivered to a process by the operating system which can terminate a program prematurely. You can generate interrupts by pressing Ctrl+C on a UNIX, LINUX, Mac OS X or Windows system.
程序无法捕获的信号存在,但以下列表中列出的信号可以在程序中捕获,并可以根据信号采取适当的操作。这些信号在 C++ 头文件 <csignal> 中定义。
There are signals which can not be caught by the program but there is a following list of signals which you can catch in your program and can take appropriate actions based on the signal. These signals are defined in 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 sent to the program. |
The signal() Function
C++ 信号处理库提供函数 signal 来捕获意外事件。signal() 函数的语法如下 −
C++ signal-handling library provides function signal to trap unexpected events. Following is the syntax of the signal() function −
void (*signal (int sig, void (*func)(int)))(int);
简单来说,此函数接收两个参数:第一个参数为整数,表示信号号;第二个参数为指向信号处理函数的指针。
Keeping it simple, this function receives two arguments: first argument as an integer which represents signal number and second argument as a pointer to the signal-handling function.
我们编写一个简单的 C++ 程序,在其中将使用 signal() 函数捕获 SIGINT 信号。无论要在程序中捕获哪个信号,您都必须使用 signal 函数注册该信号,并将其与信号处理程序关联起来。查看以下示例 −
Let us write a simple C++ program where we will catch SIGINT signal using signal() function. Whatever signal you want to catch in your program, you must register that signal using signal function and associate it with a signal handler. Examine the following example −
#include <iostream>
#include <csignal>
using namespace std;
void signalHandler( int signum ) {
cout << "Interrupt signal (" << signum << ") received.\n";
// cleanup 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;
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Going to sleep....
Going to sleep....
Going to sleep....
现在,按 Ctrl+c 中断程序,您将看到程序将捕获该信号,并通过打印类似以下内容的方式退出 −
Now, press Ctrl+c to interrupt the program and you will see that your program will catch the signal and would come out by printing something as follows −
Going to sleep....
Going to sleep....
Going to sleep....
Interrupt signal (2) received.
The raise() Function
您可以通过函数 raise() 生成信号,该函数采用整数信号号作为参数,并具有以下语法。
You can generate signals by function raise(), which takes an integer signal number as an argument and has the following syntax.
int raise (signal sig);
在此处, sig 是用于发送任何信号的信号号:SIGINT、SIGABRT、SIGFPE、SIGILL、SIGSEGV、SIGTERM、SIGHUP。以下示例中,我们使用 raise() 函数在内部引发了一个信号,如下所示 −
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 −
#include <iostream>
#include <csignal>
using namespace std;
void signalHandler( int signum ) {
cout << "Interrupt signal (" << signum << ") received.\n";
// cleanup 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;
}
当编译并执行上述代码时,它将生成以下结果并自动退出 −
When the above code is compiled and executed, it produces the following result and would come out automatically −
Going to sleep....
Going to sleep....
Going to sleep....
Interrupt signal (2) received.