Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

epoll_wait 返回事件的类型判断处理疑问? #297

Closed
stormzy opened this issue Mar 19, 2021 · 2 comments
Closed

epoll_wait 返回事件的类型判断处理疑问? #297

stormzy opened this issue Mar 19, 2021 · 2 comments

Comments

@stormzy
Copy link

stormzy commented Mar 19, 2021

image
__poller_thread_routine 函数中的 __poller_wait 调用在linux下是通过 epoll_wait 来检测 fd 发生的事件。
fd及其事件信息被保存在 events 这个 epoll_event 结构体数组中。个人理解是 epoll_event.events 是操作系统告诉本程序该fd
当前发生的事件类型。但是下面判断 fd 发生的事件类型是通过 node->data.operation 来判断的。这个个人理解是注册 fd 到
epoll 的时候所要注册的事件。而且直接用switch来判断,所以如果一个 fd 同时关注读写事件的话,这里个人感觉是有点疑问。

@Barenboim
Copy link
Contributor

你好。难得有用户看得这么仔细。为了性能考虑我们的poller_node是一个以fd为下标的数组,而每个node只能关注一种事件,READ或WRITE。所有我们需要通过operation来判断调用哪个处理函数,而不能通过event来判断。
也就是说,对于一个fd,在poller里是单工的。如果需要全双工,方法是通过系统调用dup()产生一个新的fd再加进来。我们现在准备用来做websocket的代码就是这样实现的。一个fd占用的系统资源非常小,而且只有当同步写不能将数据全部写入时,才需要再产生一个fd进行异步写。

@stormzy
Copy link
Author

stormzy commented Mar 20, 2021

明白了,感谢耐心解答。 长知识长知识啦。也了解了一种新的思路。谢谢谢谢。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants