在同一个 fd(socket) 上进行 2 个连续的 SELECT 系统调用,其中一个需要时间,而第二个则立即返回,为什么?
我在同一个 fd 上有 2 个相继的“选择”调用。两者都有 diff fd_set,但两者都只有一个 fd int it 和相同的 fd。(尝试从同一个套接字读取)
问题是第二个 Select 超时。 我正在尝试重新创建问题,但不能,在我的测试中,即使超时 = 0,第二个选择也几乎立即完成。
我很困惑。套接字在内核空间中是否有数据,因此第二个选择会立即进行。
I have 2 'Select' calls one after other on same fd. both have diff fd_set, but both have only one fd int it and the same fd.(trying to read from the same socket)
the problem is second Select times out.
I am trying to recreate the issue but cant, in my testing the second select goes through almost instantaneously, even with timeout=0.
I am confused. Does the Socket have data in Kernel Space due to which the second select goes through immediately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,套接字确实在内核空间中缓冲了传入数据 - 这就是您在第一个
select()
返回后调用read()
时得到的数据,表明有一些东西可以阅读。如果您还没有阅读全部内容,那么当然另一个select()
将立即返回。如果您已经调用了
read()
,那么它可能表明可用数据比您已读取的数据多,您应该继续阅读,直到获得全部数据。只有当read()
的调用失败或者在select
指示它可读之后阻塞,或者如果您有理由相信不应该存在这样的情况时,这里才会出现问题。数据遵循您已阅读的内容。Yes, the socket does have incoming data buffered in kernel space - that's what you'll get when you call
read()
, after the firstselect()
returns to indicate that there is something available to read. If you haven't read it all yet, then of course anotherselect()
will return immediately.If you have called
read()
, then it probably indicates that there's more data available than you've read, and you should carry on reading until you've got it all. There's only a problem here if either the call toread()
fails or blocks afterselect
has indicated that it's readable, or if you have reason to believe that there should be no data following what you've already read.