【linux】操作系统的事件通知机制eventfd

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值