定时器

可以用系统定时器信号SIGALARM

最近工作需要于是又发现了一个新玩意timerfd配合epoll使用。

man 手册看一下

TIMERFD_CREATE(2)                         Linux Programmer's Manual                         TIMERFD_CREATE(2)

NAME
timerfd_create, timerfd_settime, timerfd_gettime - timers that notify via file descriptors SYNOPSIS
#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); DESCRIPTION
These system calls create and operate on a timer that delivers timer expiration notifications via a
file descriptor. They provide an alternative to the use of setitimer(2) or timer_create(2), with the
advantage that the file descriptor may be monitored by select(2), poll(2), and epoll(7). The use of these three system calls is analogous to the use of timer_create(2), timer_settime(2), and
timer_gettime(2). (There is no analog of timer_getoverrun(2), since that functionality is provided by
read(2), as described below.)

这是一个专门针对fd的定时器,通过fd可以读取定时数据(定时时间到了就会有数据回来,否则阻塞(阻塞模式))。

结合epoll使用先弄个epoll出来

/* init epoll */
int epollInit(){
int epFd = epoll_create(EPOLL_SIZE);
if (epFd < 0){
perror("epoll create");
return -1;
}
return epFd;
}

然后在弄个timerfd,并把它加入到epoll 事件中

int TimerFdInit(int epFd)
{
struct itimerspec new_value;
/*init time*/
new_value.it_value.tv_sec = 1;
new_value.it_value.tv_nsec = 0;
/*time interval*/
new_value.it_interval.tv_sec = 1;
new_value.it_interval.tv_nsec = 0; int timerFd = timerfd_create(CLOCK_MONOTONIC, 0);
if (timerFd < 0) {
cerr<<strerror(errno)<<endl;
return -1;
} int ret = timerfd_settime(timerFd, 0, &new_value, NULL);
if (ret < 0) {
cerr<<strerror(errno)<<endl;
close(timerFd);
return -1;
}
/* add to epoll */
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLHUP | EPOLLRDHUP;
ev.data.fd = timerFd;
epoll_ctl(epFd, EPOLL_CTL_ADD, timerFd, &ev);
return timerFd;
}

接下来就去epoll_wait循环等待这个定时器的数据

int epollHandle(int epFd, int timerFd)
{
struct epoll_event epEvents[EPOLL_SIZE] = {};
int timeOut = -1;
uint64_t totalExp = 0;
while (1)
{
//blocked
int eventNum = epoll_wait(epFd, epEvents, EPOLL_SIZE, timeOut);
if(eventNum < 0) {
perror("epoll failure");
return -1;
} //handle epEvents
for(int i = 0; i < eventNum; ++i) {
int tmpFd = epEvents[i].data.fd;
if(epEvents[i].events & EPOLLIN) {
if (timerFd == tmpFd) {
/*handle timerFd*/
uint64_t tmpExp = 0;
/*must read*/
read(timerFd, &tmpExp, sizeof(uint64_t));
totalExp += tmpExp;
cout<<"timer count "<<totalExp<<endl;
}
}
}
}
return 0;
}

fd定时器--timerfd学习的更多相关文章

  1. MySQL 定时器EVENT学习

    原文:http://blog.csdn.net/lifuxiangcaohui/article/details/6583535 MySQL 定时器EVENT学习 MySQL从5.1开始支持event功 ...

  2. Linux定时器 timerfd使用

    英文使用手册原汁原味,一手资料. NAME       timerfd_create, timerfd_settime, timerfd_gettime - timers that notify vi ...

  3. Linux使用定时器timerfd 和 eventfd接口实现进程线程通信

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  4. 定时器——Cocos2d-x学习历程(十一)

    1.定时器 利用场景.层和精灵等游戏元素,我们可以构建游戏的框架,但是此时的游戏仍然是静止不动的.在一切游戏中,游戏的状态都会随着时间的流逝而改变,同时我们还需要定时进行一些逻辑判断,例如鱼和子弹的碰 ...

  5. 【转载】Java定时器的学习

    前几看了一下<thinking in java>了解到java原生的Times类有两个问题: (1)Timer是启动单个线程来处理所有的时间任务,如果一个任务耗时很久,那么如果在执行这个过 ...

  6. c#进程、定时器初步学习

    首先是什么原因让我做这个小项目的呢,是因为在知乎里看到的游侠的文章才尝试着自己做的,文章地址是:https://www.zhihu.com/question/48811975 开始做的时候我是照着文章 ...

  7. Linux Timer定时器【转】

    转自:https://www.jianshu.com/p/66b3c75cae81 timerfd为Linux为用户程序提供的定时器接口,该接口基于文件描述符,通过文件描述符的可读事件进行超时通知,且 ...

  8. linux新定时器:timefd及相关操作函数

    timerfd是Linux为用户程序提供的一个定时器接口.这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景. 一,相关操作函数 #inclu ...

  9. (二十四)linux新定时器:timefd及相关操作函数

    timerfd是Linux为用户程序提供的一个定时器接口.这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景. 一,相关操作函数 #inclu ...

随机推荐

  1. 一键配置tomcat定期日志清理功能

    概述 日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处理一个单个的 ...

  2. C语言中的符号重载

    摘自<C专家编程>第二章37页                     C语言中符号的重载 符号 意义 static 在函数内部,表示该变量的值在各个调用间一直保持延续性在函数这一级,表示 ...

  3. java的split方法中的regex参数

    我们需要以|进行分割,为了匹配|本身,正则中采用\|进行转义,而Java中\也表示转义,从java到正则需要必须使用\\|进行转义,即split中的\\表示正则的转义.

  4. POJ1426——Find The Multiple (简单搜索+取余)

    题意: 给一个数n,让你找出一个只有1,0,组成的十进制数,要求是找到的数可以被n整除. 用DFS是搜索 当前位数字 (除最高位固定为1),因为每一位都只有0或1两种选择,换而言之是一个双入口BFS. ...

  5. 使用metaweblog API实现通用博客发布 之 本地图片自动上传以及替换路径

    使用metaweblog API实现通用博客发布 之 本地图片自动上传以及替换路径 通过metaweblog API 发布博文的时候,由于markdown中的图片路径是本地路径,将导致发布的文章图片不 ...

  6. ecshop调用商品的购买次数方法

    这时候我们修改一下 写成一个函数放到lib_goods.php 这样就可以随便调用了 --------------------------------------------------------- ...

  7. rationrose安装步骤

    Rational Rose是Rational公司出品的一种面向对象的统一建模语言的可视化建模工具.用于可视化建模和公司级水平软件应用的组件构造. 就像一个戏剧导演设计一个剧本一样,一个软件设计师使用R ...

  8. IE浏览器报400错误:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    前言: 在用IE浏览器时访问tomcat项目时,页面报400错误,后台错误: java.lang.IllegalArgumentException: Invalid character found i ...

  9. Python3入门系列之-----内置的文件操作模块OS

    前言 在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这个时候就需要用到os模块. 使用前 ...

  10. WPF进阶技巧和实战03-控件(5-列表、树、网格02)

    数据模板 样式提供了基本的格式化能力,但是不管如何修改ListBoxItem,他都不能够展示功能更强大的元素组合,因为了每个ListBoxItem只支持单个绑定字段(通过DisplayMemberPa ...