Re: Threading and Timeouts



Marcin 'Qrczak' Kowalczyk wrote:

> Giancarlo Niccolai <searchforme@xxxxxxxxxxxx> writes:
>
>> Actually, I found that blocking i/o with select or poll works fine.
>> Once select returns signaling that a socket can be read/written,
>> a single read/write operation is GUARANTEED not to block;
>
> Except when another processes reads it in the meantime,

Of course. Select is meant for single process operation. However, it
guarantees that the NEXT operation on a file descriptor (regardless the
underlying physical media) won't block, and ... if the NEXT operation is
performed in another thread or process... well, that's YOUR problem ;-)

[And IMHO a BIG problem of yours, as sharing a socket/fd among two processes
or threads without coordination is, to say the least, awful design, and
usually, totally meaningless]


> and except
> that Linux has a bug where certain socket activities trigger select
> to return but there is nothing to read.

May be, but read() won't block and must return immediately 0, or Linux is
not posix compliant. Which may be, but I doubt a so relevant and important
non-compliance can survive a single sub release.

>
> And of course with blocking sockets you have to select before each
> read, and with non-blocking ones you only select when read fails with
> EAGAIN.

This is a way, but it is -at least- inefficient, as select() will perform,
in much more efficient and CPU effective way, the same thing for you. No
need to decide which sockets must be waited on select by checking their
EAGAIN status; let select do that for you.

>
> select is primarily designed for non-blocking I/O.

Where did you get this information?
As you can see from this (as well as many other authoritative sources):

http://seth.positivism.org/man.cgi/2/select_tut

(Linux programmer manual)

Select is applied to (blocking) pipes as i.e. stdin, without the need to
change it to nonblocking.

I can't see anything in the standards nor in relevant documentation
suggesting that selected sockets/file descriptors should be non-blocking;
OTOH, the purpose of select is to BLOCK your process until there's data to
be processed, so...

Bests
Giancarlo Niccolai.

.



Relevant Pages

  • Re: reading from a socket
    ... completed sending and is itself currently blocking on a read from the ... is "ok, set socket to non-blocking mode, and just use non-blocking IO instead". ... say, if code is given out with a no warranty clause, and someone else uses it in a place where it risks compromising peoples' health or safety, it is the fault of the person who used the code in such a context, and not the responsibility of the original developer. ...
    (comp.lang.c)
  • Re: Sockets causing deadlock
    ... My mistake if I said non-blocking. ... They are actually blocking. ... Allocate a new socket ... Status = connecting ...
    (microsoft.public.win32.programmer.networks)
  • Re: Socket overlapped I/O and FIONBIO
    ... Non blocking mode demand overlapped mode too, so the socket will be ... even if socket itself is non-blocking ...
    (microsoft.public.win32.programmer.networks)
  • Re: Socket overlapped I/O and FIONBIO
    ... is for the case you haven't set the socket in non-blocking mode. ... BSD socket approach. ... WSA_FLAG_OVERLAPPED that forces blocking mode. ... kernel completes if that will occur in fixed time. ...
    (microsoft.public.win32.programmer.networks)
  • Re: recv blocks although socket is ready
    ... of the members of the interface in my network layer have blocking ... Only one has non-blocking semantics. ... call any socket operation and rely on the socket to block. ... Now I have to implement blocking myself in all other ...
    (microsoft.public.win32.programmer.networks)