仪陇家园分类信息网、仪陇生活网、仪陇家园网

搜索

定时器timerfd

[复制链接]
seo 发表于 2022-5-31 13:30:41 | 显示全部楼层 |阅读模式
定时器timerfd发布时间:2022/5/31 13:13:23
            
                                                       
                                                       
            
        
        
               
                    1.为什么要加入此定时器接口
linux2.6.25版本新增了timerfd这个供用户程序使用的定时接口,这个接口基于文件描述符,当超时事件发生时,该文件描述符就变为可读。我首次接触这个新特性是在muduo网络库的定时器里看到的,那么新增一个这样的定时器接口有什么意义呢?

要说明这个问题我得先给大家列举一下Linux下能实现定时功能的各个接口,然后通过逐一比较来说明原因

linux下的定时接口主要有如下几种

[ol]
  • .sleep()
  • .alarm()
  • .usleep()
  • .nanosleep()
  • .clock_nanosleep()
  • .getitimer()/setitimer()
  • .timer_create()/timer_settime/timer_gettime()/timer_delete()
  • .timerfd_create()/timerfd_gettime()/timer_settime()
    [/ol]
    以上便是Linux下常用的一些定时接口
    1.前三种sleep()/alarm()/usleep()在实现时可能用了SIGALRM信号,在多线程中使用信号是相当麻烦的
    2.nanosleep()/clock_nanosleep()会让线程挂起,这样会使程序失去响应,多线程网络编程中我们应该避免这样做
    3.getitimer()/timer_cteate()也是用信号来deliver超时

    而我们的timerfd_create()把时间变成了一个文件描述符,该文件描述符会在超时时变得可读,这种特性可以使我们在写服务器程序时,很方便的便把定时事件变成和其他I/O事件一样的处理方式,并且此定时接口的精度也足够的高,所以我们只要以后在写I/O框架时用到了定时器就该首选timerfd_create()

    2.timerfd的接口介绍
    (1)timerfd的创建

      
    [ol]
  • int timer_create(int clockid,int flags);
  • //成功返回0
    [/ol]
      
    第一个参数一般为CLOCK_REALTIME或者CLOCK_MONOTONIC,其参数意义为参数意义

    CLOCK_REALTIME:相对时间,从1970.1.1到目前时间,之所以说其为相对时间,是因为我们只要改变当前系统的时间,从1970.1.1到当前时间就会发生变化,所以说其为相对时间

    CLOCK_MONOTONIC:与CLOCK_REALTIME相反,它是以绝对时间为准,获取的时间为系统最近一次重启到现在的时间,更该系统时间对其没影响

    第二个参数为控制标志:TFD_NONBLOCK(非阻塞),TFD_CLOEXEC(同O_CLOEXEC)

    (2)定时器的设置

      
    [ol]
  • int timerfd_settime(int fd,int flags
  • const struct itimerspec *new_value
  • struct itimerspec *old_value);
  • //成功返回0
    [/ol]
      
    该函数的功能为启动和停止定时器,第一个参数fd为上面的timerfd_create()函数返回的定时器文件描述符,第二个参数flags为0表示相对定时器,为TFD_TIMER_ABSTIME表示绝对定时器,第三个参数new_value用来设置超时时间,为0表示停止定时器,第四个参数为原来的超时时间,一般设为NULL

    需要注意的是我们可以通过clock_gettime获取当前时间,如果是绝对定时器,那么我们得获取1970.1.1到当前时间(CLOCK_REALTIME),在加上我们自己定的定时时间。若是相对定时,则要获取我们系统本次开机到目前的时间加我们要定的时常(即获取CLOCK_MONOTONIC时间)

    上述参数中itimerspec的结构定义如下

      
    [ol]
  • struct itimerspec {
  • struct timespec it_interval; /* Interval for periodic timer */
  • struct timespec it_value; /* Initial expiration */
  • };
    [/ol]
      
    其中it_value保存首次超时时间值,即在哪个时间点超时的那个时间的值,it_interval为后续周期性超时的时间间隔,注意是时间间隔不是时间值啦
    timespec的结构定义如下

      
    [ol]
  • struct timespec {
  • time_t tv_sec; /* Seconds */
  • long tv_nsec; /* Nanoseconds */
  • };
    [/ol]
      
    需要注意的是当设置定时器后,我们就可以用read读取定时器的文件描述符了,当其可读时,就是超时发生的时间,下面的实例中给出用法,请读者仔细体会

    3.具体实例
    以绝对超时为例

      
    [ol]
  • #include
  • #include
  • #include
  • #include
  • #include
  • #include  /* Definition of uint64_t */
  • #define handle_error(msg) \
  • do { perror(msg); exit(EXIT_FAILURE); } while (0)
  • //打印当前定时距首次开始计时的时间
  • static void print_elapsed_time(void)
  • {
  • static struct timespec start;
  • struct timespec curr;
  • static int first_call = 1;
  • int secs, nsecs;
  • if (first_call) { //获取开始时间
  • first_call = 0;
  • if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
  • handle_error("clock_gettime");
  • }
  • if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)
  • handle_error("clock_gettime");
  • //时间差等于每次的当前时间减去start的开始时间
  • secs = curr.tv_sec - start.tv_sec;
  • nsecs = curr.tv_nsec - start.tv_nsec;
  • if (nsecs
  • secs--;
  • nsecs += 1000000000; //相差的纳秒数
  • }
  • printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
  • }
  • int main(int argc, char *argv[])
  • {
  • struct itimerspec new_value;
  • int max_exp, fd;
  • struct timespec now;
  • uint64_t exp, tot_exp;
  • ssize_t s;
  • if ((argc != 2) && (argc != 4)) {
  • fprintf(stderr, "%s init-secs [interval-secs max-exp]\n",
  • argv[0]);
  • exit(EXIT_FAILURE);
  • }
  • if (clock_gettime(CLOCK_REALTIME, &now) == -1)
  • handle_error("clock_gettime");
  • /* Create a CLOCK_REALTIME absolute timer with initial
  • expiration and interval as specified in command line */
  • new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
  • new_value.it_value.tv_nsec = now.tv_nsec;
  • if (argc == 2) {
  • new_value.it_interval.tv_sec = 0;
  • max_exp = 1;
  • } else {
  • new_value.it_interval.tv_sec = atoi(argv[2]); //之后的定时间隔
  • max_exp = atoi(argv[3]); //定时总次数
  • }
  • new_value.it_interval.tv_nsec = 0;
  • //生成与定时器关联的文件描述符
  • fd = timerfd_create(CLOCK_REALTIME, 0);
  • if (fd == -1)
  • handle_error("timerfd_create");
  • if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)
  • handle_error("timerfd_settime");
  • //获取并打印首次首次定时开始的时间
  • print_elapsed_time();
  • printf("timer started\n");
  • for (tot_exp = 0; tot_exp
  • s = read(fd, &exp, sizeof(uint64_t)); //read阻塞等待知道超时发生
  • if (s != sizeof(uint64_t))
  • handle_error("read");
  • tot_exp += exp;
  • print_elapsed_time();
  • printf("read: %llu; total=%llu\n",
  • (unsigned long long) exp,
  • (unsigned long long) tot_exp);
  • }
  • exit(EXIT_SUCCESS);
  • }
    [/ol]





                   
            
            
       
                
            
            
  • 回复

    使用道具 举报

    全部回复0 显示全部楼层

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    楼主

    审核员

    热门推荐

    联系客服 关注微信 下载APP 返回顶部 返回列表