转载请注明来源:https://www.cnblogs.com/hookjc/

eventfd 在内核版本,2.6.22以后有效。查看内核版本可以用命令 uname -r 。
  1. #include<sys/eventfd.h>
  2. int eventfd(unsigned int initval,int flags);

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

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

EFD_CLOEXEC 我的理解是,这个标识被设置的话,调用exec后会自动关闭文件描述符,防止泄漏。

如果是2.6.26或之前版本的内核,flags 必须设置为0。

创建这个对象后,可以对其做如下操作。

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

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

poll select epoll

close 当不需要eventfd的时候可以调用close关闭, 当这个对象的所有句柄都被关闭的时候,内核会释放资源。 为什么不是close就直接释放呢, 如果调用fork 创建

进程的时候会复制这个句柄到新的进程,并继承所有的状态。

(ps:也就是说,在write之后没有read,但是又write新的数据,那么读取的是这两次的8个字节的和,在read之后再write,可以完成read和write之间的交互)

一个例子:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <stdint.h>
  5. #include <pthread.h>
  6. #include <sys/eventfd.h>
  7. #include <sys/epoll.h>
  8. int efd = -1;
  9. void *read_thread(void *dummy)
  10. {
  11. int ret = 0;
  12. uint64_t count = 0;
  13. int ep_fd = -1;
  14. struct epoll_event events[10];
  15. if (efd < 0)
  16. {
  17. printf("efd not inited.\n");
  18. goto fail;
  19. }
  20. ep_fd = epoll_create(1024);
  21. if (ep_fd < 0)
  22. {
  23. perror("epoll_create fail: ");
  24. goto fail;
  25. }
  26. {
  27. struct epoll_event read_event;
  28. read_event.events = EPOLLHUP | EPOLLERR | EPOLLIN;
  29. read_event.data.fd = efd;
  30. ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, efd, &read_event);
  31. if (ret < 0)
  32. {
  33. perror("epoll ctl failed:");
  34. goto fail;
  35. }
  36. }
  37. while (1)
  38. {
  39. ret = epoll_wait(ep_fd, &events[0], 10, 5000);
  40. if (ret > 0)
  41. {
  42. int i = 0;
  43. for (; i < ret; i++)
  44. {
  45. if (events[i].events & EPOLLHUP)
  46. {
  47. printf("epoll eventfd has epoll hup.\n");
  48. goto fail;
  49. }
  50. else if (events[i].events & EPOLLERR)
  51. {
  52. printf("epoll eventfd has epoll error.\n");
  53. goto fail;
  54. }
  55. else if (events[i].events & EPOLLIN)
  56. {
  57. int event_fd = events[i].data.fd;
  58. ret = read(event_fd, &count, sizeof(count));
  59. if (ret < 0)
  60. {
  61. perror("read fail:");
  62. goto fail;
  63. }
  64. else
  65. {
  66. struct timeval tv;
  67. gettimeofday(&tv, NULL);
  68. printf("success read from efd, read %d bytes(%llu) at %lds %ldus\n",
  69. ret, count, tv.tv_sec, tv.tv_usec);
  70. }
  71. }
  72. }
  73. }
  74. else if (ret == 0)
  75. {
  76. /* time out */
  77. printf("epoll wait timed out.\n");
  78. break;
  79. }
  80. else
  81. {
  82. perror("epoll wait error:");
  83. goto fail;
  84. }
  85. }
  86. fail:
  87. if (ep_fd >= 0)
  88. {
  89. close(ep_fd);
  90. ep_fd = -1;
  91. }
  92. return NULL;
  93. }
  94. int main(int argc, char *argv[])
  95. {
  96. pthread_t pid = 0;
  97. uint64_t count = 0;
  98. int ret = 0;
  99. int i = 0;
  100. efd = eventfd(0, 0);
  101. if (efd < 0)
  102. {
  103. perror("eventfd failed.");
  104. goto fail;
  105. }
  106. ret = pthread_create(&pid, NULL, read_thread, NULL);
  107. if (ret < 0)
  108. {
  109. perror("pthread create:");
  110. goto fail;
  111. }
  112. for (i = 0; i < 5; i++)
  113. {
  114. count = 4;
  115. ret = write(efd, &count, sizeof(count));
  116. if (ret < 0)
  117. {
  118. perror("write event fd fail:");
  119. goto fail;
  120. }
  121. else
  122. {
  123. struct timeval tv;
  124. gettimeofday(&tv, NULL);
  125. printf("success write to efd, write %d bytes(%llu) at %lds %ldus\n",
  126. ret, count, tv.tv_sec, tv.tv_usec);
  127. }
  128. sleep(1);
  129. }
  130. fail:
  131. if (0 != pid)
  132. {
  133. pthread_join(pid, NULL);
  134. pid = 0;
  135. }
  136. if (efd >= 0)
  137. {
  138. close(efd);
  139. efd = -1;
  140. }
  141. return ret;
  142. }

  1. success write to efd, write 8 bytes(4) at 1328805612s 21939us
  2. success read from efd, read 8 bytes(4) at 1328805612s 21997us
  3. success write to efd, write 8 bytes(4) at 1328805613s 22247us
  4. success read from efd, read 8 bytes(4) at 1328805613s 22287us
  5. success write to efd, write 8 bytes(4) at 1328805614s 22462us
  6. success read from efd, read 8 bytes(4) at 1328805614s 22503us
  7. success write to efd, write 8 bytes(4) at 1328805615s 22688us
  8. success read from efd, read 8 bytes(4) at 1328805615s 22726us
  9. success write to efd, write 8 bytes(4) at 1328805616s 22973us
  10. success read from efd, read 8 bytes(4) at 1328805616s 23007us
  11. epoll wait timed out.

同时他也是支持进程间通信的,过程和这个差不多。

来源:python脚本自动迁移

linux内核中的eventfd的更多相关文章

  1. Linux 内核中的 Device Mapper 机制

    本文结合具体代码对 Linux 内核中的 device mapper 映射机制进行了介绍.Device mapper 是 Linux 2.6 内核中提供的一种从逻辑设备到物理设备的映射框架机制,在该机 ...

  2. 向linux内核中添加外部中断驱动模块

    本文主要介绍外部中断驱动模块的编写,包括:1.linux模块的框架及混杂设备的注册.卸载.操作函数集.2.中断的申请及释放.3.等待队列的使用.4.工作队列的使用.5.定时器的使用.6.向linux内 ...

  3. Linux内核中双向链表的经典实现

    概要 前面一章"介绍双向链表并给出了C/C++/Java三种实现",本章继续对双向链表进行探讨,介绍的内容是Linux内核中双向链表的经典实现和用法.其中,也会涉及到Linux内核 ...

  4. Linux内核中的fastcall和asmlinkage宏

    代码中看见:#define _fastcall 所以了解下fastcall -------------------------------------------------------------- ...

  5. Linux内核中的GPIO系统之(3):pin controller driver代码分析

    一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道,pin control subsystem也不例外,被它驱动的硬件叫做pin controller(一般ARM soc的datash ...

  6. (十)Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  7. Apparmor——Linux内核中的强制访问控制系统

      AppArmor 因为最近在研究OJ(oline judge)后台的安全模块的实现,所以一直在研究Linux下沙箱的东西,同时发现了Apparmor可以提供访问控制. AppArmor(Appli ...

  8. KSM剖析——Linux 内核中的内存去耦合

    简介: 作为一个系统管理程序(hypervisor),Linux® 有几个创新,2.6.32 内核中一个有趣的变化是 KSM(Kernel Samepage Merging)  允许这个系统管理程序通 ...

  9. linux内核中的get_user和put_user

    linux内核中的get_user和put_user 在 内核空间和用户空间交换数据时,get_user和put_user是两个两用的函数.相对于copy_to_user和 copy_from_use ...

随机推荐

  1. css中cursor(光标类型)

    值 描述 url 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一种普通的光标,以防没有由 URL 定义的可用光标. default 默认光标(通常是一个箭头) auto 默认.浏览器 ...

  2. ProtoBuf3语法指南(Protocol Buffers)_下

    0.说明 ProtoBuf3语法指南, 又称为proto3, 是谷歌的Protocol Buffers第3个版本. 本文基于官方英文版本翻译, 加上了自己的理解少量修改, 一共分为上下两部分. 1.A ...

  3. js- float类型相减 出现无限小数的问题

    6.3 -1.1 是不是应该等于5.2? 但是js 会导致得出 5.19999999999的结果 怎么办?可以先先乘100 后相减,然是用方法 舍入为最接近的整数,然后再除于100, Math.rou ...

  4. Echart可视化学习(四)

    文档的源代码地址,需要的下载就可以了(访问密码:7567) https://url56.ctfile.com/f/34653256-527823386-04154f 正文: 地图模块高度为 810px ...

  5. 老旧业务重构案例——IM系统如何设计

    一年半之前刚来到这个团队,便遭遇了一次挑战: 当时有个CRM系统,老是出问题,之前大的优化进行了4次小的优化进行了10多次,要么BUG重复出现,要么性能十分拉胯,总之体验是否糟糕!技术团队因此受到了诸 ...

  6. 【洛谷】P1067 多项式输出

    原题链接:P1067 多项式输出 题目分析:学长推荐的OJ网站 --洛谷,发现挺好用的还可以下载提交出错的数据. 废话就不多说了,这道题属于基础题.提交出错主要是因为一些小细节不到位,这里就不一一赘述 ...

  7. 不会一致性hash算法,劝你简历别写搞过负载均衡

    大家好,我是小富~ 个人公众号:程序员内点事,欢迎学习交流 这两天看到技术群里,有小伙伴在讨论一致性hash算法的问题,正愁没啥写的题目就来了,那就简单介绍下它的原理.下边我们以分布式缓存中经典场景举 ...

  8. Python与Javascript相互调用超详细讲解(2022年1月最新)(三)基本原理Part 3 - 通过C/C++联通

    目录 TL; DR python调javascript javascript调python 原理 基于Node.js的javascript调用python 从Node调用python函数 V8 嵌入P ...

  9. rocketmq之延迟队列(按照18个等级来发送)

    1 启动消费者等待传入的订阅消息 import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache ...

  10. php中关于数据库的操作

    php中对数据库的操作,举例如下: //连接到本地mysql数据库 $myconn=mysql_connect("localhost","root"," ...