重要的是如何设置的最大的fd上选择? [英] how important is setting max fd on select?
问题描述
在一个无限循环中,我使用选择听100+文件描述符。如果FD具有一定的包准备好被读取,我通知分配给该文件描述符的包处理器线程,我没有为下一轮该文件描述符的比特,直到我收到来自数据处理器线程说是做的通知。我不知道我的code将如何低效的,如果我将不计算最大。 FD的选择,每次我清楚/套起订的文件描述符。我期待文件描述符是几乎连续的,数据到达率是几千字节每秒每个FD。
Within an infinite loop, I am listening 100+ file descriptors using select. If fd has some packets ready to be read, I notify the packet processor thread assigned to this file descriptor and I don't set the bit for this file descriptor for the next round until I receive a notification from data processor thread saying it is done. I wonder how inefficient my code would be if I won't calculate the max. fd for select everytime I clear/set a file descriptor from the set. I am expecting file descriptors to be nearly contiguous, data arrival rate to be a few thousands bytes every second for each fd.
推荐答案
您真的应该使用调查
而不是选择
。两者都是标准的,但调查
更容易使用,不把一个上限,您可以检查文件描述符的数量(而选择
限制你的编译时间常数 FD_SETSIZE
),更高效。如果你使用选择
,你可以随时通过 FD_SETSIZE
的第一个参数,但是这当然会给予最差情况下的性能,因为内核必须扫描整个 FD_SET
;通过实际的MAX + 1允许较短的搜索,但仍然没有有效的数组传递给调查
。
You should really use poll
instead of select
. Both are standard, but poll
is easier to use, does not place a limit on the number of file descriptors you can check (whereas select
limits you to the compile-time constant FD_SETSIZE
), and more efficient. If you do use select
, you can always pass FD_SETSIZE
for the first argument, but this will of course give worst-case performance since the kernel has to scan the whole fd_set
; passing the actual max+1 allows a shorter search, but still not as efficient as the array passed to poll
.
有关它的价值,这几天好像时尚使用非标准的Linux 的epoll
或任何BSD当量。这些接口可具有一定的优势,如果你有一个庞大的数字长寿命(至少几个往返)连接的(数万的量级),但在其他性能不会noticably更好(并且,在下端,可能会更糟),而且这些接口当然是不可移植的,在我看来,更难比平原正确使用,便携调查
。
For what it's worth, these days it seems stylish to use the nonstandard Linux epoll
or whatever the BSD equivalent is. These interfaces may have some advantages if you have a huge number (on the order of tens of thousands) of long-lived (at least several round trips) connections, but otherwise performance will not be noticably better (and, at the lower end, may be worse), and these interfaces are of course non-portable, and in my opinion, harder to use correctly than the plain, portable poll
.
这篇关于重要的是如何设置的最大的fd上选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!