Re: blocking read/write in socket
- From: "Alex Fraser" <me@xxxxxxxxxxx>
- Date: Sat, 13 May 2006 12:23:48 +0100
"Robert Harris" <robert.f.harris@xxxxxxxxxxxxxxxx> wrote in message
news:Vx79g.133048$tc.14612@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Alex Fraser wrote:
"Robert Harris" <robert.f.harris@xxxxxxxxxxxxxxxx> wrote in message
news:ml_8g.136610$xt.64419@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[snip]
1. Don't call read() or write() from your server until you know, from
calling select(), that your read or write will not block.
If you have a blocking socket, and select() (or poll()) says it is
writeable, write() (or send()) may still block. I think this is obviously
true if you attempt to write more than one byte.
No (at least certainly not for TCP/IP). Your write() (or send()) may
write less than you requested and it will return the number of bytes
actually written.
In the absence of errors and signals, a blocking write() will write the
number of bytes requested, blocking the thread if necessary.
In fact, it is possible that write() will block even if you attempt to
write
just one byte (after select() indicates the socket is writeable).
Similarly,
it is possible that read() (or recv()) will block if called after
select()
indicates the socket is readable. The reason is perhaps not obvious but
quite simple: in general terms, the situation may change between the time
select() determines a descriptor is readable/writeable and a subsequent
read()/write() on that descriptor. The solution, of course, is to set
O_NONBLOCK.
If you do that, then a read or write that would otherwise block will
return with an error condition. If your process has nothing to do until
it can actually read or write (a common condition), then with O_NONBLOCK
set it will just waste CPU time looping whereas using select() or poll()
with blocking reads and writes will allow the process to wait until it
has something to do.
I did not mean to suggest setting O_NONBLOCK and looping instead of using
select() or poll(). My point was that using select() or poll() does not, and
cannot, guarantee that you will not block in a subsequent read() or write().
When you use select() or poll(), it is normally potentially disastrous (or
at least undesirable) to block in read() or write(). For the reasons given
above, you can only ensure that will not happen by setting O_NONBLOCK on the
descriptor(s).
In other words, "using select() or poll() with blocking reads and writes" is
generally a bug waiting to happen.
Alex
.
- References:
- blocking read/write in socket
- From: dis_is_eagle
- Re: blocking read/write in socket
- From: Robert Harris
- Re: blocking read/write in socket
- From: Alex Fraser
- Re: blocking read/write in socket
- From: Robert Harris
- blocking read/write in socket
- Prev by Date: Re: getifaddr
- Next by Date: What GUI APIs are available in recent AIX releases?
- Previous by thread: Re: blocking read/write in socket
- Next by thread: Need Suggestion on Software structure
- Index(es):
Relevant Pages
|