linux内核中的eventfd
转载请注明来源:https://www.cnblogs.com/hookjc/
- #include<sys/eventfd.h>
- 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之间的交互)
一个例子:
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <stdint.h>
- #include <pthread.h>
- #include <sys/eventfd.h>
- #include <sys/epoll.h>
- int efd = -1;
- void *read_thread(void *dummy)
- {
- int ret = 0;
- uint64_t count = 0;
- int ep_fd = -1;
- struct epoll_event events[10];
- if (efd < 0)
- {
- printf("efd not inited.\n");
- goto fail;
- }
- ep_fd = epoll_create(1024);
- if (ep_fd < 0)
- {
- perror("epoll_create fail: ");
- goto fail;
- }
- {
- struct epoll_event read_event;
- read_event.events = EPOLLHUP | EPOLLERR | EPOLLIN;
- read_event.data.fd = efd;
- ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, efd, &read_event);
- if (ret < 0)
- {
- perror("epoll ctl failed:");
- goto fail;
- }
- }
- while (1)
- {
- ret = epoll_wait(ep_fd, &events[0], 10, 5000);
- if (ret > 0)
- {
- int i = 0;
- for (; i < ret; i++)
- {
- if (events[i].events & EPOLLHUP)
- {
- printf("epoll eventfd has epoll hup.\n");
- goto fail;
- }
- else if (events[i].events & EPOLLERR)
- {
- printf("epoll eventfd has epoll error.\n");
- goto fail;
- }
- else if (events[i].events & EPOLLIN)
- {
- int event_fd = events[i].data.fd;
- ret = read(event_fd, &count, sizeof(count));
- if (ret < 0)
- {
- perror("read fail:");
- goto fail;
- }
- else
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- printf("success read from efd, read %d bytes(%llu) at %lds %ldus\n",
- ret, count, tv.tv_sec, tv.tv_usec);
- }
- }
- }
- }
- else if (ret == 0)
- {
- /* time out */
- printf("epoll wait timed out.\n");
- break;
- }
- else
- {
- perror("epoll wait error:");
- goto fail;
- }
- }
- fail:
- if (ep_fd >= 0)
- {
- close(ep_fd);
- ep_fd = -1;
- }
- return NULL;
- }
- int main(int argc, char *argv[])
- {
- pthread_t pid = 0;
- uint64_t count = 0;
- int ret = 0;
- int i = 0;
- efd = eventfd(0, 0);
- if (efd < 0)
- {
- perror("eventfd failed.");
- goto fail;
- }
- ret = pthread_create(&pid, NULL, read_thread, NULL);
- if (ret < 0)
- {
- perror("pthread create:");
- goto fail;
- }
- for (i = 0; i < 5; i++)
- {
- count = 4;
- ret = write(efd, &count, sizeof(count));
- if (ret < 0)
- {
- perror("write event fd fail:");
- goto fail;
- }
- else
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- printf("success write to efd, write %d bytes(%llu) at %lds %ldus\n",
- ret, count, tv.tv_sec, tv.tv_usec);
- }
- sleep(1);
- }
- fail:
- if (0 != pid)
- {
- pthread_join(pid, NULL);
- pid = 0;
- }
- if (efd >= 0)
- {
- close(efd);
- efd = -1;
- }
- return ret;
- }
- success write to efd, write 8 bytes(4) at 1328805612s 21939us
- success read from efd, read 8 bytes(4) at 1328805612s 21997us
- success write to efd, write 8 bytes(4) at 1328805613s 22247us
- success read from efd, read 8 bytes(4) at 1328805613s 22287us
- success write to efd, write 8 bytes(4) at 1328805614s 22462us
- success read from efd, read 8 bytes(4) at 1328805614s 22503us
- success write to efd, write 8 bytes(4) at 1328805615s 22688us
- success read from efd, read 8 bytes(4) at 1328805615s 22726us
- success write to efd, write 8 bytes(4) at 1328805616s 22973us
- success read from efd, read 8 bytes(4) at 1328805616s 23007us
- epoll wait timed out.
同时他也是支持进程间通信的,过程和这个差不多。
来源:python脚本自动迁移
linux内核中的eventfd的更多相关文章
- Linux 内核中的 Device Mapper 机制
本文结合具体代码对 Linux 内核中的 device mapper 映射机制进行了介绍.Device mapper 是 Linux 2.6 内核中提供的一种从逻辑设备到物理设备的映射框架机制,在该机 ...
- 向linux内核中添加外部中断驱动模块
本文主要介绍外部中断驱动模块的编写,包括:1.linux模块的框架及混杂设备的注册.卸载.操作函数集.2.中断的申请及释放.3.等待队列的使用.4.工作队列的使用.5.定时器的使用.6.向linux内 ...
- Linux内核中双向链表的经典实现
概要 前面一章"介绍双向链表并给出了C/C++/Java三种实现",本章继续对双向链表进行探讨,介绍的内容是Linux内核中双向链表的经典实现和用法.其中,也会涉及到Linux内核 ...
- Linux内核中的fastcall和asmlinkage宏
代码中看见:#define _fastcall 所以了解下fastcall -------------------------------------------------------------- ...
- Linux内核中的GPIO系统之(3):pin controller driver代码分析
一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道,pin control subsystem也不例外,被它驱动的硬件叫做pin controller(一般ARM soc的datash ...
- (十)Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- Apparmor——Linux内核中的强制访问控制系统
AppArmor 因为最近在研究OJ(oline judge)后台的安全模块的实现,所以一直在研究Linux下沙箱的东西,同时发现了Apparmor可以提供访问控制. AppArmor(Appli ...
- KSM剖析——Linux 内核中的内存去耦合
简介: 作为一个系统管理程序(hypervisor),Linux® 有几个创新,2.6.32 内核中一个有趣的变化是 KSM(Kernel Samepage Merging) 允许这个系统管理程序通 ...
- linux内核中的get_user和put_user
linux内核中的get_user和put_user 在 内核空间和用户空间交换数据时,get_user和put_user是两个两用的函数.相对于copy_to_user和 copy_from_use ...
随机推荐
- 高效位运算 __builtin_系列函数
•int __builtin_ffs (unsigned int x) 返回x的最后一位1的是从后向前第几位,比如7368(1110011001000)返回4. •int __builtin_clz ...
- 编写Java程序,使用JTable表格组件展现人员信息列表
返回本章节 返回作业目录 需求说明: 使用JTable组件显现人员信息列表 实现思路: 创建一个JTable对象. 创建一个JScrollPane对象(显示横向和纵向滚动条). 将表格添加到滚动面板. ...
- 基于MCRA-OMLSA的语音降噪(三):实现(续)
上篇文章(基于MCRA-OMLSA的语音降噪(二):实现)讲了基于MCRA-OMLSA的语音降噪的软件实现.本篇继续讲,主要讲C语言下怎么对数学库里的求平方根(sqrt()).求自然指数(exp()) ...
- Linux查看RAM内存信息
1.查看/proc/meminfo文件 查看RAM使用情况最简单的方法是通过/proc/meminfo. 这个动态更新的虚拟文件列出了详细的内存使用情况. cat /proc/meminfo 命令输出 ...
- Eclipse提速优化方法
Eclipse提速优化方法 Eclipse运行太慢了,尤其是导入大型maven项目的时候经常动不动就down掉,于是总结了一下Eclipse提速的方法. 1.关闭不常用的工程 打开的项目太多,会严重影 ...
- Win10 开启 Hyper-V 及简单使用
简介 Windows 10 上内置了 Hyper-V.Hyper-V 提供硬件虚拟化,每个虚拟机都在虚拟硬件上运行. 系统要求 Windows 10 企业版.专业版或教育版.家庭版.移动版.移动企业版 ...
- nalu,在java中使用lambda查询数据库
不忘初心 最开始接触写代码的时候,用的是C井,查数据库直接硬编码sql,挺难受的. 后来学习到EntityFramework,用起来是真香,都是强类型,各种智能提示,代码写起来极度舒适,效率起飞. 最 ...
- PowerShell【变量篇】
PS C:\Users\Administrator> $str='这是一个变量' PS C:\Users\Administrator> $str 这是一个变量 PS C:\Users\Ad ...
- SQL Server数据库出现“无法访问数据库XXX(objectExplorer)”的解决办法
数据库版本为2008R2,服务器异常重启并重新挂载iscsi后,数据库出现"无法访问数据库XXX(objectExplorer)"问题. 输入SQL命令查看数据库状态 1 sele ...
- nuxt 相关
https://github.com/xuqiang521/nuxt-ssr-demo https://www.cnblogs.com/laozhang-is-phi/p/10249248.html ...