Skip to contentSkip to conversation

select() - Linux vs. BSD

DISCUSSION [email protected] 3 participants 4 messages Jun 1, 2001 – Jun 3, 2001

lore
Share & tools
lore
#0 lore raw

On Tue, 29 May 2001, John Chris Wren wrote:

> In BSD, select() states that when a time out occurs,
> the bits passed to select will not be altered.
> In Linux, which claims BSD compliancy for this
> in the man page (but does not state either way
> what will happen to the bits), zeros the users bit masks
> when a timeout occurs.

(i) "In BSD" - which one did you look at? 4.2? 4.3? 4.4? Details differ a bit.

(ii) The Linux man page only says

RETURN VALUE
On success, select and pselect return the number of descriptors contained in the descriptor sets, which may be zero if the timeout expires before anything interesting happens. On error, -1 is returned, and errno is set appropriately; the sets and timeout become undefined, so do not rely on their contents after an error.

That is, a wise programmer does not assume any particular value for the bits after a timeout.

Quoted 1 line

Should the man pages be changed to reflect reality,

They do reflect reality.

Quoted 1 line

or select() fixed to act like BSD?

No, select() is fine.

dean gaudet answered:

sounds like a man page bug.

Shame on him for noticing man page bugs without cc'ing the man page maintainer! Fortunately however, I do not think anything is wrong here. But just to be sure I added a sentence to select.2:

On BSD, when a timeout occurs, the file descriptor bits are not changed. Linux follows SUSv2 and sets the bit masks to zero upon a timeout.

Andries

#1 lore raw
Quoted 10 lines · On Fri, 1 Jun 2001 [email protected] wrote:

(ii) The Linux man page only says

RETURN VALUE
On success, select and pselect return the number of descriptors contained in the descriptor sets, which may be zero if the timeout expires before anything interesting happens. On error, -1 is returned, and errno is set appropriately; the sets and timeout become undefined, so do not rely on their contents after an error.

Quoted 2 lines

That is, a wise programmer does not assume any particular value for the bits after a timeout.

So how does this say the value of the fdsets are undefined after a timeout? It says specifically that upon success it returns the number of descriptors in the sets, zero if the timeout expires. That is a success condition upon which select() sets the fdsets to contain the descriptors upon which something interesting happened. In the case of a timeout with no descriptor having anything interesting, the sets would, logically, be cleared.

The man page does state that the value of "timeout" is effectively undefined upon return and that "timeout" and the fdsets are undefined after an error, however.

Of course, not looking at the sets upon a zero return is a fairly obvious optimization as there is little point in doing so.

William Astle
finger [email protected] for further information

Geek Code V3.12: GCS/M/S d- s+:+ !a C++ UL++++$ P++ L+++ !E W++ !N w--- !O !M PS PE V-- Y+ PGP t+@ 5++ X !R tv+@ b+++@ !DI D? G e++ h+ y?

#2 lore raw
In reply to [email protected]
Quoted 2 lines · [email protected] wrote:

Of course, not looking at the sets upon a zero return is a fairly obvious optimization as there is little point in doing so.

No; a fairly obvious optimisation is to avoid calling FD_ZERO if you can clear the bits individually when you test them.

When you examine the sets, you can clear each bit that you examine and then you know you have a zero set. Then you can set only the relevant bits for the next call to select().

If you can't rely on the sets being cleared on a timeout, then you will have to call FD_ZERO in that case, or you will have to go through the list of descriptors and clear them individually. (This can be avoided but it means keeping track of state between successive calls to select()). This is contrary to the non-timeout case, where you stop checking bits when you have counted N of them set.

So you see, there is a handy optimisation if you can assume the sets are zeroed on timeout.

-- Jamie

#3 lore raw
Quoted 6 lines · Jamie Lokier
Quoted 2 lines · [email protected] wrote:

Of course, not looking at the sets upon a zero return is a fairly obvious optimization as there is little point in doing so.

No; a fairly obvious optimisation is to avoid calling FD_ZERO if you can clear the bits individually when you test them.

That would make sense. HOWEVER, you took my comment out of context. I had pointed out that since the zero return from select() is not an error condition, you can rely on the sets being zeroed out. The zero simply indicates that no descriptors had anything interesting occur and if the sets are not zeroed, the implementation is broken. Upon error, the values are undefined and the value of timeout is undefined. (In this case, doing nothing upon a zero return from select() is perfectly reasonable.)

If I recall, the original posting was about whether the sets were zeroed upon an error condition which a timeout is not defined as in this case.

William Astle
finger [email protected] for further information

Geek Code V3.12: GCS/M/S d- s+:+ !a C++ UL++++$ P++ L+++ !E W++ !N w--- !O !M PS PE V-- Y+ PGP t+@ 5++ X !R tv+@ b+++@ !DI D? G e++ h+ y?