侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Linux 新的API signalfd、timerfd、eventfd使用说明

linux  /  管理员 发布于 7年前   233

三种新的fd加入linux内核的的版本:

signalfd:2.6.22

timerfd:2.6.25

eventfd:2.6.22

三种fd的意义:

lsignalfd

传统的处理信号的方式是注册信号处理函数;由于信号是异步发生的,要解决数据的并发访问,可重入问题。signalfd可以将信号抽象为一个文件描述符,当有信号发生时可以对其read,这样可以将信号的监听放到select、poll、epoll等监听队列中。

ltimerfd

可以实现定时器的功能,将定时器抽象为文件描述符,当定时器到期时可以对其read,这样也可以放到监听队列的主循环中。

leventfd

实现了线程之间事件通知的方式,也可以用于用户态和内核通信。eventfd的缓冲区大小是sizeof(uint64_t);向其write可以递增这个计数器,read操作可以读取,并进行清零;eventfd也可以放到监听队列中,当计数器不是0时,有可读事件发生,可以进行读取。

三种新的fd都可以进行监听,当有事件触发时,有可读事件发生。

signalfd涉及API:

点击(此处)折叠或打开

#include <sys/signalfd.h> int signalfd(int fd, const sigset_t *mask, int flags);#include <sys/signalfd.h> int signalfd(int fd, const sigset_t *mask, int flags);

参数fd:如果是-1则表示新建一个,如果是一个已经存在的则表示修改signalfd所关联的信号;

参数mask:信号集合;

参数flag:内核版本2.6.27以后支持SFD_NONBLOCK、SFD_CLOEXEC;

成功返回文件描述符,返回的fd支持以下操作:read、select(poll、epoll)、close

l例子

#include <sys/signalfd.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { sigset_t mask; int sfd; struct signalfd_siginfo fdsi; ssize_t s; sigemptyset(&mask); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGQUIT); if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) handle_error("sigprocmask"); sfd = signalfd(-1, &mask, 0); if (sfd == -1) handle_error("signalfd"); for (;;) { s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo)); if (s != sizeof(struct signalfd_siginfo)) handle_error("read"); if (fdsi.ssi_signo == SIGINT) { printf("Got SIGINT\n"); } else if (fdsi.ssi_signo == SIGQUIT) { printf("Got SIGQUIT\n"); exit(EXIT_SUCCESS); } else { printf("Read unexpected signal\n"); } } }#include <sys/signalfd.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { sigset_t mask; int sfd; struct signalfd_siginfo fdsi; ssize_t s; sigemptyset(&mask); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGQUIT); if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) handle_error("sigprocmask"); sfd = signalfd(-1, &mask, 0); if (sfd == -1) handle_error("signalfd"); for (;;) { s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo)); if (s != sizeof(struct signalfd_siginfo)) handle_error("read"); if (fdsi.ssi_signo == SIGINT) { printf("Got SIGINT\n"); } else if (fdsi.ssi_signo == SIGQUIT) { printf("Got SIGQUIT\n"); exit(EXIT_SUCCESS); } else { printf("Read unexpected signal\n"); } } }

L17-L21:将感兴趣的信号加入到sigset_t中;

L24:调用signalfd,把信号集与fd关联起来,第一个参数为-1表示新建一个signalfd,不是-1并且是一个合法的signalfd表示向其添加新的信号。

L29:阻塞等待信号的发生并读取。根据读取的结果可以知道发生了什么信号。

timerfd涉及的API

#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);#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:创建一个timerfd;返回的fd可以进行如下操作:read、select(poll、epoll)、closetimerfd_settime:设置timer的周期,以及起始间隔timerfd_gettime:获取到期时间。//函数参数中数据结构如下: 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 */ };//函数参数中数据结构如下: 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 */ };

l例子

#include <sys/timerfd.h> #include <sys/time.h> #include <time.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h> /* Definition of uint64_t */ #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) void printTime() { struct timeval tv; gettimeofday(&tv, NULL); printf("printTime: current time:%ld.%ld ", tv.tv_sec, tv.tv_usec); } int main(int argc, char *argv[]) { struct timespec now; if (clock_gettime(CLOCK_REALTIME, &now) == -1) handle_error("clock_gettime"); struct itimerspec new_value; new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]); new_value.it_value.tv_nsec = now.tv_nsec; new_value.it_interval.tv_sec = atoi(argv[2]); new_value.it_interval.tv_nsec = 0; int fd = timerfd_create(CLOCK_REALTIME, 0); if (fd == -1) handle_error("timerfd_create"); if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1) handle_error("timerfd_settime"); printTime(); printf("timer started\n"); for (uint64_t tot_exp = 0; tot_exp < atoi(argv[3]);) { uint64_t exp; ssize_t s = read(fd, &exp, sizeof(uint64_t)); if (s != sizeof(uint64_t)) handle_error("read"); tot_exp += exp; printTime(); printf("read: %llu; total=%llu\n",exp, tot_exp); } exit(EXIT_SUCCESS); }#include <sys/timerfd.h> #include <sys/time.h> #include <time.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h> /* Definition of uint64_t */ #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) void printTime() { struct timeval tv; gettimeofday(&tv, NULL); printf("printTime: current time:%ld.%ld ", tv.tv_sec, tv.tv_usec); } int main(int argc, char *argv[]) { struct timespec now; if (clock_gettime(CLOCK_REALTIME, &now) == -1) handle_error("clock_gettime"); struct itimerspec new_value; new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]); new_value.it_value.tv_nsec = now.tv_nsec; new_value.it_interval.tv_sec = atoi(argv[2]); new_value.it_interval.tv_nsec = 0; int fd = timerfd_create(CLOCK_REALTIME, 0); if (fd == -1) handle_error("timerfd_create"); if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1) handle_error("timerfd_settime"); printTime(); printf("timer started\n"); for (uint64_t tot_exp = 0; tot_exp < atoi(argv[3]);) { uint64_t exp; ssize_t s = read(fd, &exp, sizeof(uint64_t)); if (s != sizeof(uint64_t)) handle_error("read"); tot_exp += exp; printTime(); printf("read: %llu; total=%llu\n",exp, tot_exp); } exit(EXIT_SUCCESS); }

代码L25-L29:初始化定时器的参数,初始间隔与定时间隔。

L32:创建定时器fd,CLOCK_REALTIME:真实时间类型,修改时钟会影响定时器;CLOCK_MONOTONIC:相对时间类型,修改时钟不影响定时器。

L35:设置定时器的值。

L44:阻塞等待定时器到期。返回值是未处理的到期次数。比如定时间隔为2秒,但过了10秒才去读取,则读取的值是5。

编译运行:编译时要加rt库(g++ -lrt timerfd.cc -o timerfd)

[root@localhost appTest]# ./timerfd 5 2 10printTime: current time:1357391736.146196 timer startedprintTime: current time:1357391741.153430 read: 1; total=1printTime: current time:1357391743.146550 read: 1; total=2printTime: current time:1357391745.151483 read: 1; total=3printTime: current time:1357391747.161155 read: 1; total=4printTime: current time:1357391749.153934 read: 1; total=5printTime: current time:1357391751.157309 read: 1; total=6printTime: current time:1357391753.158384 read: 1; total=7printTime: current time:1357391755.150470 read: 1; total=8printTime: current time:1357391757.150253 read: 1; total=9printTime: current time:1357391759.149954 read: 1; total=10[root@localhost appTest]#

第一个参数5为第一次定时器到期间隔,第二个参数2为定时器的间隔,第三个参数为定时器到期10次则退出。程序运行(5+2*10)S退出。

详细信息可以:man timerfd_create

eventfd涉及API:

#include <sys/eventfd.h> int eventfd(unsigned int initval, int flags);#include <sys/eventfd.h> int eventfd(unsigned int initval, int flags);

创建一个eventfd,这是一个计数器相关的fd,计数器不为零是有可读事件发生,read以后计数器清零,write递增计数器;返回的fd可以进行如下操作:read、write、select(poll、epoll)、close。

这个函数会创建一个事件对象 (eventfd object), 用来实现,进程(线程)间的等待/通知(wait/notify) 机制. 内核会为这个对象维护一个64位的计数器(uint64_t)。并且使用第一个参数(initval)初始化这个计数器。调用这个函数就会返回一个新的文件描述符(event object)。2.6.27版本开始可以按位设置第二个参数(flags)。有如下的一些宏可以使用:

lEFD_NONBLOCK

功能同open(2)的O_NONBLOCK,设置对象为非阻塞状态,如果没有设置这个状态的话,read(2)读eventfd,并且计数器的值为0 就一直堵塞在read调用当中,要是设置了这个标志, 就会返回一个 EAGAIN 错误(errno = EAGAIN)。效果也如同 额外调用select(2)达到的效果。

lEFD_CLOEXEC

这个标识被设置的话,调用exec后会自动关闭文件描述符,防止泄漏。如果是2.6.26或之前版本的内核,flags 必须设置为0。
创建这个对象后,可以对其做如下操作:

1) write: 将缓冲区写入的8字节整形值加到内核计数器上。

2) read: 读取8字节值, 并把计数器重设为0. 如果调用read的时候计数器为0, 要是eventfd是阻塞的, read就一直阻塞在这里,否则就得到 一个EAGAIN错误。如果buffer的长度小于8那么read会失败, 错误代码被设置成 EINVAL。

3) poll select epoll

4) close: 当不需要eventfd的时候可以调用close关闭, 当这个对象的所有句柄都被关闭的时候,内核会释放资源。 为什么不是close就直接释放呢, 如果调用fork 创建
进程的时候会复制这个句柄到新的进程,并继承所有的状态。

l例子

#include <sys/eventfd.h>#include <unistd.h>#include <stdio.h>#include <stdint.h>#include <stdlib.h>#include <errno.h>#define handle_error(msg) \do { perror(msg); exit(1); } while (0)int main( int argc, char **argv ){uint64_t u;ssize_t s;5 int j;if ( argc < 2 ) {fprintf(stderr, "input in command argument");exit(1);}int efd;if ( (efd = eventfd(0, EFD_NONBLOCK)) == -1 )handle_error("eventfd failed");switch (fork()) {case 0:for( j = 1; j < argc; j ++ ) {printf("Child writing %s to efd\n", argv[j] );u = strtoull(argv[j], NULL, 0); /* analogesly atoi */s = write(efd, &u, sizeof(uint64_t));/*append u to counter */if ( s != sizeof(uint64_t) )handle_error("write efd failed");}printf("child completed write loop\n");exit(0);default:sleep (2);printf("parent about to read\n");s = read(efd, &u, sizeof(uint64_t));if ( s != sizeof(uint64_t) ) {if (errno = EAGAIN) {printf("Parent read value %d\n", s);return 1;}handle_error("parent read failed");}printf("parent read %d , %llu (0x%llx) from efd\n",s, (unsigned long long)u, (unsigned long long) u);exit(0);case -1:handle_error("fork ");}return 0;}#include <sys/eventfd.h>#include <unistd.h>#include <stdio.h>#include <stdint.h>#include <stdlib.h>#include <errno.h>#define handle_error(msg) \do { perror(msg); exit(1); } while (0)int main( int argc, char **argv ){uint64_t u;ssize_t s;5 int j;if ( argc < 2 ) {fprintf(stderr, "input in command argument");exit(1);}int efd;if ( (efd = eventfd(0, EFD_NONBLOCK)) == -1 )handle_error("eventfd failed");switch (fork()) {case 0:for( j = 1; j < argc; j ++ ) {printf("Child writing %s to efd\n", argv[j] );u = strtoull(argv[j], NULL, 0); /* analogesly atoi */s = write(efd, &u, sizeof(uint64_t));/*append u to counter */if ( s != sizeof(uint64_t) )handle_error("write efd failed");}printf("child completed write loop\n");exit(0);default:sleep (2);printf("parent about to read\n");s = read(efd, &u, sizeof(uint64_t));if ( s != sizeof(uint64_t) ) {if (errno = EAGAIN) {printf("Parent read value %d\n", s);return 1;}handle_error("parent read failed");}printf("parent read %d , %llu (0x%llx) from efd\n",s, (unsigned long long)u, (unsigned long long) u);exit(0);case -1:handle_error("fork ");}return 0;}

以上所述是小编给大家介绍的Linux 新的API signalfd、timerfd、eventfd使用说明,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!


  • 上一条:
    详解CentOS下VMware用桥接模式,静态ip上外网
    下一条:
    linux 后台日志 mysql 错误异常的解释(推荐)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客