每当我在 C 中看到 select
调用时,我都会看到它写为:
select(sock_fd + 1, &fdset, NULL, NULL, &tv)
或者类似的东西。增加文件描述符背后的含义是什么?
最佳答案
来自POSIX specification of select
:
The
nfds
argument specifies the range of descriptors to be tested. The firstnfds
descriptors shall be checked in each set; that is, the descriptors from zero throughnfds-1
in the descriptor sets shall be examined.
也就是说,您给出集合的大小,其中每个描述符都是一个索引。
描述符集基本上实现为数组,sock_fd
(在您的情况下)是该数组的索引,而sock_fd + 1
是要检查的数组的大小。
关于c - 为什么调用 select 时套接字文件描述符参数会加一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050819/