Managing read/write socket without polling

Managing read/write socket without polling

Post by Peter Ammo » Wed, 09 Jul 2003 03:19:43



I need to manage multiple socket connections to a server.  Each one can
have data that needs to be read or written at any time.  I would like to
be able to read and write data as it becomes available without having to
poll for it.  I am targetting Mac OS X and Linux at a minimum.

The usual way to avoid polling on a read is to use select(), but what
happens if data becomes available for writing while the thread is
waiting on select() or blocking on a read?  Do I have to use a separate
thread for writing?  If so, are there synchronization issues I shold be
aware of for reading and writing to the same socket at the same time in
separate threads?

Would nonblocking I/O be simpler to implement instead?

Thanks for any advice,
-Peter

 
 
 

Managing read/write socket without polling

Post by David Schwart » Wed, 09 Jul 2003 06:59:06



Quote:> I need to manage multiple socket connections to a server.  Each one can
> have data that needs to be read or written at any time.  I would like to
> be able to read and write data as it becomes available without having to
> poll for it.  I am targetting Mac OS X and Linux at a minimum.

    Okay.

Quote:> The usual way to avoid polling on a read is to use select(), but what
> happens if data becomes available for writing while the thread is
> waiting on select() or blocking on a read?

    What do you mean by "data becomes available for writing"? Do you mean
the socket has room in its buffer for a 'write' or do you mean something
else?

Quote:> Do I have to use a separate
> thread for writing?

    That might be one way.

Quote:>  If so, are there synchronization issues I shold be
> aware of for reading and writing to the same socket at the same time in
> separate threads?

    No, this is perfectly safe so long as the 'read' and the 'write' are
independent. But if you're going non-blocking using 'select', you don't need
two threads.

Quote:> Would nonblocking I/O be simpler to implement instead?

    I thought we were talking about nonblocking I/O. How do you use 'select'
with blocking I/O?

    DS

 
 
 

Managing read/write socket without polling

Post by Peter Ammo » Wed, 09 Jul 2003 08:27:51





>>I need to manage multiple socket connections to a server.  Each one can
>>have data that needs to be read or written at any time.  I would like to
>>be able to read and write data as it becomes available without having to
>>poll for it.  I am targetting Mac OS X and Linux at a minimum.

>     Okay.

>>The usual way to avoid polling on a read is to use select(), but what
>>happens if data becomes available for writing while the thread is
>>waiting on select() or blocking on a read?

>     What do you mean by "data becomes available for writing"? Do you mean
> the socket has room in its buffer for a 'write' or do you mean something
> else?

Sorry this wasn't clear.  By "data becomes available for writing," I
mean that the user typed something else that needs to be sent to the
server.  The data is generated by user interaction.

- Show quoted text -

Quote:

>>Do I have to use a separate
>>thread for writing?

>     That might be one way.

>> If so, are there synchronization issues I shold be
>>aware of for reading and writing to the same socket at the same time in
>>separate threads?

>     No, this is perfectly safe so long as the 'read' and the 'write' are
> independent.

Thanks, that's good to know.  I suppose this is the way to do it.

Quote:> But if you're going non-blocking using 'select', you don't need
> two threads.

>>Would nonblocking I/O be simpler to implement instead?

>     I thought we were talking about nonblocking I/O. How do you use 'select'
> with blocking I/O?

I would use select() to determine if the socket has data available for
read()ing.  If it does, then a call to read() will not block.  By
non-blocking I/O, I meant using a function like fcntl to make the socket
nonblocking, so that calls to it never block but aren't guaranteed to do
much before they return.

-Peter

 
 
 

Managing read/write socket without polling

Post by Michael B Alle » Wed, 09 Jul 2003 17:58:25



> I need to manage multiple socket connections to a server.  Each one can
> have data that needs to be read or written at any time.  I would like to
> be able to read and write data as it becomes available without having to
> poll for it.  I am targetting Mac OS X and Linux at a minimum.

> The usual way to avoid polling on a read is to use select(), but what
> happens if data becomes available for writing while the thread is
> waiting on select() or blocking on a read?

But if you use select() you should not be "blocking on a read". If you
mean reading from the keyboard then you should add STDIN_FILENO to your
descriptor set and use select() to handle readyness on that descriptor
as well (although specially).

Quote:> Do I have to use a separate
> thread for writing?  If so, are there synchronization issues I shold be
> aware of for reading and writing to the same socket at the same time in
> separate threads?

If you are using mutiple threads you would need to syncronize their
actions with mutexes, condition variables, and semaphores yes.

Quote:> Would nonblocking I/O be simpler to implement instead?

You might do that but it's not necessarily simplier. You could use
something like libevent. Read this:

  http://www.kegel.com/c10k.html

[Note: I am not necessarily advocating the techniques described on this
webpage. I only refer you to it because it has good information regarding
non-blocking I/O strategies and libevent]

Mike

 
 
 

Managing read/write socket without polling

Post by Barry Margoli » Wed, 09 Jul 2003 03:57:26




Quote:>I need to manage multiple socket connections to a server.  Each one can
>have data that needs to be read or written at any time.  I would like to
>be able to read and write data as it becomes available without having to
>poll for it.  I am targetting Mac OS X and Linux at a minimum.

>The usual way to avoid polling on a read is to use select(), but what
>happens if data becomes available for writing while the thread is
>waiting on select() or blocking on a read?  Do I have to use a separate
>thread for writing?  If so, are there synchronization issues I shold be
>aware of for reading and writing to the same socket at the same time in
>separate threads?

Using separate threads may be the simplest solution.  There shouldn't be
any synchronization issues with this -- reading and writing a socket are
independent, unrelated operations, and the kernel should sort it out
automatically.

Another solution is to use something you can select() on, e.g. a pipe or
Unix-domain socket, as the mechanism for making data available for writing.

Quote:>Would nonblocking I/O be simpler to implement instead?

I don't think it makes much difference, since you also have the problem
when you're blocked in select().

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

1. Sockets: write() writes OK, yet read() reads garbage...?

Strange thing happens. There is a TCP socket established between A and B. Some
information flows OK between the two. However, one particular sequence of
communication does not. A writes a command to B, and immediately goes into
read() for a responce. B does something with the message and writes back to A a
struct, with some fields set to important values. When A reads it (as the same
struct - both use the same header file), however, it contains garbage. All the
fiels are set to 0 (zero). And it reads the correct size of the struct:

  n = read( sock, &preamble, sizeof(preamble) );
  if( n < 0 || n > sizeof(preamble) )
   error...

What/where can the problem be? Does A (or maybe B) have to flush the socket
after it wrote a command, to make sure it does not read its own packet back?

Thanks.
--
Simon   B-)>

2. 2.5.10(bk r1.558): oops on mount cdrom (scsi emulation)

3. SO_KEEPALIVE - How to detect a broken socket connection without read/write?

4. loopback broke?

5. write (socket, buf, 0) and read (socket, buf, 0)

6. How do I generate my own certificates?

7. polling a port addres for a clock pulse and reading and writing data to RS422 port

8. kernel: ICMP redirect from

9. How to find a dead socket without write?

10. How to read and write on an ethernet card without TCP

11. reading each line of input without writing to a file

12. read&write in popen() without using expect package ?

13. : How to determine if a (TCP) socket has closed without reading