the function select() for multi client socket programming

206 views Asked by At

I have simple question about select() of I/O multiplexing function for socket programming.

When select function executes, it`s said that it modifies its examining fd set, so we need to reset it again every time. (fd_set read_fds for example..)

But why is that?

Why does the select function clears the uninteresting file descriptors on its fd sets?

What changes select function give to (or modify) original fd set?

Thanks.

all I found from book or somewhere else on web says 'We need to' reset for every loop routine but it doesn`t say how it is.

1

There are 1 answers

0
Jeremy Friesner On

Why does the select function clears the uninteresting file descriptors on its fd sets?

Because after select() returns, you're (presumably) going to want to query (via FD_ISSET()) which socket(s) are now ready-for-read (or ready-for-write).

So when select() returns, it modifies the fd_set objects so that the only bits still set inside them are the bits representing sockets that are now ready. If it did not do that, FD_ISSET() would have no way to know which sockets are ready for use and which are not.