Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

unix

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?

Thanks.

like image 926
sandeep Avatar asked Dec 30 '10 14:12

sandeep


People also ask

What is fd_set used for?

The fd_set structure is used by various Windows Sockets functions and service providers, such as the select function, to place sockets into a "set" for various purposes, such as testing a given socket for readability using the readfds parameter of the select function.

How does select system call work?

The select() system call enables a system to keep track of several file descriptors. So, the select system call waits for one of the descriptors or a whole to turn out to be “ready” for a particular type of I/O activity (e.g., input possible).

What does fd_set return?

RETURN VALUE FD_ISSET() a non-zero value if the bit for the file descriptor fd is set in the file descriptor set pointed to by fdset, and 0 otherwise. On successful completion, select() returns the total number of bits set in the bit masks. Otherwise, -1 is returned, and errno is set to indicate the error.

What is select system call return?

RETURN VALUE 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.


1 Answers

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.

like image 145
Jonathan Leffler Avatar answered Oct 20 '22 16:10

Jonathan Leffler