欢迎访问小站,阅读原文http://www.yandong.org/archives/764
使程序接收到ctrl+c就终止运行
int stop_loop;/*处理ctrl+c信号*/void sigint_handler(int sig_number){
static int been_here = 0;
printf("Handling sigint %d\n", been_here);
stop_loop = 1;
if( been_here > 0)
{
printf(" not been_here >0 %d\n ", been_here);
}
been_here++;}/*信号设置,ctrl+c结束本程序*/void setup_signal_handler(){
struct sigaction sigint_action;
sigint_action.sa_handler = sigint_handler;
sigemptyset(&sigint_action.sa_mask);
sigint_action.sa_flags=0;
sigaction(SIGINT, &sigint_action, NULL);}int main(){
int i=0;
stop_loop=0;
setup_signal_handler();
while(!stop_loop)
{
printf("%d\n",i);
i++;
if(stop_loop >0)
{
printf("stop_loop is %d i=%d\n",stop_loop, i);
break;
}
sleep(1);
}
printf("stop_loop is %d i=%d\n",stop_loop, i);
return 1;}