停止或启动setitimer

时间:2014-11-19 11:00:31

标签: c linux benchmarking

我已经成功设置了一个计时器,但是我有一个小问题,每当我的警报到期并调用catch_alarm函数时,我希望计时器停止并且仅在返回主函数时重新启动< / strong>。基本上我需要执行一些基准测试(从不同接口接收和发送的数据包),但我不想包括执行该处理所需的时间,因为它不能准确反映真实的Rx和Tx速率

显示代码示例:

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

void catch_alarm (int sig)
{
    //Stop timer here
    printf("CALLING INTERRUPT NOW\n");
    sleep(1);
    signal (sig, catch_alarm);
    //Start it here again
}

/*void waste (void)
{
printf("hello 1\n");
while(1)
{
printf("hello 2\n");
}
printf("hello 3\n");
}*/



struct itimerval old;
struct itimerval new;

int main(void)
{
    signal (SIGALRM, catch_alarm);
    new.it_interval.tv_sec = 1;
    new.it_interval.tv_usec = 500000;
    new.it_value.tv_sec = 1;
    new.it_value.tv_usec = 500000;

    old.it_interval.tv_sec = 0;
    old.it_interval.tv_usec = 0;
    old.it_value.tv_sec = 0;
    old.it_value.tv_sec = 0;


    if (setitimer (ITIMER_REAL, &new, &old) < 0)
        printf("Timer init failed\n");

    else
        printf("Timer init succeeded\n");

    while(1)
    {
        printf("in while loop\n");
        //sleep(10);
        //waste();
        printf("Resuming from sleep\n");
    }
    return 0;
}
P.S:如果这样,sigaction会更适合吗,怎么样?

0 个答案:

没有答案