使用边缘触发的 epoll,我应该循环发送吗?
我正在使用 epoll 编写媒体服务器。 fds 全部设置为非阻塞,我正在使用边缘触发事件。我知道对于 EPOLLIN,我需要循环读取 fd,直到返回 EAGAIN。但是写作呢?
当我想写入时,我将数据排队并在 fd 上设置 EPOLLOUT|EPOLLIN|EPOLLET。当 EPOLLOUT 事件发生时,我一次性写入整个排队缓冲区
n = send ( fd, buf, buf_len, MSG_NOSIGNAL );
: 0 && n < buf_len 我只是重置EPOLLOUT并返回。我没有看到循环发送的意义(我认为 epoll 的手册页暗示了这一点)。看起来 send 已经表明它刚刚接收了所有它能接收到的信息,并且如果立即调用将返回 EAGAIN。
在这里消除系统调用是最有效的途径吗?
I'm using epoll to write a media server. The fds are all set to non-blocking and I'm using edge-triggered events. I know for EPOLLIN I need to loop over reading the fd until EAGAIN is returned. But what about writing?
When I want to write I queue the data and set EPOLLOUT|EPOLLIN|EPOLLET on the fd. When the EPOLLOUT event occurs I write the whole queued buffer in one shot:
n = send ( fd, buf, buf_len, MSG_NOSIGNAL );
If n > 0 && n < buf_len I just reset EPOLLOUT and return. I don't see the sense in looping over send (which I think the man page for epoll implies). It seems like send has indicated it has just taken in all it can and will return EAGAIN if called immediately.
Is eliminating a system call here the most efficient route?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
epoll man page 说:
对于面向流的文件(例如,管道、FIFO、流套接字),读/写 I/O 空间耗尽的情况也可以通过检查从/写入的数据量来检测。目标文件描述符。 例如,如果您通过要求读取一定量的数据来调用 read(2),并且 read(2) 返回的字节数较少,则可以确定已耗尽该文件的读取 I/O 空间描述符。使用 write(2) 进行写入时也是如此。(如果无法保证受监视的文件描述符始终引用面向流的文件,请避免使用后一种技术。)
epoll man page says:
For stream-oriented files (e.g., pipe, FIFO, stream socket), the condition that the read/write I/O space is exhausted can also be detected by checking the amount of data read from / written to the target file descriptor. For example, if you call read(2) by asking to read a certain amount of data and read(2) returns a lower number of bytes, you can be sure of having exhausted the read I/O space for the file descriptor. The same is true when writing using write(2). (Avoid this latter technique if you cannot guarantee that the monitored file descriptor always refers to a stream-oriented file.)
我已经做了很多测试,似乎循环发送是浪费时间。
I've done a bunch of tests and it seems like looping over send is a waste of time.