如何重启计时器?

时间:2012-01-12 08:00:26

标签: c signals system-calls

我使用以下代码设置闹钟。

struct itimerval timer;
struct sigaction sa;

sa.sa_handler = handler;
sa.sa_flags = SA_RESETHAND;
timer.it_value.tv_usec = 0;
timer.it_value.tv_sec = 1;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
sigaction(SIGALRM, &sa, 0);
setitimer(ITIMER_REAL, &timer, 0); 

如何在退出处理程序功能并进入下面的while循环后重新启动计时器。我是否需要重新初始化所有内容或只是调用setittimer?

while(pause() == -1)
{   
    // goes in here after handler function.. what needs to go here to restart timer?
}  

1 个答案:

答案 0 :(得分:1)

感谢您使用SA_RESETHAND,您需要再次拨打sigactionsetitimer。我建议你把它放在一个函数中,这样你就不必编写代码来启动(或重新启动)计时器两次或更多次。