如何重新启动计时器?

发布于 2024-12-26 03:03:37 字数 548 浏览 2 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2025-01-02 03:03:37

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

Singe you use SA_RESETHAND you need to call both sigaction and setitimer 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文