Re: strange multithreaded read() behavior
From: John Galt (johngalt___at_hotmail.com)
Date: 07/09/03
- Next message: Richard L. Hamilton: "Re: tty-dev-number <-> /dev/pts"
- Previous message: Akop Pogosian: "Re: Network Printing Under Solaris 9"
- In reply to: David Schwartz: "Re: strange multithreaded read() behavior"
- Next in thread: David Schwartz: "Re: strange multithreaded read() behavior"
- Reply: David Schwartz: "Re: strange multithreaded read() behavior"
- Reply: Casper H.S. Dik: "Re: strange multithreaded read() behavior"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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
- Next message: Richard L. Hamilton: "Re: tty-dev-number <-> /dev/pts"
- Previous message: Akop Pogosian: "Re: Network Printing Under Solaris 9"
- In reply to: David Schwartz: "Re: strange multithreaded read() behavior"
- Next in thread: David Schwartz: "Re: strange multithreaded read() behavior"
- Reply: David Schwartz: "Re: strange multithreaded read() behavior"
- Reply: Casper H.S. Dik: "Re: strange multithreaded read() behavior"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|