C++ | C++ 信号处理
C++ 信号处理
信号是由操作系统传给进程的中断,会提早终止一个程序。在 UNIX、LINUX、Mac OS X 或 Windows 系统上,可以通过按 Ctrl+C
产生中断。
有些信号不能被程序捕获,但是下表所列信号可以在程序中捕获,并可以基于信号采取适当的动作。
这些信号是定义在 C++ 头文件 <csignal
> 中。
信号 | 描述 |
---|---|
SIGABRT | 程序的异常终止,如调用 abort。 |
SIGFPE | 错误的算术运算,比如除以零或导致溢出的操作。 |
SIGILL | 检测非法指令。 |
SIGINT | 程序终止(interrupt)信号。 |
SIGSEGV | 非法访问内存。 |
SIGTERM | 发送到程序的终止请求。 |
signal()
函数
C++ 信号处理库提供了 signal
函数,用来捕获突发事件。
SIGNAL(2) Linux Programmer's Manual SIGNAL(2)
NAME
signal - ANSI C signal handling
SYNOPSIS
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
void (*signal (int sig, void (*func)(int)))(int);
signal(registered signal, signal handler)
实例1:
/*******************************************************************
* > File Name: signal.cpp
* > Create Time: 2021年09月21日 12:38:10
******************************************************************/
#include <iostream>
#include <csignal>
#include <unistd.h>
using namespace std;
void signalHandler(int signum)
{
cout << "Interrupt signal :" << signum << ", received.\n" ;
exit(signum);
}
int main(int argc, char* argv[])
{
static unsigned int count = 0;
signal(SIGINT, signalHandler); /* 注册信号和信号处理程序 */
for(;;){
cout << "Going to sleep " << count << "..." << endl;
sleep(1);
count ++;
}
return 0;
}
编译、运行(使用Ctrl + C
结束运行):
PS D:\study\cplusplus\day8> make
g++ -o signal signal.cpp -g -Wall
PS D:\study\cplusplus\day8> .\signal.exe
Going to sleep 0...
Going to sleep 1...
Going to sleep 2...
Going to sleep 3...
Interrupt signal :2, received.
raise()
函数
使用函数 raise()
生成信号。
RAISE(3) Linux Programmer's Manual RAISE(3)
NAME
raise - send a signal to the caller
SYNOPSIS
#include <signal.h>
int raise(int sig);
DESCRIPTION
The raise() function sends a signal to the calling process or thread. In a single-threaded program it is
equivalent to
kill(getpid(), sig);
In a multithreaded program it is equivalent to
pthread_kill(pthread_self(), sig);
If the signal causes a handler to be called, raise() will return only after the signal handler has returned.
RETURN VALUE
raise() returns 0 on success, and nonzero for failure.
实例2:
/*******************************************************************
* > File Name: raise.cpp
* > Author: fly
* > Mail: 1358326274@qq.com
* > Create Time: 2021年09月21日 12:48:24
******************************************************************/
#include <iostream>
#include <csignal>
#include <unistd.h>
using namespace std;
void signalHandler(int signum)
{
cout << "Interrupt signal (" << signum << ") recevied.\n";
exit(signum);
}
int main(int argc, char* argv[])
{
int i = 0;
signal(SIGINT, signalHandler);
while(++i){
cout << "Going to sleep "<< i <<"..." << endl;
if(i == 3){
raise(SIGINT);
}
sleep(1);
}
return 0;
}
编译、运行:
PS D:\study\cplusplus\day8> make
g++ -o raise raise.cpp -g -Wall
PS D:\study\cplusplus\day8> .\raise.exe
Going to sleep 1...
Going to sleep 2...
Going to sleep 3...
Interrupt signal (2) recevied.
笔记:
Sleep
函数
功能:执行挂起一段时间,也就是等待一段时间在继续执行
- (1)
Sleep
是区分大小写的,有的编译器是大写,有的是小写。 - (2)
Sleep
括号里的时间,在 Windows 下是以毫秒
为单位,而 Linux 是以秒
为单位。 - (3)Linux 用
#include <unistd.h>
和sleep()
,Windos 用#include <windows.h>
和Sleep()
。