timerfd.h中定义的函数


timerfd_create

timerfd_settime

timerfd_gettime

用法

#include<sys/timerfd.h>

int timerfd_create(int clockid,int flags);
int timerfd_settime(int fd,int flags,const struct itimerspec *new_value,struct itimerspec *old_value);
int timerfd_gettime(int fd,struct itimerspec *curr_value);

简介

这三个系统调用创建并操作一个timer,这个timer通过文件描述符来传递timer expiration notification。这几个函数提供了settimer以及timer_create函数的替代,这种替代方式有一个优点,因为文件描述符可以被select、poll以及epoll monitored。

使用这三个系统调用和使用timer_create,timer_settime以及timer_gettime是类似的。(注意没有和timer_getoverrun类似的调用,因为这个函数功能由read提供)。

timerfd_create

int timerfd_create(int clockid,int flags);

timerfd_create创建一个新的timer对象,并且返回了一个指向这个timer对象的文件描述符。clockid变量必须为下列的一种:

  • CLOCK_REALTIME:一个可设定的system-wide实时clock
  • CLOCK_MONOTONIC:一个不可设定的单调增长的clock,它衡量了从过去某个时间点到现在为止所经历的时间,不随着系统启动而变化

timerfd_settime

int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);

这个函数启动或终止由文件描述符fd所引用的timer。

new_value变量指定了timer的初始expiration以及interval。其中itimerspec的定义如下:

struct itimerspec{
	struct timespec it_interval;		//interval for  periodic timer
	struct timespec it_value;			//initial expiration			
}

new_value.it_value指定了timer的初始expiration,用second和nanosecond进行衡量。设定new_value.it_value任意一个field为一个非零值都会启动这个timer。如果将new_value.it_value两个field都设定为零会关闭这个timer。

new_value.it_interval的一个或两个field设定为非零值会设定period,以second和nanosecond为单位(这个时间间隔队友重复进行的timer而言是从初始expiration之后开始进行的)。 如果两个field均为零,那么这个timer仅会expire一次,这个到期时间由new_value.it_value指定。

默认情况下,new_value所指定的初始expiration time是相对于当前调用时timer的clock进行解释的(即new_value.it_value指定了一个相对于当前clockid的clock值的时间)。我们也可以通过设定flags变量来选定一个absolute timeout。

flags变量是一个bit mask,可以选定下列的几个值:

  • TFD_TIMER_ABSTIME:将new_value.it_value解释为timer clock上的一个绝对值。timer会在clock上的值等于这个值时到期。

对文件描述符进行操作

由timerfd_create()函数创建的文件描述符支持如下的操作:

read:If the timer has already expired one or more times since its settings were last modified using timerfd_settime(), or since the last successful read(2), then the buffer given to read(2) returns an unsigned 8-byte integer (uint64_t) containing the number of expirations that have occurred. (The returned value is in host byte order—that is, the native byte order for integers on the host machine.)

If no timer expirations have occurred at the time of the read(2), then the call either blocks until the next timer expiration, or fails with the error EAGAIN if the file descriptor has been made nonblocking (via the use of the fcntl(2) F_SETFL operation to set the O_NONBLOCK flag).

A read(2) fails with the error EINVAL if the size of the supplied buffer is less than 8 bytes.

If the associated clock is either CLOCK_REALTIME or CLOCK_REALTIME_ALARM, the timer is absolute (TFD_TIMER_ABSTIME), and the flag TFD_TIMER_CANCEL_ON_SET was specified when calling timerfd_settime(), then read(2) fails with the error ECANCELED if the real-time clock undergoes a discontinuous change. (This allows the reading application to discover such discontinuous changes to the clock.)

If the associated clock is either CLOCK_REALTIME or CLOCK_REALTIME_ALARM, the timer is absolute (TFD_TIMER_ABSTIME), and the flag TFD_TIMER_CANCEL_ON_SET was not specified when calling timerfd_settime(), then a discontinuous negative change to the clock (e.g., clock_settime(2)) may cause read(2) to unblock, but return a value of 0 (i.e., no bytes read), if the clock change occurs after the time expired, but before the read(2) on the file descriptor.

close:当不再需要这个文件描述符后它应该被关闭。当和同一个timer对象相关联的所有文件描述符都关闭后,这个timer会被弃用,内核会释放和它有关的资源。

参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值