定时器

可以用系统定时器信号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. Ts基本数据类型

    TS的基本数据类型 string let str : string str = 1 //报错 会提示num是字符串 不是数字 str = 'test' //正确 //拼接字符串 let str2 : ...

  2. java线程day-01

    综述:下面写的是我学习java线程时做的一些笔记和查阅的一些资料总结而成.大多以问答的形式出现. 一.什么是线程? 答:线程是一个轻量级的进程,现在操作系统中一个基本的调度单位,而且线程是彼此独立执行 ...

  3. PHPDebug互动扩展【phpdbg】功能浅析

    对于 PHP 开发者来说,单步的断点 Debug 调试并不是我们的必修课,而 Java . C# . C++ 这些静态语言则会经常性地进行这种调试.其实,我们 PHP 也是支持这类调试方式的,特别是对 ...

  4. 微信小程序view不能换行 text实现转义换行符

    在html中可以直接使用<br />换行,但是小程序wxml中使用<br />无效,可以换成\n Page({ data: { title: '至少5个字\n请多说些感受吧' ...

  5. httprunner版本没有更新问题

    使用命令行创建虚拟环境,创建脚手架目录后,使用pycharm打开所创建的脚手架目录. 执行:hrun demo_testcase_request.yml 提示: E:\hrun_ven\test_hr ...

  6. 执行:vim /etc/profile,提示:Command 'vim' not found, but can be installed with:

    root@uni-virtual-machine:/# vim /etc/profile Command 'vim' not found, but can be installed with: apt ...

  7. 鸿蒙内核源码分析(信号消费篇) | 谁让CPU连续四次换栈运行 | 百篇博客分析OpenHarmony源码 | v49.04

    百篇博客系列篇.本篇为: v49.xx 鸿蒙内核源码分析(信号消费篇) | 谁让CPU连续四次换栈运行 | 51.c.h .o 进程管理相关篇为: v02.xx 鸿蒙内核源码分析(进程管理篇) | 谁 ...

  8. k8s负载资源StatefulSet工作解析

    在k8s中工作负载资源StatefulSet用于管理有状态应用. 什么是无状态? 组成一个应用的pod是对等的,它们之前没有关联和依赖关系,不依赖外部存储. 即我们上篇小作文中deployment创建 ...

  9. Winform 实现图片轮播(解决Image.FromFile内存不足)

    前言 最近项目中需要在winform中做一个类似于网页那种轮播的效果,这里做下记录. 实现 整体的实现思路如下: 读取图片文件夹. 建立一个集合存储Image对象. 定时器定时更换PictrueBox ...

  10. Docker入门系列之五:15个 Docker 命令

    在这篇文章中,我们将学习15个Dockers CLI命令.如果你还不了解Docker,请查看这个系列的其他部分进行学习,Docker概念,生态系统,Dockerfile,Docker镜像. Docke ...