Linux timerfd定时器的介绍及使用

Linux提供了timerfd系列的定时器接口,由于是通过文件描述符timerfd进行组织的定时器,所以可以配合select/epoll进行对timerfd的监听,进而处理超时的事件。

timerfd系列主要包含三个相关的函数:

#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);

timerfd_create

创建一个新的定时器对象,并返回一个引用该定时器的文件描述符。
clockid参数指定用于标记定时器进度的时钟,并且必须是以下类型之一:
1、CLOCK_REALTIME —— 系统实时时钟。
2、CLOCK_MONOTONIC —— 系统启动后不可改变的单调递增时钟。
3、CLOCK_BOOTTIME(Since Linux 3.15)—— 类似CLOCK_MONOTONIC,单调递增时钟。
4、CLOCK_REALTIME_ALARM (Since Linux 3.11)—— 类似CLOCK_REALTIME,但如果系统被暂停,它将唤醒系统,调用者必须具有CAP_WAKE_ALARM功能,才能根据此时钟设置定时器。
5、CLOCK_BOOTTIME_ALARM (since Linux 3.11) —— 类似BOOTTIME,但如果系统被暂停,它将唤醒系统,调用者必须具有CAP_WAKE_ALARM功能,才能根据此时钟设置定时器。

flags参数有两个选项:
1、TFD_NONBLOCK —— 设置文件描述符非阻塞。
2、TFD_CLOEXEC —— 设置文件描述符close-on-exec,即使用exec执行程序时,关闭该文件描述符。

timerfd_create()调用成功则返回一个定时器的文件描述符。

timerfd_settime

启动或停止文件描述符timerfd引用的定时器。new_value参数指定定时器的初始过期时间和间隔,用于此参数的itimerspec结构包含两个字段,每个字段又是类型为timespec的结构时间段:

struct timespec {
	time_t tv_sec;                /* Seconds */
    long   tv_nsec;               /* Nanoseconds */
};

struct itimerspec {
    struct timespec it_interval;  /* Interval for periodic timer */
    struct timespec it_value;     /* Initial expiration */
};

new_value里的it_value表示启动定时器后,定时器第一次定时到期的时间,如果it_value的tv_sec或tv_nsec值非0,那么定时器在调用timerfd_settime()之后就会启动。

new_value里的it_interval表示第一次定时到期之后,往后的定时时间,如果tv_sec和tv_nsec值都是0,那么这个定时器就是one shot的,只定时一次。

flags参数是bit mask,包含以下选项:
1、TFD_TIMER_ABSTIME —— 使用绝对定时器。当定时器的时钟值达到new_value中指定的值时,定时器将过期。

2、TFD_TIMER_ABSTIME
如果此标志与TFD_TIMER_ABSTIME一起指定,并且此定时器的时钟为CLOCK_REALTIME或CLOCK_REALTIME_ALARM,当时钟经常发生不连续的变化时,这个定时器是可以取消的。

timerfd_gettime

获取fd指向的定时器的当前设置,返回在itimerspec结构里,该结构包含文件描述符fd引用的定时器的当前设置。
it_value字段结构返回定时器下次过期之前的时间,如果该结构的两个字段都为零,则计时器当前被解除。
it_interval字段结构返回定时器的时间间隔。如果该结构的两个字段都为零,那么定时器将被设置为仅在curr_value(即第二个参数)指定的时间过期一次。

例程,通过epoll监听timerfd,打印出当前时间,设置定时时间为1秒钟。

#include <iostream>
#include <time.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <string.h>
using namespace std;

static time_t t;
static char buffer[32];

int getTimer() {
    // 构建定时器fd
    int timefd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);  
    if(timefd < 0) {
        perror("timerfd_create");
        return -1; 
    }   
    //第二个参数time_value初始化
    struct itimerspec new_value;    //超时结构体, it_value为第一次超时时间,it_interval为以后每一次超时时间
    
    //第一次超时时间设置
    new_value.it_value.tv_sec = 5;
    new_value.it_value.tv_nsec = 0;

    //之后每次超时时间设置
    new_value.it_interval.tv_sec = 1;
    new_value.it_interval.tv_nsec = 0;
             
    //开启定时器
    int ret = timerfd_settime(timefd, 0, &new_value, NULL);
    if(ret < 0) {
        perror("timerfd_settime");
        return -1;
    }
    cout << "定时器开启" << endl;
    return timefd;
}
            
void epoll_loop(int timefd) {
    //创建epoll, 将timefd交由epoll监听
    int epollfd = epoll_create(5);
    epoll_event events[5];
    epoll_event ev;
    ev.data.fd = timefd;
    ev.events = EPOLLIN | EPOLLET;  
    
    epoll_ctl(epollfd, EPOLL_CTL_ADD, timefd, &ev);

    while(1) { 
        int num = epoll_wait(epollfd, events, 5, 0);
        for(int i = 0; i < num; ++i) {
            if(events[i].data.fd == timefd) {
                //收到超时信号, 需要读出uint64_t大小
                uint64_t buf;
                int r = read(events[i].data.fd, &buf, sizeof(buf));
                if(r == sizeof(uint64_t)) {
                    time(&t);
                    ctime_r(&t, buffer);
                    cout << "超时信号触发, 当前时间: " << buffer << endl;
                    memset(buffer, 0, sizeof(buffer));
                }
            } else {
                continue;
            }
        }
    }
}

int main() {
    int timefd = getTimer();
    epoll_loop(timefd);
    return 0;
}  
     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值