关于GCC的c99版本下SIGALRM错误
出现的问题:
当代码中有两个闹钟或者使用了setitimer()后,当第一个SIGALRM产生后,会打印出 闹钟 或者 Alarm clock。又或者是SIGVTALRM信号,打印 虚拟计时器超时或 Virtual timer expired。再或者是SIGPROF信号,打印 Profiling timer expired
只需要在编译的时候将 -std=c99 去掉
代码:
void signalHandler(int signo)
{
switch (signo){
case SIGALRM:
printf("SIGALRM\n");
break;
}
system("date");
}
int SetiTimer()
{
int ret;
signal(SIGALRM, signalHandler);
struct itimerval new_value;
bzero(&new_value,sizeof(new_value));
new_value.it_value.tv_sec = 1;
new_value.it_value.tv_usec = 0;
new_value.it_interval.tv_sec =1;
new_value.it_interval.tv_usec = 0;
system("date");
ret=setitimer(ITIMER_REAL, &new_value, NULL);
if(ret == -1)perror("setitimer error");
for (int i = 0; i < 4; ++i)
{
printf("ads\n");
}
while(1);
return 0;
}
有时为了方便for循环中定义变量,使用c99版本编译,反而会有这种奇奇怪怪的问题。有没有大佬知道在c99版本下,不会出错的方法。