setitimer定時器的簡單使用

1 函數簡介

#include <sys/time.h>
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 <stdio.h>
#include <sys/time.h>
#include <signal.h>


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 <stdio.h>
#include <sys/time.h>
#include <signal.h>


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 <stdio.h>
#include <sys/time.h>
#include <signal.h>

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!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章