转自:https://www.jianshu.com/p/66b3c75cae81

timerfd为Linux为用户程序提供的定时器接口,该接口基于文件描述符,通过文件描述符的可读事件进行超时通知,且能够被用于epoll/select。主要有三个函数。

头文件: include <sys/timerfd.h>

int timerfd_create(int clockid, int flags)

功能: 生成定时器,返回文件描述符。
clockid: CLOCK_MONOTONIC或CLOCK_REALTIME,其中CLOCK_MONOTONIC表示获取的时间为系统重启到现在的时间,更改系统时间对其没有影响。CLOCK_REALTIME表示从1970.1.1到目前的时间,更改系统时间会更改获取的值。
flags: TFD_NONBLOCK(非阻塞), TFD_CLOEXEC(同O_CLOEXEC)。
return: timer的文件描述符。

int timerfd_settime(int tfd, int flags, const struct itimerspec *newValue, struct itimerspec *oldValue)

功能: 用于启动或关闭指定fd的定时器。
tfd: timerfd,由timerfd_create函数返回。
flags: 1表示设置的是绝对时间;0表示相对时间。
newValue: 指定新的超时时间,若newValue.it_value非0则启动定时器,否则关闭定时器。若newValue.it_interval为0则定时器只定时一次,否则之后每隔设定时间超时一次。
oldValue:不为NULL时则返回定时器这次设置之前的超时时间。
return:失败则返回-1。

struct timespec
{
time_t tv_sec; //秒
long tv_nsec; //纳秒
}
struct itimerspec
{
struct timespec it_interval; //首次超时后,每隔it_interval超时一次
struct timespec it_value; //首次超时时间
}

int timerfd_gettime(int fd, struct itimerspec *curValue)

功能: 用于获取距离下次超时还剩下的时间。如果调用时定时器已经到期(即超过it_value时间),并且定时器处于循环模式(即it_interval不为0), 那么调用该函数后定时器重新开始计时。
fd: timerfd,由timerfd_create函数返回。
curValue: 返回距离下次超时剩下的时间。
return:失败返回-1

读取timerfd

当定时器超时,timerfd可读,返回uint64_t类型的整数,为超时的数目(指有多少个超时未读),如果定时器没有发生超时事件,若timerfd为阻塞时,read将阻塞,若timerfd为非阻塞时,返回EAGAIN错误。如果read是的数据小于8字节以EINVAL错误返回。

样例代码

#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <stdint.h>
#include <iostream>
using namespace std; const int EPOLL_SIZE = 10; int main(int argc, char* argv[])
{
int tfd, epfd, nfds;
struct epoll_event event;
struct epoll_event events[EPOLL_SIZE]; //创建timerfd, CLOCK_REALTIME为绝对时间,TFD_NONBLOCK为非阻塞
tfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
if (tfd < 0)
{
cerr << "timerfd_create error!" << endl;
return -1;
}
struct timespec startTime, intervalTime;
startTime.tv_sec = 0;
startTime.tv_nsec = 1; //相当于立即到达超时时间
intervalTime.tv_sec = 3; //首次超时后,每三秒超时一次
intervalTime.tv_nsec = 0;
struct itimerspec newValue;
newValue.it_value = startTime;
newValue.it_interval = intervalTime;
//设置超时时间,且为相对时间
if (timerfd_settime(tfd, 0, &newValue, NULL) < 0)
{
cerr << "timerfd_settime error!" << endl;
return -1;
}
//用epoll来监听描述符
epfd = epoll_create(EPOLL_SIZE);
if (epfd < 0)
{
cerr << "epoll_create error!" << endl;
return -1;
} event.data.fd = tfd;
event.events = EPOLLIN;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, tfd, &event) < 0)
{
cerr << "epoll_ctl error!" << endl;
return -1;
} uint64_t count = 0;
while (1)
{
//非阻塞等待
nfds = epoll_wait(epfd, events, EPOLL_SIZE, 0);
if (nfds == 0) continue;
for (int i = 0; i < nfds; i++)
{
if (events[i].events & EPOLLIN)
{
uint64_t data;
read(events[i].data.fd, &data, sizeof(uint64_t));
count += data;
cout << "read: " << data << ", timer count: " << count << endl;
}
}
}
return 0;
}

小礼物走一走,来简书关注我

作者:agin719
链接:https://www.jianshu.com/p/66b3c75cae81
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Linux Timer定时器【转】的更多相关文章

  1. 浅析linux内核中timer定时器的生成和sofirq软中断调用流程(转自http://blog.chinaunix.net/uid-20564848-id-73480.html)

    浅析linux内核中timer定时器的生成和sofirq软中断调用流程 mod_timer添加的定时器timer在内核的软中断中发生调用,__run_timers会spin_lock_irq(& ...

  2. 浅析linux内核中timer定时器的生成和sofirq软中断调用流程【转】

    转自:http://blog.chinaunix.net/uid-20564848-id-73480.html 浅析linux内核中timer定时器的生成和sofirq软中断调用流程 mod_time ...

  3. 芯灵思Sinlinx A64开发板Linux内核定时器编程

    开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 Linux 内核定时器是内 ...

  4. 全志A33开发板Linux内核定时器编程

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...

  5. 芯灵思SinlinxA33开发板Linux内核定时器编程

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...

  6. Linux内核 - 定时器

    #include <linux/timer.h> //头文件 struct timer_list mytimer; //定义变量 static void my_timer(unsigned ...

  7. linux内核--定时器API

    /**<linux/timer.h> 定时器结构体 struct timer_list { ........ unsigned long expires; --内核希望定时器执行的jiff ...

  8. Linux内核定时器struct timer_list

    1.前言 Linux内核中的定时器是一个很常用的功能,某些需要周期性处理的工作都需要用到定时器.在Linux内核中,使用定时器功能比较简单,需要提供定时器的超时时间和超时后需要执行的处理函数. 2.常 ...

  9. linux 内核定时器

    无论何时你需要调度一个动作以后发生, 而不阻塞当前进程直到到时, 内核定时器是给你 的工具. 这些定时器用来调度一个函数在将来一个特定的时间执行, 基于时钟嘀哒, 并且 可用作各类任务; 例如, 当硬 ...

随机推荐

  1. Hadoop 5 Hbase 遇到的问题

    hbase伪分布式配置完成后: 在bin/hbase shell 进行create操作时出现:Can't get master address from ZooKeeper; znode data = ...

  2. python数据分析Titanic_Survived预测

    import pandas as pd import matplotlib.pyplot as plt # matplotlib画图注释中文需要设置from matplotlib.font_manag ...

  3. C#(近期目标)

    最近很多同学为了实习都在学Java,但是我个人更偏好C#,首先因为自己基础不是太好,而C#又更容易入门,拥有比较完善的开发环境,是微软开发出来的语言.它吸收了C++和Java两门语言的所有有点,因为它 ...

  4. 如何访问cxGrid控件过滤后的数据集

    var I: Integer; begin Memo1.Lines.Clear; with cxGrid1DBTableView1.DataController do for I := 0 to Fi ...

  5. Windows 7 上面安装 dotnet core 之后 使用 应用报错的处理:api-ms-win-crt-runtime-l1-1-0.dll 丢失

    Windows2016 使用 dotnet core的使用 安装了就可以了 但是发现 windows 7 不太行 报错如图示 没办法简单百度了下 https://www.microsoft.com/z ...

  6. Hibernate性能优化之EHCache缓存

    像Hibernate这种ORM框架,相较于JDBC操作,需要有更复杂的机制来实现映射.对象状态管理等,因此在性能和效率上有一定的损耗. 在保证避免映射产生低效的SQL操作外,缓存是提升Hibernat ...

  7. 基于SOA的高并发和高可用分布式系统架构和组件详解

    基于SOA的分布式高可用架构和微服务架构,是时下如日中天的互联网企业级系统开发架构选择方案.在核心思想上,两者都主张对系统的横向细分和扩展,按不同的业务功能模块来对系统进行分割并且使用一定的手段实现服 ...

  8. mybatis之关联(2)

    MapperAssoDao.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  9. requestMapping设置客户端访问地址

  10. Django_ KindEditor 插件使用

    KindEditor  富文本编辑器插件 目的及原理: 更便捷的在前端页面上实现用户的文本编辑操作, 本质上就是对标签的样式进行封装和事件预处理, 常规操作都可以通过直接的引入即可实现, 但是对于存在 ...