Reputation: 110
From the manpage for select():
those in writefds will be watched to see if a write will not block
For a file descriptor that is associated with a TCP/IP connection, how does the select() function determine when the connection can be written to without blocking? An alternative way of phrasing my question would also be, what are the conditions when select() will return indicating the file descriptor can be written to without blocking?
I'd assume that select() will not return the fd in the fd_set if the send buffer is full. If true, is this the only consideration? I can imagine many possible criteria for determining if a write should block or not, so I'm interesting in knowing specifically how this works on Linux.
Upvotes: 4
Views: 746
Reputation: 310840
It will indicate the FD is writable when there is space in the send buffer. There are no other considerations.
When connecting, this includes the case when the conenction is complete, as the send buffer is effectively allocated at that time.
Upvotes: 5
Reputation: 123260
If you write data they are not transmitted immediately to the peer, but they get first stored in the socket buffer. The kernel then takes the data out of the socket buffer and transfers them. Depending on the transfer protocol the data might be transmitted as fast as possible (UDP) or there is some flow control which causes the data to be kept on the senders side until the sender has acknowledged them (TCP). If there are too much unacknowledged data it will stop processing data inside the socket buffer, which will then start to fill up. Once there is no more space in the buffer writing will block. And once there is again enough space in the socket buffer writing will be possible again this will be signaled within select.
Upvotes: 1