Ask a Question related to UNIX Programming, Design and Development.
-
Peter Ammon #1
Managing read/write socket without polling
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
Peter Ammon Guest
-
File system get auto change from read-write to read-oly
I have a very strange file system with OS Redhat 7.2 The file system is read-write, but some how it randomly changes to read-only at any time.... -
Help with IO::socket simultaneous read and write
I've created an application that opens sockets (using IO::Socket) and then creates two ithreads, one that reads the socket and one that writes it. ... -
write hex to socket, weird
Hi, I just want to write raw data to a socket (by using IO::Socket) All I want to do is the following: print $my_socket "\x0f\xa1\x02.....and... -
Read & Read/Write Groups
I am trying to achieve a solution to a (hopefully) simple scenario. I have a Solaris 9 server that is going to be used for sharing files. I... -
Read/Write IO on socket file descriptor
Hi all, I'm trying to perform read and write I/O on a socket file descriptor received for another process via a Unix Domain Socket. In trying to... -
Barry Margolin #2
Re: Managing read/write socket without polling
In article <becdka$seu$1@news.apple.com>,
Peter Ammon <pa44@cornell.edu> wrote:Using separate threads may be the simplest solution. There shouldn't be>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?
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.
I don't think it makes much difference, since you also have the problem>Would nonblocking I/O be simpler to implement instead?
when you're blocked in select().
--
Barry Margolin, [email]barry.margolin@level3.com[/email]
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.
Barry Margolin Guest
-
David Schwartz #3
Re: Managing read/write socket without polling
"Peter Ammon" <pa44@cornell.edu> wrote in message
news:becdka$seu$1@news.apple.com...
Okay.> 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.
What do you mean by "data becomes available for writing"? Do you mean> 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?
the socket has room in its buffer for a 'write' or do you mean something
else?
That might be one way.> Do I have to use a separate
> thread for writing?
No, this is perfectly safe so long as the 'read' and the 'write' are> 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?
independent. But if you're going non-blocking using 'select', you don't need
two threads.
I thought we were talking about nonblocking I/O. How do you use 'select'> Would nonblocking I/O be simpler to implement instead?
with blocking I/O?
DS
David Schwartz Guest
-
Michael B Allen #4
Re: Managing read/write socket without polling
On Mon, 07 Jul 2003 14:19:43 -0400, Peter Ammon wrote:
But if you use select() you should not be "blocking on a read". If you> 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?
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).
If you are using mutiple threads you would need to syncronize their> 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?
actions with mutexes, condition variables, and semaphores yes.
You might do that but it's not necessarily simplier. You could use> Would nonblocking I/O be simpler to implement instead?
something like libevent. Read this:
[url]http://www.kegel.com/c10k.html[/url]
[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
Michael B Allen Guest



Reply With Quote

