HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Signal is passed by the operating system, the process is interrupted, will early termination of a program. On UNIX, LINUX, Mac OS X or Windows systems, you can press Ctrl + C interrupt.
Some signals can not be caught procedures, but listed in the table below the signal can be captured in the program, and can take appropriate action based on the signal. These signals are defined in the C ++ header file <csignal> in.
信号 | 描述 |
---|---|
SIGABRT | 程序的异常终止,如调用abort。 |
SIGFPE | 错误的算术运算,比如除以零或导致溢出的操作。 |
SIGILL | 检测非法指令。 |
SIGINT | 接收到交互注意信号。 |
SIGSEGV | 非法访问内存。 |
SIGTERM | 发送到程序的终止请求。 |
C ++ library providessignal signal processing function to capture emergencies.The following is the syntax signal () function:
void (*signal (int sig, void (*func)(int)))(int);
This function accepts two parameters: the first parameter is an integer representing the number of a signal; second parameter is a pointer to a signal processing function.
Let's write a simple C ++ program that uses the signal () function to capture SIGINT signal. No matter what you want to capture the signal in your program, you must use thesignal function to register the signal, and the signal processing associated with the program.Consider the following examples:
#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/en/en" << endl; sleep(1); } return 0; }
When the above code is compiled and executed, it produces the following results:
Going to sleep/en/en Going to sleep/en/en Going to sleep/en/en
Now, press Ctrl + C to interrupt the program, you'll see the program signal capture, and exit the program to print the following:
Going to sleep/en/en Going to sleep/en/en Going to sleep/en/en Interrupt signal (2) received.
You can use the functionraise () generates a signal, the signal function takes an integer number as a parameter, the following syntax:
int raise (signal sig);
Here, sig is the signal to be transmitted numbers These signals include: SIGINT, SIGABRT, SIGFPE, SIGILL , SIGSEGV, SIGTERM, SIGHUP.Here's what we use the raise () function generates a signal within an instance:
#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/en/en" << endl; if( i == 3 ){ raise( SIGINT); } sleep(1); } return 0; }
When the above code is compiled and executed, it produces the following results, and will automatically exit:
Going to sleep/en/en Going to sleep/en/en Going to sleep/en/en Interrupt signal (2) received.