Reputation: 1530
I have read some materials about socket programming online. By default, write()
is blocking. In some materials, write()
only blocks when socket buffer is full. Some other materials say write()
is blocked until all the data in the user buffer has been moved to the system buffer, which means write()
will also block if there is not enough space for the data to place. I am wondering which statement is correct if write()
is set to blocking.
Upvotes: 0
Views: 2050
Reputation: 20671
In some materials, write() only blocks when socket buffer is full. Some other materials say write() is blocked until all the data in the user buffer has been moved to the system buffer, which means write() will also block if there is not enough space for the data to place.
It is not clear to me that these are actually saying anything different.
First, what one refers to as the "system buffer" is what the other refers to as the "socket buffer", but they are the same thing.
Secondly, when the first statement says "write() only blocks when socket buffer is full" this should be interpreted as "write() only blocks when the write operation would overflow the (system) buffer". This is the same as "write() is blocked until all the data in the user buffer has been moved to the system buffer"; it is just that one says that the condition will cause write() to block and the other says that the block will cease once the condition becomes false. This is not contradictory.
Upvotes: 2
Reputation: 176
The write function behaves like the send() function when the descriptor is a socket. The write() fucntion will block if the internal buffers are full and until all the data can be sent. This assumes that the socket is blocking. Keep in mind that the write function can be interrupted by a signal; errno will be set to EINTR (the return value will be the number of bytes written when the signal was caught) and you can call write() again until all your data is sent.
See
http://pubs.opengroup.org/onlinepubs/009695399/functions/write.html http://pubs.opengroup.org/onlinepubs/009695399/functions/send.html
Upvotes: 0