What is going when nfds=0 in the select() system call

Viewed 1202

I am debugging an application on Linux. And it has a couple of threads that are periodically calling the select system call:

strace shows:

select(0, NULL, NULL, NULL, {1, 342414})

So nfds=0. I thought that nfds is the highest file descriptor number occurring in any of the sets readfds, writefds and exceptfds incremented by one. It cannot be standard input (fd=0), because this would then have nfds=1.

So what is the meaning of nfds=0 in this case?

Thanks!

3 Answers

Usually select sleeps until either the timeout expires, or an event occurs on one of the file descriptors. If there are no file descriptors, the timeout is the only remaining behaviour.

My local manpage for select(2) even includes the text

Some code calls select() with all three sets empty, nfds zero, and a non-NULL timeout as a fairly portable way to sleep with subsecond precision.

Note that the descriptor sets are all null pointers. In that case the first argument isn't really used and select is simply used to put the thread to sleep. For 1 second and 342414 microseconds (unless there's a signal interrupting the sleep).

The nfds argument is not the largest descriptor, it is the largest + 1. So if you watched stdin with a file descriptor of 0, you would need to pass in 1.

Effectively select(0, NULL, NULL, NULL, {1, 342414}) says there are no file descripts, and the actual file descriptor sets are pass in as NULL so they are note even checked. So only the timeout will have any effect in this call.

Related