如何重新启动计时器?
我使用以下代码来设置闹钟。
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?
}
I use the following code to set an alarm.
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);
How do I restart the timer after it has exited the handler function and went into the while loop below. Do I need to reinitialize everything or just call setittimer?
while(pause() == -1)
{
// goes in here after handler function.. what needs to go here to restart timer?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦使用
SA_RESETHAND
,您需要再次调用sigaction
和setitimer
。我建议您将其放在一个函数中,这样您就不必编写代码来启动(或重新启动)计时器两次或更多次。Singe you use
SA_RESETHAND
you need to call bothsigaction
andsetitimer
again. I suggest you put it in a function so you don't have to write the code to start (or restart) the timer twice or more.