setitimer定时器的简单使用

1 函数简介

#include 
int setitimer(int which, const struct itimerval *new_value,
                     struct itimerval *old_value);

The  system  provides  each  process  with  three  interval timers, each decrementing in a distinct time
       domain.  When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.
       ITIMER_REAL    decrements in real time, and delivers SIGALRM upon expiration.
       ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.
       ITIMER_PROF    decrements both when the process executes and when the system is executing on  behalf  of the process.  Coupled with ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application in user and kernel space.  SIGPROF is delivered upon expiration.
Timer values are defined by the following structures:

struct itimerval {
               struct timeval it_interval; /* next value */
               struct timeval it_value;    /* current value */
           };
           struct timeval {
               time_t      tv_sec;         /* seconds */
               suseconds_t tv_usec;        /* microseconds */
           };
上面是linux的man手册中对于该函数的介绍。我们主要使用ITIMER_REAL,用该函数实现单片机中定时器的效果。
Timers decrement from it_value to zero, generate a signal, and reset to it_interval.  A timer  which  is set to zero (it_value is zero or the timer expires and it_interval is zero) stops.

该函数的工作原理如上面介绍的,进程运行后,it_value值按照实际的时间进行递减,直到减为0,则产生SIGALRM信号。然后,把it_interval的值放入it_value中,重新开始递减,如此循环。达到单片机中定时器的效果。

如果it_value值一开始就是0,则定时器不会启动,不论it_interval是多少。

如果it_value值不为0,it_interval为0,则定时器定时到it_value指定的时间就结束,不会循环。

2 实例

2.1 it_value和it_interval均不为0

#include 
#include 
#include 


void alarm_fun()
{   
    printf("hello world!\n");
}
    
int set_timer()
{   
    struct itimerval value;
    
    signal(SIGALRM, alarm_fun);
    
    value.it_value.tv_sec = 1;
    value.it_value.tv_usec = 0;
    value.it_interval.tv_sec = 1;
    value.it_interval.tv_usec = 0;
    setitimer(ITIMER_REAL, &value, NULL);
    
    return 0;
}   
   
int main()
{  
    set_timer();
    while(1);
    return 0;
}

执行结果:每隔1S输出一次

hello world!
hello world!
hello world!
hello world!
hello world!
hello world!

2.2 it_value为0

#include 
#include 
#include 


void alarm_fun()
{   
    printf("hello world!\n");
}
    
int set_timer()
{   
    struct itimerval value;
    
    signal(SIGALRM, alarm_fun);
    
    value.it_value.tv_sec = 0;
    value.it_value.tv_usec = 0;
    value.it_interval.tv_sec = 1;
    value.it_interval.tv_usec = 0;
    setitimer(ITIMER_REAL, &value, NULL);
    
    return 0;
}   
   
int main()
{  
    set_timer();
    while(1);

    return 0;
}
执行结果:不会有输出

2.2 it_value不为0,it_interval为0

#include 
#include 
#include 

void alarm_fun()
{   
    printf("hello world!\n");
}
    
int set_timer()
{   
    struct itimerval value;
    
    signal(SIGALRM, alarm_fun);
    
    value.it_value.tv_sec = 1;
    value.it_value.tv_usec = 0;
    value.it_interval.tv_sec = 0;
    value.it_interval.tv_usec = 0;
    setitimer(ITIMER_REAL, &value, NULL);
    
    return 0;
}   
   
int main()
{  
    set_timer();

    while(1);

    return 0;
}
 执行结果:只输出一次

hello world!


你可能感兴趣的:(Linux编程)