eventfd,是操作系统提供的事件通知机制,详情查看eventfd。
通过文件使用的形式,让俩个模块之间可以有交互,在一定程度上可以使用在进程间通信。
事件机制是一种典型的生产和消费的模式,对于eventfd,有俩种主要的使用方式。
- 纯通知模式
- 类似信号模式(打开eventfd的时候加上EFD_SEMAPHORE)
俩种的差别是,纯通知模式无论你write几次,最后read的时候,事件就立马清0;这个时候你读取的值是之前累加的值,因为它是一个counter。
信号模式就是,你每次read,都是read一个信号出来。之前写入几次,你就可以read几次。
read(2)
Each successful read(2) returns an 8-byte integer. A
read(2) fails with the error EINVAL if the size of the
supplied buffer is less than 8 bytes.
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:
1、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.
2、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.
3、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) o