为什么必须在边缘触发的epoll函数中使用非阻塞fd? [英] Why having to use non-blocking fd in a edge triggered epoll function?

查看:754
本文介绍了为什么必须在边缘触发的epoll函数中使用非阻塞fd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络上阅读文档中边缘触发的epoll函数,如下所示:

I read document abount edge triggered epoll function in web as follows:

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.
.......
.......

使用epoll作为边缘触发(EPOLLET)接口的建议方法如下: i)使用非阻塞文件描述符 ii)仅在read(2)或write(2)返回EAGAIN后才调用epoll_wait进行事件.

The suggested way to use epoll as an edge-triggered (EPOLLET) interface is as follows: i) Use nonblocking file descriptors ii) Call epoll_wait for an event only after read(2) or write(2) return EAGAIN.

我理解2,但是我不知道为什么要使用无阻塞文件描述符.

I understood 2, but I couldn't know why nonblocking file descriptors are used.

谁能解释使用非阻塞文件描述符的原因? 为什么可以在级别触发的epoll函数中使用阻止文件描述符?

Could anyone explain the reason why nonblocking file descriptors are used? Why is it all right to use blocking file descriptors in a level triggered epoll function?

推荐答案

这个想法是,当您收到一条边沿触发的通知,通知您有数据时,尝试完全耗尽文件描述符.因此,一旦epoll()返回,您就可以遍历read()write(),直到没有更多数据需要返回-EAGAIN为止.

The idea is to try to completely drain the file descriptor when you have an edge-triggered notification that there is data to be had. So, once epoll() returns, you loop over the read() or write() until it returns -EAGAIN when there is no more data to be had.

如果打开fd阻塞,则最后一个read()write()也将阻塞,您将没有机会回到epoll()调用来等待整个fds.打开无阻塞打开后,最后一个read()/write()确实会返回,您确实有机会回到轮询.

If the fd was opened blocking, then this last read() or write() would also block, and you wouldn't have the chance to go back to the epoll() call to wait on the entire set of fds. When opened nonblocking, the last read()/write() does return, and you do have the chance to go back to polling.

以级别触发的方式使用epoll()时,这并不是什么大问题,因为在这种情况下,如果有任何个数据,epoll()将立即返回.因此,一个(伪代码)循环如下:

This is not so much of a concern when using epoll() in a level-triggered fashion, since in this case epoll() will return immediately if there is any data to be had. So a (pseudocode) loop such as:

while (1) {
  epoll();
  do_read_write();
}

会起作用,因为只要有数据,您就可以保证调用do_read_write().当使用边缘触发的epoll时,如果它出现在do_read_write()的结束与下一次调用epoll()的之间,则有可能会丢失新数据的通知.

would work, as you're guaranteed to call do_read_write() as long as there is data. When using edge-triggered epoll, there is potential for the notification that new data is available to be missed, if it comes in between the finish of do_read_write() and the next call to epoll().

这篇关于为什么必须在边缘触发的epoll函数中使用非阻塞fd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆