Linux文件描述符定时器:timerfd系列接口

timerfd 是 Linux 内核 2.6.25 引入的文件描述符式定时器,把定时事件封装为文件描述符的可读事件,可无缝接入 select/poll/epoll 等 IO 多路复用框架,是异步程序(如网络服务)中处理定时任务的常用方案。

timerfd 只有 3 个核心接口,配合 close/read 即可完成所有操作。

1.核心接口

1.1.timerfd_create:创建定时器文件描述符

cpp 复制代码
#include <sys/timerfd.h>
int timerfd_create(int clockid, int flags);

clockid:选择计时时钟类型

cpp 复制代码
CLOCK_REALTIME:系统实时时钟(会被系统时间修改影响)
CLOCK_MONOTONIC:单调递增时钟(不受系统时间修改影响)

flags:扩展标志

cpp 复制代码
TFD_NONBLOCK:将 fd 设置为非阻塞模式
TFD_CLOEXEC:进程执行 exec 时自动关闭该 fd

返回值:成功返回定时器 fd,失败返回 -1(设置 errno)

1.2. timerfd_settime:设置 / 启动定时器

cpp 复制代码
int timerfd_settime(int fd, int flags,
                    const struct itimerspec *new_value,
                    struct itimerspec *old_value);

fd:timerfd_create 返回的定时器 fd。

flags:控制超时时间的解析方式。

cpp 复制代码
0:new_value 中的时间是相对时间(从调用时刻开始计算)
TFD_TIMER_ABSTIME:new_value 中的时间是绝对时间(基于 clockid 对应的时钟)

new_value:定时器超时参数(struct itimerspec 结构)

cpp 复制代码
struct itimerspec {
    struct timespec it_value;    首次超时时间(必须设置,0 表示停止定时器)
    struct timespec it_interval; 周期性超时时间(0 表示只触发一次)
};
struct timespec {
    time_t tv_sec;  秒
    long   tv_nsec; 纳秒(0~999999999)
};

old_value:传出参数,保存定时器的旧配置(不需要则传 NULL)

返回值:成功返回 0,失败返回 -1

1.3. timerfd_gettime:获取定时器剩余时间

cpp 复制代码
int timerfd_gettime(int fd, struct itimerspec *curr_value);

curr_value:传出参数,保存当前定时器的剩余时间(it_value 是剩余时间,it_interval 是周期时间)。

返回值:成功返回 0,失败返回 -1。

2.事件处理-读取溢出次数

当定时器触发时,对应的 fd 会变为可读状态,此时调用 read 可以获取超时溢出次数(即定时器触发但未被处理的次数):

cpp 复制代码
uint64_t overrun;
ssize_t ret = read(fd, &overrun, sizeof(overrun));

read 返回值为 8(因为 uint64_t 占 8 字节),overrun 是溢出次数(正常触发时为 1,若程序处理不及时可能大于 1)。

若 fd 被设为非阻塞模式,未触发时 read 会返回 -1 且 errno=EAGAIN。

3.用 epoll 同时监听网络连接和 timerfd 定时任务

cpp 复制代码
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>

#define MAX_EVENTS 10

int main() {
    // 1. 创建 timerfd
    int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
    if (tfd == -1) { perror("timerfd_create"); return 1; }

    // 2. 设置定时器:1秒后首次触发,之后每3秒触发一次
    struct itimerspec its = {
        .it_value = {.tv_sec = 1, .tv_nsec = 0},    // 首次超时
        .it_interval = {.tv_sec = 3, .tv_nsec = 0}  // 周期超时
    };
    if (timerfd_settime(tfd, 0, &its, NULL) == -1) {
        perror("timerfd_settime"); return 1;
    }

    // 3. 创建 epoll 实例,添加 timerfd 到 epoll 监听
    int epollfd = epoll_create1(0);
    struct epoll_event ev, events[MAX_EVENTS];
    ev.events = EPOLLIN;  // 监听可读事件
    ev.data.fd = tfd;
    epoll_ctl(epollfd, EPOLL_CTL_ADD, tfd, &ev);

    // 4. 事件循环
    while (1) {
        int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
        for (int i = 0; i < nfds; i++) {
            if (events[i].data.fd == tfd) {
                // 处理定时事件:读取溢出次数
                uint64_t overrun;
                read(tfd, &overrun, sizeof(overrun));
                printf("定时器触发,溢出次数:%lu\n", overrun);
            }
            // 可在此处添加网络fd的处理逻辑
        }
    }

    // 5. 清理资源(实际中需在退出时调用)
    close(tfd);
    close(epollfd);
    return 0;
}
相关推荐
凡人叶枫27 分钟前
Effective C++ 条款28:避免使用 handles 指向对象内部
linux·服务器·开发语言·c++·嵌入式开发
AI帮小忙29 分钟前
Debian系linux操作系统里安装OpenClaw
linux·运维·debian
极创信息31 分钟前
Linux挖矿病毒深度清理实战教程,从进程隐藏、Rootkit驻留到彻底根除
java·大数据·linux·运维·安全·tomcat·健康医疗
志栋智能2 小时前
超自动化巡检剧本(Playbook):运维经验的数字化封装
运维·自动化
风味蘑菇干2 小时前
WTomcat服务器
java·服务器
ElevenS_it1882 小时前
Nginx日志监控告警实战:access_log解析+5xx突增+慢请求+异常IP自动告警完整方案(Filebeat+Zabbix)
运维·网络·tcp/ip·nginx·zabbix
weixin_307779132 小时前
Python写入Shell文件使用Linux系统的换行符
linux·开发语言·python·自动化
liulilittle2 小时前
Linux Swap 文件配置与持久化(虚拟内存)
linux·运维·服务器
未若君雅裁2 小时前
日志采集与ELK:从本地日志到集中检索分析
运维·elk·jenkins
青梅橘子皮3 小时前
Linux---进程控制(2)(进程程序替换)
linux·c++·算法