Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use the "fd_set *writefds" parameter in the select() statement for

Tags:

c

This is the prototype of the select statement (acc to man pages):

    int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
           struct timeval *timeout);

I know what to use the readfds parameter for: with this one you can see if data was written to one of your sockets. On the other hand, the writefds page that I found, states that this is to see "if any of the sockets is ready to send() data to". But what does this mean? In Windows Sockets Network Programming by Quin and Shute it says that this detects either the connected or the writable state. What is the point of this? Is it simply to check whether a socket still has a connection to a connected client and test whether is has any use writing something to that socket?

So: what does one normally use writefds for?

like image 251
Django Avatar asked Sep 14 '25 00:09

Django


1 Answers

If you keep writing to a TCP socket and the other side doesn't receive as fast as you send, there comes a time when write blocks. You want to avoid that so you need to test that "you can write without blocking". Because this normally doesn't happen in test programs, it could come as a shock, but write(2) and send(2) can block.

So if select(2) says a fd is set in writefds then it means any write or send on it will actually write at least one byte without blocking.

EDIT

From the standard:

The pselect() function shall examine the file descriptor sets whose addresses are passed in the readfds, writefds, and errorfds parameters to see whether some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively.

like image 151
cnicutar Avatar answered Sep 16 '25 17:09

cnicutar