Socket blocking/non-blocking access puzzle.

Sergei Organov osv at
Fri Sep 27 08:13:02 UTC 2002


Suppose we have an open socket and dedicated thread for reading data from the
socket that performs blocking reads. Suppose also that another thread needs to
periodically write to the socket in non-blocking mode. Unfortunately,
O_NONBLOCK flag is shared between otherwise almost independent input and
output parts of the socket so can't be used directly[1].

Looking into documentation one can find that it's possible to setup separate
timeout values for read and write operations on socket (using SO_RCVTIMEO and
SO_SNDTIMEO with setsockopt()), but for whatever reason the required
functionality to achieve non-blocking behavior is missed [2].

To solve the problem I've modified TCP stack sources to provide required
non-blocking semantics if both fields of 'tv' time structure passed to the
setsockopt() are set to '(unsigned)(-1)' [3]. It seems that everything runs
like a breeze now, but I'd be thankful if someone who is familiar with the
stack takes a look at the changes I've made. Inclusion of the changes into the
main RTEMS source tree is also encouraged.

Unfortunately, attached patch is against rather old rtems-ss-20010424 snapshot
and location of libnetworking directory has been changed since then, so manual
intervention is required to apply it to the recent sources. Hopefully it's not
a big deal as only 3 files has been modified slightly.

[1] I said "directly" because apparently select() along with O_NONBLOCK flag
set on the socket could be used in reading thread to solve the puzzle, but I
don't like the idea to use separate select() for every socket I need to

[2] Setting fields of 'tv' structure to 0 (in attempt to apply select()
conventions here) gives us "block indefinitely" instead of "don't block".

[3] Another option would be to allow to pass NULL pointer to specify
non-blocking behavior, but NULL pointer is considered to be invalid value for
the last argument of the setsockopt(). It would also create another surprise
for those who are familiar with the meaning of timeout argument of select(),
where NULL means "block indefinitely".

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: socket_timeout.diff
URL: <>

More information about the users mailing list