1.为什么要加入此定时器接口
linux2.6.25版本新增了timerfd这个供用户程序使用的定时接口,这个接口基于文件描述符,当超时事件发生时,该文件描述符就变为可读。我首次接触这个新特性是在muduo网络库的定时器里看到的,那么新增一个这样的定时器接口有什么意义呢?
要说明这个问题我得先给大家列举一下Linux下能实现定时功能的各个接口,然后通过逐一比较来说明原因
linux下的定时接口主要有如下几种
.sleep()
.alarm()
.usleep()
.nanosleep()
.clock_nanosleep()
.getitimer()/setitimer()
.timer_create()/timer_settime/timer_gettime()/timer_delete()
.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
- 2
- 3
- 1
- 2
- 3
第一个参数一般为CLOCK_REALTIME或者CLOCK_MONOTONIC,其参数意义为参数意义
CLOCK_REALTIME:相对时间,从1970.1.1到目前时间,之所以说其为相对时间,是因为我们只要改变当前系统的时间,从1970.1.1到当前时间就会发生变化,所以说其为相对时间
CLOCK_MONOTONIC:与CLOCK_REALTIME相反,它是以绝对时间为准,获取的时间为系统最近一次重启到现在的时间,更该系统时间对其没影响
第二个参数为控制标志:TFD_NONBLOCK(非阻塞),TFD_CLOEXEC(同O_CLOEXEC)
(2)定时器的设置
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
该函数的功能为启动和停止定时器,第一个参数fd为上面的timerfd_create()函数返回的定时器文件描述符,第二个参数flags为0表示相对定时器,为TFD_TIMER_ABSTIME表示绝对定时器,第三个参数new_value用来设置超时时间,为0表示停止定时器,第四个参数为原来的超时时间,一般设为NULL
需要注意的是我们可以通过clock_gettime获取当前时间,如果是绝对定时器,那么我们得获取1970.1.1到当前时间(CLOCK_REALTIME),在加上我们自己定的定时时间。若是相对定时,则要获取我们系统本次开机到目前的时间加我们要定的时常(即获取CLOCK_MONOTONIC时间)
上述参数中itimerspec的结构定义如下
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
其中it_value保存首次超时时间值,即在哪个时间点超时的那个时间的值,it_interval为后续周期性超时的时间间隔,注意是时间间隔不是时间值啦
timespec的结构定义如下
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
需要注意的是当设置定时器后,我们就可以用read读取定时器的文件描述符了,当其可读时,就是超时发生的时间,下面的实例中给出用法,请读者仔细体会
3.具体实例
以绝对超时为例
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
编译并运行上述程序,结果如下
当定一次值,设为3秒后超时
当定5次时,初次为3s后之后每1秒超时一次,运行结果如下