信号是由操作系统传给进程的中断,会提早终止一个程序。在 UNIX、LINUX、Mac OS X 或 Windows 系统上,可以通过按 Ctrl+C 产生中断。
有些信号不能被程序捕获,但是下表所列信号可以在程序中捕获,并可以基于信号采取适当的动作。这些信号是定义在 C++ 头文件 中。
#include <iostream>
#include <csignal>
#include<windows.h>
using namespace std;
void signalHandler(int signum)
{
cout << "Interrupt signal (" << signum << ") received.\n";
// 清理并关闭
// 终止程序
exit(signum);
}
int main()
{
// 注册信号 SIGINT 和信号处理程序
signal(SIGINT, signalHandler);
int i = 0;
try {
while (++i) {
cout << "Going to sleep...." << endl;
if (i == 3)
{
raise(SIGINT);//中断
}
Sleep(1);
}
}
catch (const exception& e) {
// 处理异常,例如清理资源
cout << "Exception occurred: " << e.what() << endl;
// 继续处理其他异常,例如抛出新的异常或退出程序
}
return 0;
}