Linux下利用setitimer()实现简单的定时器

Linux下利用setitimer()实现简单的定时器

一、setitimer()函数的使用

通过man手册可以查看详细的用法 见文末

1、头文件
#include 
2、函数原型
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value,
                     struct itimerval *old_value);
3、函数使用

(1)which
which参数表示类型,可选的值有:
ITIMER_REAL:以系统真实的时间来计算,它送出SIGALRM信号。
ITIMER_VIRTUAL:以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号。
ITIMER_PROF:以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF信号
(2)时间结构体

struct itimerval 
{
	/* Interval for periodic timer */
	struct timeval it_interval;
	/* Time until next expiration */ 
	struct timeval it_value;    
};
struct timeval 
{
	time_t      tv_sec;         /* seconds */
	suseconds_t tv_usec;        /* microseconds 1/1000000 seconds */
};

new_value: 参数用来对计时器进行设置,it_interval为计时间隔,it_value为延时时长,下面例子中表示的是在setitimer方法调用成功后,延时it_value后触发一次SIGALRM信号,以后每隔it_interval触发一次SIGALRM信号。

settimer工作机制是,先对it_value倒计时,当it_value为零时触发信号,然后重置为it_interval,继续对it_value倒计时,一直这样循环下去。
基于此机制,setitimer既可以用来延时执行,也可定时执行。
假如it_value为0是不会触发信号的,所以要能触发信号,it_value得大于0;如果it_interval为零,只会延时,不会定时(也就是说只会触发一次信号)。
old_value: 参数,通常用不上,设置为NULL,它是用来存储上一次setitimer调用时设置的new_value值。

二、基于setitimer()的Linux定时器的实现

以下是简单的定时器实现

1、头文件
#ifndef  _LINUX_TIMER_H_
#define  _LINUX_TIMER_H_
#include 
#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include 
//定时器精度 100ms
typedef struct TimerEvent
{
    unsigned long long periodic_time;
    void (*ExecuFunc)(void *);
}TimerEvent;
int InitTimer();
int AddTimer(TimerEvent * _pst_timer_event);
int DeleteTimer(TimerEvent * _pst_timer_target);
#endif
2、源文件
#include "linux_timer.h"
using namespace std;
static std::list <TimerEvent> list_timer_event;
static unsigned long long g_timer_expires = 0;
static void TimerSchedule(int signo);
int InitTimer()
{
    struct itimerval value_new;
    signal(SIGALRM,TimerSchedule);
    value_new.it_value.tv_sec = 1;  // 一秒之后启动定时器
    value_new.it_value.tv_usec = 0;
    value_new.it_interval.tv_sec = 0;
    value_new.it_interval.tv_usec = 100000;
    setitimer(ITIMER_REAL, &value_new, NULL);
}
int AddTimer(TimerEvent * _pst_timer_event)
{
    if(_pst_timer_event != nullptr)
    {
        list_timer_event.push_back(* _pst_timer_event);
    }
}
int DeleteTimer(TimerEvent * _pst_timer_target)
{
    for(auto it_timer_event = list_timer_event.begin();
                it_timer_event != list_timer_event.end();it_timer_event++)
    {
        if(it_timer_event->periodic_time == _pst_timer_target->periodic_time && 
            it_timer_event->ExecuFunc == _pst_timer_target->ExecuFunc)
        {
            list_timer_event.erase(it_timer_event);
        }
    }
}
static void ThrExecuFunc(std::list<TimerEvent>::iterator _it_timer_event)
{
    _it_timer_event->ExecuFunc(NULL);
}
static void TimerSchedule(int signo)
{
    g_timer_expires++;
    for(auto it_timer_event = list_timer_event.begin();it_timer_event != list_timer_event.end();it_timer_event++)
    {
        if(it_timer_event->periodic_time && (g_timer_expires % it_timer_event->periodic_time) == 0
            && it_timer_event->ExecuFunc)
        {
            thread thr_execu_func(ThrExecuFunc,it_timer_event);
            thr_execu_func.detach();
        }
    }
}

setitimer() man手册全部

http://man7.org/linux/man-pages/

GETITIMER(2)                                        Linux Programmer's Manual                                       GETITIMER(2)

NAME
       getitimer, setitimer - get or set value of an interval timer

SYNOPSIS
       #include 

       int getitimer(int which, struct itimerval *curr_value);
       int setitimer(int which, const struct itimerval *new_value,
                     struct itimerval *old_value);

DESCRIPTION
       These  system calls provide access to interval timers, that is, timers that initially expire at some point in the future,
       and (optionally) at regular intervals after that.  When a timer expires, a signal is generated for the  calling  process,
       and the timer is reset to the specified interval (if the interval is nonzero).

       Three  types  of timers—specified via the which argument—are provided, each of which counts against a different clock and
       generates a different signal on timer expiration:

       ITIMER_REAL    This timer counts down in real (i.e., wall clock) time.  At each expiration, a SIGALRM  signal  is  gener‐
                      ated.

       ITIMER_VIRTUAL This  timer counts down against the user-mode CPU time consumed by the process.  (The measurement includes
                      CPU time consumed by all threads in the process.)  At each expiration, a SIGVTALRM signal is generated.

       ITIMER_PROF    This timer counts down against the total (i.e., both user and system) CPU time consumed  by  the  process.
                      (The measurement includes CPU time consumed by all threads in the process.)  At each expiration, a SIGPROF
                      signal is generated.

                      In conjunction with ITIMER_VIRTUAL, this timer can be used to profile user and system CPU time consumed by
                      the process.

       A process has only one of each of the three types of timers.

       Timer values are defined by the following structures:

           struct itimerval {
               struct timeval it_interval; /* Interval for periodic timer */
               struct timeval it_value;    /* Time until next expiration */
           };

           struct timeval {
               time_t      tv_sec;         /* seconds */
               suseconds_t tv_usec;        /* microseconds */
           };

   getitimer()
       The function getitimer() places the current value of the timer specified by which in the buffer pointed to by curr_value.

       The  it_value  substructure  is  populated  with  the amount of time remaining until the next expiration of the specified
       timer.  This value changes as the timer counts down, and will be reset to it_interval when the timer  expires.   If  both
       fields of it_value are zero, then this timer is currently disarmed (inactive).

       The  it_interval substructure is populated with the timer interval.  If both fields of it_interval are zero, then this is
       a single-shot timer (i.e., it expires just once).

   setitimer()
       The function setitimer() arms or disarms the timer specified by which, by setting the timer to  the  value  specified  by
       new_value.   If  old_value  is non-NULL, the buffer it points to is used to return the previous value of the timer (i.e.,
       the same information that is returned by getitimer()).

       If either field in new_value.it_value is nonzero, then the timer is armed to initially expire at the specified time.   If
       both fields in new_value.it_value are zero, then the timer is disarmed.

       The new_value.it_interval field specifies the new interval for the timer; if both of its subfields are zero, the timer is
       single-shot.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS
       EFAULT new_value, old_value, or curr_value is not valid a pointer.

       EINVAL which is not one of ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF; or (since Linux 2.6.22) one of the tv_usec fields
              in the structure pointed to by new_value contains a value outside the range 0 to 999999.

CONFORMING TO
       POSIX.1-2001,  SVr4,  4.4BSD  (this call first appeared in 4.2BSD).  POSIX.1-2008 marks getitimer() and setitimer() obso‐
       lete, recommending the use of the POSIX timers API (timer_gettime(2), timer_settime(2), etc.) instead.

NOTES
       Timers will never expire before the requested time, but may expire some (short) time afterward, which depends on the sys‐
       tem  timer resolution and on the system load; see time(7).  (But see BUGS below.)  If the timer expires while the process
       is active (always true for ITIMER_VIRTUAL), the signal will be delivered immediately when generated.

       A child created via fork(2) does not inherit its parent's interval timers.   Interval  timers  are  preserved  across  an
       execve(2).

       POSIX.1  leaves  the  interaction between setitimer() and the three interfaces alarm(2), sleep(3), and usleep(3) unspeci‐
       fied.

       The standards are silent on the meaning of the call:

           setitimer(which, NULL, &old_value);

       Many systems (Solaris, the BSDs, and perhaps others) treat this as equivalent to:

           getitimer(which, &old_value);

       In Linux, this is treated as being equivalent to a call in which the new_value fields are zero; that  is,  the  timer  is
       disabled.  Don't use this Linux misfeature: it is nonportable and unnecessary.

BUGS
       The  generation  and  delivery of a signal are distinct, and only one instance of each of the signals listed above may be
       pending for a process.  Under very heavy loading, an ITIMER_REAL timer may expire before the signal from a previous expi‐
       ration has been delivered.  The second signal in such an event will be lost.

       On  Linux  kernels before 2.6.16, timer values are represented in jiffies.  If a request is made set a timer with a value
       whose jiffies representation exceeds MAX_SEC_IN_JIFFIES (defined in include/linux/jiffies.h), then the timer is  silently
       truncated  to  this  ceiling  value.  On Linux/i386 (where, since Linux 2.6.13, the default jiffy is 0.004 seconds), this
       means that the ceiling value for a timer is approximately 99.42 days.  Since Linux 2.6.16, the kernel  uses  a  different
       internal representation for times, and this ceiling is removed.

       On  certain  systems  (including i386), Linux kernels before version 2.6.12 have a bug which will produce premature timer
       expirations of up to one jiffy under some circumstances.  This bug is fixed in kernel 2.6.12.

       POSIX.1-2001 says that setitimer() should fail if a tv_usec value is specified that is outside of the range 0 to  999999.
       However,  in  kernels  up  to and including 2.6.21, Linux does not give an error, but instead silently adjusts the corre‐
       sponding seconds value for the timer.  From kernel 2.6.22 onward, this nonconformance  has  been  repaired:  an  improper
       tv_usec value results in an EINVAL error.

SEE ALSO
       gettimeofday(2), sigaction(2), signal(2), timer_create(2), timerfd_create(2), time(7)

COLOPHON
       This  page  is  part  of  release  4.15  of the Linux man-pages project.  A description of the project, information about
       reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.

Linux                                                      2017-09-15                                               GETITIMER(2)

参考博文 https://blog.csdn.net/lixianlin/article/details/25604779

你可能感兴趣的:(Linux)