我正在开发服务器应用程序并想使用 SIGINT 来终止应用程序。虽然我知道 2 种方法,但我不确定哪种方法适合终止此应用程序。
有没有更好的方法来处理 SIGING?
int main() {
startServer();
1. while(true) {sleep(3); if(sigFlag) break;}
2. std::getline(std::cin, param);
stopServer();
return 0;
}
最佳答案
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
bool run = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
{
printf("received SIGINT\n");
run = false;
}
}
int main(void)
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
// A long long wait so that we can easily issue a signal to this process
while(run)
sleep(1);
return 0;
}
来源:http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
使用 return 或 exit 代替 printf
关于c++ - 在 C++ 中使用 SIGINT 杀死应用程序的好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931884/