Search code examples
cunixposix-select

Is it necessary to reset the fd_set between select system call?


I am facing a problem using the select function in Unix.

I have a server that waits for for a connection. First I add the listening socket file descriptor listener to the fd_set readfds using FD_SET(listener, readfds) and then I use that in select().

When I get a connection, I call accept() and set the readfds in select with the accepted file descriptor and start receiving the data from connection. However, when I check the code in strace, The select doesn't show the listener in the readfds while select() is executing a second time.

Do I need to set the listener file descriptor again using FD_SET(listener, readfds) before calling select() again?


Solution

  • Yes (it is necessary to reset the fd_set between select() system calls).

    It is a nuisance, but they act as input/output parameters; they are read by and modified by the system call. When select() returns, the values have all been modified to reflect the set of file descriptors ready. So, every time before you call select(), you have to (re)initialize the fd_set values.