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?
推荐答案
当您使用 select()
,您正在尝试检查一组文件描述符的状态.您感兴趣的文件描述符的可能范围从 0(标准输入)的低值到某个最大值(您有兴趣检查其状态的已打开的最高文件描述符).您必须告诉 select()
文件描述符列表有多大,因为总数可能很大(例如 32767).在这种情况下,内核处理描述符需要时间,而且您可能还没有将 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()
进行测试,并且通常您必须在再次调用 select()
之前重做 fd_set
的初始化(或复制保存的值).
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屋!