定时器timerfd

1.为什么要加入此定时器接口

linux2.6.25版本新增了timerfd这个供用户程序使用的定时接口,这个接口基于文件描述符,当超时事件发生时,该文件描述符就变为可读。我首次接触这个新特性是在muduo网络库的定时器里看到的,那么新增一个这样的定时器接口有什么意义呢?

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

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

  1. .sleep()

  2. .alarm()

  3. .usleep()

  4. .nanosleep()

  5. .clock_nanosleep()

  6. .getitimer()/setitimer()

  7. .timer_create()/timer_settime/timer_gettime()/timer_delete()

  8. .timerfd_create()/timerfd_gettime()/timer_settime()

以上便是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的创建

 
  1. int timer_create(int clockid,int flags);

  2.  
  3. //成功返回0

 

第一个参数一般为CLOCK_REALTIME或者CLOCK_MONOTONIC,其参数意义为参数意义

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

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

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

(2)定时器的设置

 
  1. int timerfd_settime(int fd,int flags

  2. const struct itimerspec *new_value

  3. struct itimerspec *old_value);

  4. //成功返回0

 

该函数的功能为启动和停止定时器,第一个参数fd为上面的timerfd_create()函数返回的定时器文件描述符,第二个参数flags为0表示相对定时器,为TFD_TIMER_ABSTIME表示绝对定时器,第三个参数new_value用来设置超时时间,为0表示停止定时器,第四个参数为原来的超时时间,一般设为NULL

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

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

 
  1. struct itimerspec {

  2. struct timespec it_interval; /* Interval for periodic timer */

  3. struct timespec it_value; /* Initial expiration */

  4. };

 

其中it_value保存首次超时时间值,即在哪个时间点超时的那个时间的值,it_interval为后续周期性超时的时间间隔,注意是时间间隔不是时间值啦 
timespec的结构定义如下

 
  1. struct timespec {

  2. time_t tv_sec; /* Seconds */

  3. long tv_nsec; /* Nanoseconds */

  4. };

 

需要注意的是当设置定时器后,我们就可以用read读取定时器的文件描述符了,当其可读时,就是超时发生的时间,下面的实例中给出用法,请读者仔细体会

3.具体实例

以绝对超时为例

 
  1. #include <sys/timerfd.h>

  2. #include <time.h>

  3. #include <unistd.h>

  4. #include <stdlib.h>

  5. #include <stdio.h>

  6. #include <stdint.h> /* Definition of uint64_t */

  7.  
  8. #define handle_error(msg) \

  9. do { perror(msg); exit(EXIT_FAILURE); } while (0)

  10.  
  11.  
  12. //打印当前定时距首次开始计时的时间

  13. static void print_elapsed_time(void)

  14. {

  15. static struct timespec start;

  16. struct timespec curr;

  17. static int first_call = 1;

  18. int secs, nsecs;

  19.  
  20. if (first_call) { //获取开始时间

  21. first_call = 0;

  22. if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)

  23. handle_error("clock_gettime");

  24. }

  25.  
  26. if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)

  27. handle_error("clock_gettime");

  28.  
  29. //时间差等于每次的当前时间减去start的开始时间

  30. secs = curr.tv_sec - start.tv_sec;

  31. nsecs = curr.tv_nsec - start.tv_nsec;

  32. if (nsecs < 0) {

  33. secs--;

  34. nsecs += 1000000000; //相差的纳秒数

  35. }

  36. printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);

  37. }

  38.  
  39.  
  40. int main(int argc, char *argv[])

  41. {

  42. struct itimerspec new_value;

  43. int max_exp, fd;

  44. struct timespec now;

  45. uint64_t exp, tot_exp;

  46. ssize_t s;

  47.  
  48. if ((argc != 2) && (argc != 4)) {

  49. fprintf(stderr, "%s init-secs [interval-secs max-exp]\n",

  50. argv[0]);

  51. exit(EXIT_FAILURE);

  52. }

  53.  
  54. if (clock_gettime(CLOCK_REALTIME, &now) == -1)

  55. handle_error("clock_gettime");

  56.  
  57. /* Create a CLOCK_REALTIME absolute timer with initial

  58. expiration and interval as specified in command line */

  59.  
  60. new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);

  61. new_value.it_value.tv_nsec = now.tv_nsec;

  62. if (argc == 2) {

  63. new_value.it_interval.tv_sec = 0;

  64. max_exp = 1;

  65. } else {

  66. new_value.it_interval.tv_sec = atoi(argv[2]); //之后的定时间隔

  67. max_exp = atoi(argv[3]); //定时总次数

  68. }

  69. new_value.it_interval.tv_nsec = 0;

  70.  
  71. //生成与定时器关联的文件描述符

  72. fd = timerfd_create(CLOCK_REALTIME, 0);

  73. if (fd == -1)

  74. handle_error("timerfd_create");

  75. if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)

  76. handle_error("timerfd_settime");

  77.  
  78. //获取并打印首次首次定时开始的时间

  79. print_elapsed_time();

  80. printf("timer started\n");

  81.  
  82. for (tot_exp = 0; tot_exp < max_exp;) {

  83. s = read(fd, &exp, sizeof(uint64_t)); //read阻塞等待知道超时发生

  84. if (s != sizeof(uint64_t))

  85. handle_error("read");

  86.  
  87. tot_exp += exp;

  88. print_elapsed_time();

  89. printf("read: %llu; total=%llu\n",

  90. (unsigned long long) exp,

  91. (unsigned long long) tot_exp);

  92. }

  93.  
  94. exit(EXIT_SUCCESS);

  95. }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值