eventfd
#include <sys/eventfd.h>
int eventfd(unsigned int initval, int flags);
eventfd() creates an "eventfd object" that can be used as an event wait/notify mechanism by user-space applications, and by the kernel to
notify user-space applications of events. The object contains an unsigned 64-bit integer (uint64_t) counter that is maintained by the ker‐
nel. This counter is initialized with the value specified in the argument initval.
The following values may be bitwise ORed in flags to change the behaviour of eventfd():
EFD_CLOEXEC (since Linux 2.6.27)
Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor. See the description of the O_CLOEXEC flag in open(2) for rea‐
sons why this may be useful.
EFD_NONBLOCK (since Linux 2.6.27)
Set the O_NONBLOCK file status flag on the new open file description. Using this flag saves extra calls to fcntl(2) to achieve the
same result.
EFD_SEMAPHORE (since Linux 2.6.30)
Provide semaphore-like semantics for reads from the new file descriptor. See below.
In Linux up to version 2.6.26, the flags argument is unused, and must be specified as zero.
As its return value, eventfd() returns a new file descriptor that can be used to refer to the eventfd object. The following operations can
be performed on the file descriptor:
read(2)
Each successful read(2) returns an 8-byte integer. A read(2) will fail with the error if the size of the supplied buffer is
less than 8 bytes.
The value returned by read(2) is in host byte order, i.e., the native byte order for integers on the host machine.
The semantics of read(2) depend on whether the eventfd counter currently has a nonzero value and whether the EFD_SEMAPHORE flag was
specified when creating the eventfd file descriptor:
* If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero value, then a read(2) returns 8 bytes containing that
value, and the counter's value is reset to zero.
如果没有指定EFD_SEMAPHORE,同时eventfd计数器的值非零,那么read将会读出计数器的值,该值的长度为8个字节。
* If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value, then a read(2) returns 8 bytes containing the value
1, and the counter's value is decremented by 1.
如果指定了EFD_SEMAPHORE,同时eventfd计数器的值非零,那么read读出的值只为1,并且eventfd计数器的值减1.
综上所述,没有指定EFD_SEMAPHORE,read一次读出计数器的值,计数器的值归零。如果指定了EFD_SEMAPHORE,read一次只返回1,计数器只减1.
那么问题来了,一般EFD_SEMAPHORE用在什么场景呢?
* If the eventfd counter is zero at the time of the call to read(2), then the call either blocks until the counter becomes nonzero
(at which time, the read(2) proceeds as described above) or fails with the error EAGAIN if the file descriptor has been made non‐
blocking.
当计数器的值为0时,调用read函数,将会阻塞,直到计数器的值非零。如果文件描述符设置为非阻塞,那么会失败并返回EAGAIN.
write(2)
A write(2) call adds the 8-byte integer value supplied in its buffer to the counter. The maximum value that may be stored in the
counter is the largest unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe). If the addition would cause the counter's value to
exceed the maximum, then the write(2) either blocks until a read(2) is performed on the file descriptor, or fails with the error
EAGAIN if the file descriptor has been made nonblocking.
如果计数器的值超过最大值,write函数将会阻塞,直到read函数被调用。如果fd设置为非阻塞,那么失败并返回EAGAIN.
A write(2) will fail with the error EINVAL if the size of the supplied buffer is less than 8 bytes, or if an attempt is made to
write the value 0xffffffffffffffff.
poll(2), select(2) (and similar)
The returned file descriptor supports poll(2) (and analogously epoll(7)) and select(2), as follows:
* The file descriptor is readable (the select(2) readfds argument; the poll(2) POLLIN flag) if the counter has a value greater than
0.
* The file descriptor is writable (the select(2) writefds argument; the poll(2) POLLOUT flag) if it is possible to write a value of
at least "1" without blocking.
* If an overflow of the counter value was detected, then select(2) indicates the file descriptor as being both readable and
writable, and poll(2) returns a POLLERR event. As noted above, write(2) can never overflow the counter. However an overflow can
occur if 2^64 eventfd "signal posts" were performed by the KAIO subsystem (theoretically possible, but practically unlikely). If
an overflow has occurred, then read(2) will return that maximum uint64_t value (i.e., 0xffffffffffffffff).
The eventfd file descriptor also supports the other file-descriptor multiplexing APIs: pselect(2) and ppoll(2).
close(2)
When the file descriptor is no longer required it should be closed. When all file descriptors associated with the same eventfd
object have been closed, the resources for object are freed by the kernel.
A copy of the file descriptor created by eventfd() is inherited by the child produced by fork(2). The duplicate file descriptor is associ‐
ated with the same eventfd object. File descriptors created by eventfd() are preserved across execve(2), unless the close-on-exec flag has
been set.
RETURN VALUE
On success, eventfd() returns a new eventfd file descriptor. On error, -1 is returned and errno is set to indicate the error.
ERRORS
EINVAL An unsupported value was specified in flags.
EMFILE The per-process limit on open file descriptors has been reached.
ENFILE The system-wide limit on the total number of open files has been reached.
ENODEV Could not mount (internal) anonymous inode device.
ENOMEM There was insufficient memory to create a new eventfd file descriptor.
VERSIONS
eventfd() is available on Linux since kernel 2.6.22. Working support is provided in glibc since version 2.8. The eventfd2() system call
(see NOTES) is available on Linux since kernel 2.6.27. Since version 2.9, the glibc eventfd() wrapper will employ the eventfd2() system
call, if it is supported by the kernel.
NOTES
Applications can use an eventfd file descriptor instead of a pipe (see pipe(2)) in all cases where a pipe is used simply to signal events.
The kernel overhead of an eventfd file descriptor is much lower than that of a pipe, and only one file descriptor is required (versus the
two required for a pipe).
When used in the kernel, an eventfd file descriptor can provide a bridge from kernel to user space, allowing, for example, functionalities
like KAIO (kernel AIO) to signal to a file descriptor that some operation is complete.
A key point about an eventfd file descriptor is that it can be monitored just like any other file descriptor using select(2), poll(2), or
epoll(7). This means that an application can simultaneously monitor the readiness of "traditional" files and the readiness of other kernel
mechanisms that support the eventfd interface. (Without the eventfd() interface, these mechanisms could not be multiplexed via select(2),
poll(2), or epoll(7).)
eventfd的更多相关文章
- linux之eventfd()
参考:http://www.man7.org/linux/man-pages/man2/eventfd.2.html 一.简介 简单来说,这个函数就是创建一个用于事件通知的文件描述符.它类似于pipe ...
- [转] linux新的API signalfd、timerfd、eventfd使用说明
http://blog.csdn.net/gdutliuyun827/article/details/8460417 三种新的fd加入linux内核的的版本: signalfd:2.6.22 time ...
- Linux使用定时器timerfd 和 eventfd接口实现进程线程通信
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- timerfd与eventfd
1.timerfd timerfd是定时器描述符,通过timerfd_create()来创建它,timerfd_settime()来设置定时器时间,当时间到期定时器文件描述符就可读,所以能够在sele ...
- muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制
目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...
- Linux eventfd分析
2017-07-20 eventfd在linux中是一个较新的进程通信方式,和信号量等不同的是event不仅可以用于进程间的通信,还可以用户内核发信号给用户层的进程.eventfd在virtIO后端驱 ...
- linux新的API signalfd、timerfd、eventfd使用说明
原文:http://www.cfanz.cn/?c=article&a=read&id=46555注意很多当前(2013/8/6)线上运营的Linux内核可能不支持! 三种新的fd加入 ...
- eventfd(2) 结合 select(2) 源码分析
eventfd(2) 结合 select(2) 源码分析 本文代码选自内核 4.17 eventfd(2) - 创建一个文件描述符用于事件通知. 使用 源码分析 参考 #include <sys ...
- eventfd(2) 结合 select(2) 分析
本文代码选自内核 4.17 eventfd(2) - 创建一个文件描述符用于事件通知. 使用 源码分析 参考 #include <sys/eventfd.h> int eventfd(un ...
随机推荐
- 【bzoj1085】【 [SCOI2005]骑士精神】启发式剪枝+迭代加深搜索
(上不了p站我要死了,侵权度娘背锅) 如果这就是启发式搜索的话,那启发式搜索也不是什么高级玩意嘛..(啪啪打脸) Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且 ...
- Flash3D学习计划(三)——学习VB,IB相关,理解三角形顶点顺序;在屏幕上显示2D矩形,并实现缩放,平移,旋转
VB:顶点缓冲 IB: 顶点索引缓冲 三角形的顶点顺序决定了三角形是顺时针还是逆时针,从而决定了三角形在背面剔除的过程中是否会被剔除掉. 相关理论知识可以在前面的文章中找到更多的说明. 实现效果 sf ...
- 动态路由协议(3)--ospf
1.设置pc ip 网关 192.168.1.1 192.168.1.254 192.168.4.1 192.168.4.254 2.设置路由器 (1)设置接口ip Router(config-/ R ...
- 移动 web 1px 边框解决方案
在移动端web页面开发中,为了使css中使用的尺寸与设计稿一致,通常会采用 rem 单位配合 lib-flexible 来实现移动端的适配,在IOS设备上 flexible.js 会根据设备的分辨率动 ...
- Flex 布局学习笔记
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- javascript 对象属性的添加,删除,json对象和字符串转换方法等
1:动态添加 对象属性 var obj = new Object(); console.log (obj.username); obj.username = "haha"; con ...
- 为什么代理属性设置成assign为了防止生成保留环来
循环引用 全部的引用计数系统, 都存在循环应用的问题, 比如以下的引用关系: 1. 对象a创建并引用到了对象b 2. 对象b创建并引用到了对象c 3. 对象c创建并引用到了对象b 这时候b和c的引用计 ...
- 【Hadoop】HADOOP 总结--思维导图
- 2017.7.18 linux下ELK环境搭建
参考来自:Linux日志分析ELK环境搭建 另一篇博文:2017.7.18 windows下ELK环境搭建 0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1 ...
- elasticsearch 基础性操作
1 基础概念 Elasticsearch是一个近实时的系统,从你写入数据到数据可以被检索到,一般会有1秒钟的延时.Elasticsearch是基于Lucene的,Lucene的读写是两个分开的句柄,往 ...