gpt4 book ai didi

写入文件描述符时的 epoll 行为

转载 作者:行者123 更新时间:2023-12-04 00:46:08 26 4
gpt4 key购买 nike

我正在使用 epoll 将大消息写入使用 HTTP 协议(protocol)的服务器。 fds 都设置为非阻塞,我正在使用边缘触发事件。我知道对于 EPOLLIN,我需要循环读取 fd,直到返回 EAGAIN。对于写作,我不确定是否应该在获得 EAGAIN 后继续循环,还是应该等待 epoll 通知 fd 何时可再次读取。

例如,我正在写一条 20K 的消息,在第一次::write 尝试时,发送的数据量 = 13K。下一次写入尝试返回 retVal = -1 和 errno = EAGAIN。

此时我应该继续在 while(1) 中循环直到可以写入数据,还是应该在 FD 准备好再次写入时等待 epoll 调用我的回调。我的理解是,既然fd是注册写的,那么当fd准备好再次写的时候,epoll应该会通知我。但这似乎并没有在我的程序中发生。

我是否需要设置特殊标志或修改 FD 才能收到通知?

最佳答案

At this point should I continue looping in a while(1) until I can write the data



不!

or should I wait for epoll to invoke my call back when the FD is ready for writing again.



是的,你应该(但是什么回调? epoll_wait 没有回调机制,它只是返回)

My understanding is that since the fd is registered for writing, epoll should notify me when the FD is ready for writing again. But that doesn't seem to be happening in my program.



如果 FD 注册到 EPOLLOUTEPOLLIN | EPOLLOUT ,确实应该。你能提供一个小例子来说明这个问题吗?

关于写入文件描述符时的 epoll 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9853166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com