我正在开发一个使用C ++并使用Linux GNU C编译器进行编译的应用程序。但我想在用户使用CtrlC 键中断脚本时调用一个函数。我该怎么办?
发布于 2018-03-01 14:55:53
我提供了一个简单的例子吧,应该对你有帮助:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h> // our new library
volatile sig_atomic_t flag = 0;
void my_function(int sig){ // can be called asynchronously
flag = 1; // set flag
}
int main(){
// Register signals
signal(SIGINT, my_function);
// ^ ^
// Which-Signal |-- which user defined function registered
while(1)
if(flag){ // my action when signal set it 1
printf("\n Signal caught!\n");
printf("\n default action it not termination!\n");
flag = 0;
}
return 0;
}
https://stackoverflow.com/questions/-100007477
复制相似问题