select() return 除了 -1、0 或 1 之外的任何值?
How would select() return anything other than -1, 0, or 1?
这绝对是我的一个愚蠢的误解,但是 select() 的手册页指出:
The timeout argument specifies the interval that select() should block waiting for a file descriptor to become ready. The call will block until either:
*a file descriptor becomes ready;
*the call is interrupted by a signal handler; or
*the timeout expires.
此外,
On success, select() and pselect() return the number of file
descriptors contained in the three returned descriptor sets (that is,
the total number of bits that are set in readfds, writefds,
exceptfds) which may be zero if the timeout expires before anything
interesting happens. On error, -1 is returned, and errno is set to
indicate the error; the file descriptor sets are unmodified, and
timeout becomes undefined.
所以我的问题是——如果它在文件描述符准备就绪后立即停止阻塞,它不会立即 return 1 吗?如果没有 fds 准备好,它 returns 0,否则错误和 returns -1.
显然在实践中它 return 大于 1:重点是你应该能够 read/write 多个 fds,对吗?
由于现代抢占式多任务处理的工作方式,多个描述符可能会在您的进程被唤醒之前就绪,并且 select
调用会对描述符进行计数。
这绝对是我的一个愚蠢的误解,但是 select() 的手册页指出:
The timeout argument specifies the interval that select() should block waiting for a file descriptor to become ready. The call will block until either:
*a file descriptor becomes ready;
*the call is interrupted by a signal handler; or
*the timeout expires.
此外,
On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets (that is, the total number of bits that are set in readfds, writefds, exceptfds) which may be zero if the timeout expires before anything interesting happens. On error, -1 is returned, and errno is set to indicate the error; the file descriptor sets are unmodified, and timeout becomes undefined.
所以我的问题是——如果它在文件描述符准备就绪后立即停止阻塞,它不会立即 return 1 吗?如果没有 fds 准备好,它 returns 0,否则错误和 returns -1.
显然在实践中它 return 大于 1:重点是你应该能够 read/write 多个 fds,对吗?
由于现代抢占式多任务处理的工作方式,多个描述符可能会在您的进程被唤醒之前就绪,并且 select
调用会对描述符进行计数。