Re: strange multithreaded read() behavior

From: John Galt (johngalt___at_hotmail.com)
Date: 07/09/03


Date: 9 Jul 2003 13:58:29 -0700


> > int client_sockfds[10]; /* there are 10 clients */
> > FD_SET(listening_sockfd, &clients);
> > for(i=0; i < 10; i++)
> > FD_SET(client_sockfds[i], &clients);
> >
> > select(12, &clients, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
> >
> > for(i=0; i < 10; i++)
> > {
> > if (FD_ISSET(client_sockfds[i], &clients))
> > /* mq_send() a message to the threadpool with the socket
> > descriptor */
> > }
>
> Ouch. If it loops, it will send bazillions of messages because the
> socket is still ready for reading!

Yes! Man David, you're good... What's a good way around this? Can I
put a timeout in select()? (I am guessing not; does the timeout
indicate how long to wait _until_ something is ready?)

Also, will a client socket test true for reading all the time? Or will
it test true only if the client actually sent something. To get more
precise, suppose the client sends the string "hello\r\n", will the
socket test true for reading 7 times or just one time?

>
> > And worker threads basically do this:
> >
> > for(;;)
> > {
> > mq_receive(...); /* blocks until a message arrives from the main
> > thread
> > * this is synchronized; only one thread will
> > awake.
> > * the message is the socket descriptor of a
> > client
> > * that, according to select(), is ready for
> > reading
> > */
> > len = read(sockfd, buf, BUFSIZ);
> > write(sockfd, buf, len);
> > /* go back to waiting for a message */
> > }
>
> > I am seeing this behavior:
> >
> > - The main sends two messages, each with a different client socket
> > descriptor.
> > - Two different threads get one socket descriptor each
> > - The first thread to complete a read() basically seems to take over,
> > i.e.
> > the other thread's read()s are blocked until the first client
> > disconnects!
>
> Probably because *all* the threads are blocked on the other decriptor.

Not sure what you mean here. Only one thread will get the message and
come out of the mq_receive(). The other threads will remain blocked in
mq_receive(). (I don't think that more than one thread is blocked on
the same socket descriptor because the first client does get a full
response, inspite of the bazillions of messages.)

>
> > I've been told to put them into nonblocking mode, but am not sure
> > _why_ that will work. (BTW, these are plain vanilla SOCK_STREAM
> > sockets on a SPARC Blade 1000 running Solaris 8, using gcc, compiled
> > with -lpthread, -lsocket -lnsl).
>
> Because you don't want to block and you can't assure that you don't
> block without non-blocking sockets. However, you seem to want to do blocking
> writes, which is kind of odd.

Hmm. If a socket tests true for reading after a select(), then why
would a subsequent read() block? I do take your point on the write()
part. In fact, from the debugging messages in my code, I note that
there are a helluva lot of zero byte read()s.

John
>
> DS



Relevant Pages

  • Re: strange multithreaded read() behavior
    ... will a client socket test true for reading all the time? ...
    (comp.unix.programmer)
  • Re: Winsock Send hangs thread
    ... Then the client is not reading all of the sent data, ... socket's internal buffer to eventually fill up. ... Because the socket has no room available to send more data. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: UNIX domain sockets
    ... >> I have written a very simple client and server system to test this. ... > to this socket, and flushing an input buffer should rarely be done. ... > I recommend getting and reading a copy of "Unix Network Programming, ...
    (comp.lang.perl.misc)
  • Re: Server can receive but cant send even tho both sockets are open?
    ... the socket for notifications via WSAAsyncSelect after reading. ... data before the notification for it is sent. ... What i dont understand is it is reading fine then randomly it seems stops ... What makes it stranger is the socket is fine ont he client end and the ...
    (microsoft.public.win32.programmer.networks)
  • Re:Sockets in Cocoa
    ... For socket communications, you should ... I am writting a chat client for a not so known client ... Nothing really worked so now I'm reading through ... Should I just stick to socketused in Unix Network Programming? ...
    (comp.sys.mac.programmer.help)