C : 如何暂停 sigaction Timer [英] C : How to pause sigaction Timer

查看:62
本文介绍了C : 如何暂停 sigaction Timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个函数叫 test(),我想每 30 秒调用一次这个函数,请找到我实现的代码片段.

There is one function called test(), I want to call this function in every 30 seconds, Please find my implemented code snippet.

void init_sigaction(void) {
    struct sigaction act;    
    act.sa_handler = test; //test()
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGPROF, &act, NULL);
}


void init_time(void) {
    struct itimerval val;
    val.it_value.tv_sec = 30; //Timer 30 Seconds
    val.it_value.tv_usec = 0;
    val.it_interval = val.it_value;
    setitimer(ITIMER_PROF, &val, NULL);
}

int main()
{
  /*Set the handler for the signal SIG to HANDLER */
    signal(SIGINT, signal_handler);
    init_sigaction();
    init_time();

    Some_other_function();

}

现在我正在使用其他一些函数,我想暂停 sigaction 计时器,直到其他函数执行为止.如何实现暂停中断?

Now I am using some other function, and I want to pause sigaction timer until other function's execution. how can I implemented interrupt for pause?

谢谢,

推荐答案

来自手册页setitimer 的一个>:

From the manual page of setitimer:

设置为零的计时器(it_value 为零或计时器到期且 it_interval 为零)停止.

A timer which is set to zero (it_value is zero or the timer expires and it_interval is zero) stops.

使用零次调用 setitimer,并使用有效的 old_value 参数来存储计时器的当前值,以便您稍后再次启动它.

Call setitimer with zero times, and with a valid old_value argument to store the current values of the timer, so you can start it again later.

这样的事情怎么样:

struct itimerval old_timer;

void pause_timer()
{
    struct itimerval zero_timer = { 0 };
    setitimer(ITIMER_PROF, &zero_time, &old_timer);
}

void resume_timer()
{
    setitimer(ITIMER_PROF, &old_timer, NULL);
}

注意以上代码未经测试,仅通过阅读手册页进行编码.

Note The code above is untested, and coded only by reading the manual page.

这篇关于C : 如何暂停 sigaction Timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆