select()中的nfds用于什么 [英] What is the nfds from select() used for
问题描述
我想知道nfds
的作用,在阅读了不同的手册后,我得到的唯一答案是它是编号最高的文件描述符加一个.它到底是干什么用的?
I was wondering what the nfds
does, after reading different manuals, I end up with the only answer being it is the highest numbered file descriptor plus one. What is it exactly used for?
推荐答案
使用fd_set直到达到该条目数. FD_SETSIZE
也在等式中显示数字,但有时您可以更改该值.
When you're using select()
, you are trying to check the status of a set of file descriptors. The possible range of file descriptors you're interested in ranges from a low of 0 (standard input) to some maximum value (the highest file descriptor you have open that you're interested in checking the status of). You have to tell select()
how big the list of file descriptors is because the total number can be 'vast' (32767, for example). In that case, it takes time for the kernel to process the descriptors, plus you may not have initialized the fd_set
up to that number of entries. FD_SETSIZE
also figures in the equation, but sometimes you can change that value.
因此,如果要监视文件描述符24-31,请将nfds
设置为32,并确保使用FD_ZERO()
将整个fd_set
和FD_SET()
设置为零,以设置条目24- 31.还要注意,select()
会修改输入参数,因此您必须在select()
返回后使用FD_ISSET()
进行测试,并且通常必须重做fd_set
的初始化(或复制保存的值)再次调用select()
之前.
So, if you want to monitor file descriptors 24-31, you'd set nfds
to 32, and ensure that you use FD_ZERO()
to zero the whole fd_set
and FD_SET()
to set entries 24-31. Note, too, that select()
modifies the input parameters, so you have to use FD_ISSET()
to test after the select()
returns, and in general you have to redo the initialization (or copy a saved value) of fd_set
before calling select()
again.
这篇关于select()中的nfds用于什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!