use*_*694 4 linux epoll nonblocking socket.io
我在网上读了文件abount edge触发的epoll函数,如下所示:
1. The file descriptor that represents the read side of a pipe (rfd) is registered on the epoll instance.
2. A pipe writer writes 2 kB of data on the write side of the pipe.
3. A call to epoll_wait(2) is done that will return rfd as a ready file descriptor.
4. The pipe reader reads 1 kB of data from rfd.
5. A call to epoll_wait(2) is done.
.......
.......
Run Code Online (Sandbox Code Playgroud)
使用epoll作为边缘触发(EPOLLET)接口的建议方法如下:i)使用非阻塞文件描述符ii)仅在read(2)或write(2)返回EAGAIN之后为事件调用epoll_wait.
我理解2,但我不知道为什么使用非阻塞文件描述符.
谁能解释为什么使用非阻塞文件描述符的原因?为什么在级别触发的epoll函数中使用阻塞文件描述符是可以的?
当你有一个边缘触发的通知表明存在数据时,我们的想法是尝试完全排空文件描述符.因此,一旦epoll()返回,您将循环遍历read()或write()直到它返回-EAGAIN时没有更多数据.
如果fd被打开阻塞,那么最后read()或者write()也会阻塞,你将没有机会回到epoll()等待整个fds集的调用.当打开非阻塞时,最后read()/ write()确实返回,并且您确实有机会返回轮询.
当epoll()以水平触发的方式使用时,这并不是一个问题,因为在这种情况下,epoll()如果有任何数据,将立即返回.所以(伪代码)循环如:
while (1) {
epoll();
do_read_write();
}
Run Code Online (Sandbox Code Playgroud)
do_read_write()只要有数据,你就可以保证打电话.当使用边缘触发的epoll时,如果在完成do_read_write()和下一次调用之间进入,则有可能通知新数据可用epoll().
| 归档时间: |
|
| 查看次数: |
2656 次 |
| 最近记录: |