1. 测试代码:

//https://www.jianshu.com/p/d7ebac8dc9f8
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <pthread.h>
#include <sys/eventfd.h>
#include <sys/epoll.h>int event_fd = -1;void *read_thread(void *dummy)
{uint64_t inc = 1;int ret = 0;int i = 0;for (; i < 2; i++) {ret = write(event_fd, &inc, sizeof(uint64_t));if (ret < 0) {perror("child thread write event_fd fail.");} else {printf("child thread completed write %llu (0x%llx) to event_fd\n", (unsigned long long) inc, (unsigned long long) inc);}sleep(4);}
}int main(int argc, char *argv[])
{int ret = 0;pthread_t pid = 0;event_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);if (event_fd < 0) {perror("event_fd create fail.");}ret = pthread_create(&pid, NULL, read_thread, NULL);if (ret < 0) {perror("pthread create fail.");}uint64_t counter;int epoll_fd = -1;struct epoll_event events[16];if (event_fd < 0){printf("event_fd not inited.\n");}epoll_fd = epoll_create(8);if (epoll_fd < 0){perror("epoll_create fail:");}struct epoll_event read_event;read_event.events = EPOLLIN;read_event.data.fd = event_fd;ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, event_fd, &read_event);if (ret < 0) {perror("epoll_ctl failed:");}while (1) {printf("main thread epoll is waiting......\n");ret = epoll_wait(epoll_fd, events, 16, 2000);printf("main thread epoll_wait return ret : %d\n", ret);if (ret > 0) {int i = 0;for (; i < ret; i++) {int fd = events[i].data.fd;if (fd == event_fd) {uint32_t epollEvents = events[i].events;if (epollEvents & EPOLLIN) {ret = read(event_fd, &counter, sizeof(uint64_t));if (ret < 0) {printf("main thread read fail\n");} else {printf("main thread read %llu (0x%llx) from event_fd\n", (unsigned long long) counter, (unsigned long long) counter);}} else {printf("main thread unexpected epoll events on event_fd\n");}}}} else if (ret == 0) {printf("main thread epoll_wait timed out. continue epoll\n");} else {perror("main thread epoll_wait error.");}}
}

输出结果:

wufan@Frank-Linux:~/Linux/test$ ./epoll_eventfd
main thread epoll is waiting...... // main 线程阻塞在读端
child thread completed write 1 (0x1) to event_fd // 第一次写入后阻塞 4 秒
main thread epoll_wait return ret : 1 // 第一次写完后,立即唤醒 main 线程去进行读操作
main thread read 1 (0x1) from event_fd // main 线程读到了数据
main thread epoll is waiting...... // main 线程阻塞又在读端,超时时间为 2 秒
main thread epoll_wait return ret : 0 // main 线程阻塞等待时间到,返回
main thread epoll_wait timed out. continue epoll
main thread epoll is waiting...... // main 线程阻塞又在读端,超时时间为 2 秒
child thread completed write 1 (0x1) to event_fd // // 第二次写入后阻塞 4 秒
main thread epoll_wait return ret : 1 // 第二次写完后,立即唤醒 main 线程去进行读操作
main thread read 1 (0x1) from event_fd // main 线程读到了数据
main thread epoll is waiting...... // main 线程阻塞又在读端,超时时间为 2 秒
main thread epoll_wait return ret : 0 // main 线程阻塞等待时间到,返回
main thread epoll_wait timed out. continue epoll
main thread epoll is waiting...... // main 线程阻塞又在读端,超时时间为 2 秒
只要没有写入数据,就会在这个死循环中阻塞 -> 超时 -> 阻塞...

eventfd(三)相关推荐

  1. 线程间通信之eventfd

    线程间通信之eventfd man手册中的解释: eventfd()创建了一个"eventfd对象", 通过它能够实现用户态程序间(我觉得这里主要指线程而非进程)的等待/通知机制, ...

  2. 让事件飞——Linux eventfd 原理

    让事件飞--Linux eventfd 原理 让事件飞 --Linux eventfd 原理与实践 原文作者:杨阳 eventfd/timerfd 简介 目前越来越多的应用程序采用事件驱动的方式实现功 ...

  3. linux新的API signalfd、timerfd、eventfd使用说明

    三种新的fd加入linux内核的的版本: signalfd:2.6.22 timerfd:2.6.25 eventfd:2.6.22 三种fd的意义: signalfd:传统的处理信号的方式是注册信号 ...

  4. linux新的API signalfd、timerfd、eventfd使用说明——eventfd

    好久没更新了,今天看一下第三种新的fd:eventfd类似于管道的概念,可以实现线程间的事件通知,所不同的是eventfd的缓冲区大小是sizeof(uint64_t)也就是8字节,它是一个64位的计 ...

  5. Linux C语言在用户态实现一个低时延通知(eventfd)+轮询(无锁队列ring)机制的消息队列

    目录 fastq.c fastq.h test-0.c test-1.c https://github.com/Rtoax/test/tree/master/ipc/github/fastq fast ...

  6. Android茫茫半年求职路,终于斩获三个大厂offer,我总结了24家大厂100份面试题

    synchronized中的类锁和对象锁互斥么? 讲下Java的双亲委派. 泛型. 反射. 注解. 由于篇幅有限,仅展示部分内容,所有的知识点 整理的详细内容都放在了我的[GitHub],有需要的朋友 ...

  7. 应用与系统稳定性第三篇---FD泄露问题漫谈

    cat /proc/pid/limits 查看最大打开文件Max open files cat /proc/pid/fd 查看打开文件 cat /proc/sys/kernel/threads-max ...

  8. Linux 设备驱动程序(三)

    系列文章目录 Linux 内核设计与实现 深入理解 Linux 内核(一) 深入理解 Linux 内核(二) Linux 设备驱动程序(一) Linux 设备驱动程序(二) Linux 设备驱动程序( ...

  9. muduo源码剖析——以三个切片浅析muduo库代码设计的严谨性、高效性与灵活性

    0 前言 陈硕大佬的muduo网络库的源码我已经看了好久了,奈何本人实力有限,每每看到其代码设计的精巧之处只能内心称赞,无法用言语表达出来.实在令人汗颜.最近在看到网络设计部分时有了一些体会,结合自己 ...

最新文章

  1. 《软件测试经验与教训》之二——测试内容先后顺序
  2. JavaScript事件与jQuery方法
  3. spring中aop事务
  4. Magento教程 2:Magento 社群版安装教学!
  5. Java并发编程:同步容器
  6. 【报告分享】2021年微信视频号半年度生态趋势调查报告.pdf(附下载链接)
  7. Merkle Patricia Tree 详解
  8. 有关Silverlight浮动窗体组件的研究——Silverlight学习笔记(3)
  9. 关于树节点巨多时获取用户选中(可多选)节点的优化
  10. 为什么你会觉得微服务架构很别扭
  11. 从我的公众号谈执行力
  12. Windows远程桌面及其相关问题
  13. 模拟投硬币,一次一投
  14. 远程服务器维护工具,免费小工具轻松实现多台服务器维护
  15. 【python绘图】Matplotlib绘图及设置(使用python制图)
  16. 数字孪生典型应用案例
  17. 微信crm平台是什么系统?
  18. Camtasia Studio 6录制视频时鼠标闪烁的解决办法
  19. 王兴:淘宝为什么还不支持微信支付?
  20. 利用Java实现微信公众号发送信息提醒通知

热门文章

  1. 学习编程,英语很重要!!
  2. SVN+AnkhSVN端配置
  3. mysql为什么不能插入数据_mysql为啥不能插入数据
  4. onpagefinished等了很久才执行_今天自律了吗?停课不停锻炼 才是战疫正确姿势
  5. 质性研究工具_质性研究【001】
  6. 有关输出图形的代码,我觉得好难啊,好蒙啊。
  7. 视觉平衡与物理平衡_设计中的平衡理论为什么这么重要?
  8. SIFT讲解(SIFT的特征点选取以及描述是重点)
  9. console java_Java Console writer()方法与示例
  10. lumanager mysql密码_LuManager单独安装mysqli