【发布时间】:2021-09-07 01:06:04
【问题描述】:
我有一个简单的代码循环“信号很酷”这个词接收“SIGINT”信号我如何添加更多信号以及如何控制我的程序在用户触发信号时显示的内容。
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#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 << "SIGNALS ARE COOL" << endl;
}
return 0;
}
【问题讨论】:
-
信号处理程序非常受限于它可以/不能做什么。例如,它不能执行大多数 I/O(它可以执行
read或write)或内存分配/释放。请参阅man 2 sigaction了解可以执行的操作列表。
标签: c++ signals sigint signal-handling