新手问个信号的问题??
一个信号处理的问题,如果收到SIGINT信号,就改变一个变量的值,然后退出while循环,可是一运行的时候没反应,且无论怎么按Ctrl+C 都退不出来,并且也没有任何东西打印出来,包括while循环中的那条printf语句也没有输出,不知怎么回事?
我是在windows系统上用FTerm软件登陆Unix主机的,是否这个有限制?
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <math.h>
static volatile sig_atomic_t doneflag = 0;
static void setdoneflag(int signo)
{
fprintf(stderr, "Set done flag!\n");
doneflag = 1;
}
int main(void)
{
struct sigaction act;
int count = 0;
double sum = 0;
double x;
act.sa_handler = setdoneflag;
act.sa_flags = 0;
if((sigemptyset(&act.sa_mask) == -1) || (sigaction(SIGINT, &act, NULL) == -1))
{
perror("Failed to set SIGINT handler");
return 1;
}
while(!doneflag)
{
x = (rand() +0.5)/(RAND_MAX+1.0);
sum += sin(x);
printf("Count is %d and ave = %lf\n", count, sum/count);
}
printf("Program terminating...\n");
if(count == 0)
printf("No value counted\n");
else
printf("Count is %d and ave = %lf\n", count, sum/count);
return 0;
}