在计时器运行时执行任务

时间:2012-10-12 09:58:05

标签: c linux ubuntu timer

请帮助我,我已经创建了一个计时器,我想在计时器运行时执行特定任务。但我的代码不允许我在计时器运行时运行任务。它只在计时器运行时才会休眠。我的代码是

#include<stdio.h>
#include<unistd.h>
#include<sys/time.h>
#include<signal.h>
#include <time.h>
#define INTERVAL 10


void timerhandler(int signum)
{

printf("\ni m manoj doing some specila task\n");

}

int timer()
{
    int i;
    struct itimerval val;
    struct sigaction action;

    sigemptyset(&action.sa_mask);
    action.sa_handler=&timerhandler;
    action.sa_flags=SA_RESTART;
    if(sigaction(SIGALRM,&action,NULL)==-1)
    {   
        perror("sigaction");
        return 1;
    }


    val.it_interval.tv_sec  = 0;
    val.it_interval.tv_usec = 0;
    val.it_value.tv_sec = 3;
    val.it_value.tv_usec    = 0;

    if(setitimer(ITIMER_REAL,&val,0)==-1)

        perror("alarm");



    else 

    for(;;)
    {
    Sleep(10);
    printf("timer val is  %d", &val.it_value.tv_sec );
    printf("\ni m manoj doing normal task\n");

    return 0;
    }
    return 0;
}

int main()
{
timer();
}

0 个答案:

没有答案