PNP: non-blocking IO

When short write happens in non-blocking IO

  • Save remaining data in some buffer
    • Never call write() when buffer is not empty, it reorders data
    • Alternatively, always send from buffer
  • Start watching POLLOUT event
    • Meanwhile, any write() should append the buffer instead
  • When POLLOUT is ready, write from buffer
    • consume buffer
  • If buffer becomes empty, stop watching POLLOUT event
    • Otherwise, it end up with a busy loop

使用非阻塞IO进行写数据时,没有写完要放到一个发送缓冲区中,并注册POLLOUT事件,当事件发生时,将发送缓冲区中的数据发送出去。当发送缓冲区为空时,关闭POLLOUT事件。

What if sink is slow

发送方和接收方速度不匹配时该怎么办?
比如一个proxy服务器。

  • The common pitfall in non-blocking IO
    • Avoid memory exhaustion or dropping messages

高水位回调和低水位回调,当接收缓冲区达到高水位时,停止接收。当接收缓冲区中的数据量慢慢减少到低水位时,在开始接收。

Level-trigger and edge trigger

  • select and poll are level-trigger
  • epoll stands for edge-poll, works in both LT and ET mode
  • No up-to-date benchmarks show which is faster
  • Edge trigger works best for writing and accepting
  • Level trigger works best for reading data
  • Current linux kernel doesn’t support mix LT/ET for on socket
    • muduo uses level trigger

IO-multiplexing in a multithreaded program

one loop per thread

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值