Managing read/write socket without polling

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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....
    2. 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. ...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Managing read/write socket without polling

    In article <becdka$seu$1@news.apple.com>,
    Peter Ammon <pa44@cornell.edu> wrote:
    >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.
    >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().

    --
    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

  4. #3

    Default Re: Managing read/write socket without polling


    "Peter Ammon" <pa44@cornell.edu> wrote in message
    news:becdka$seu$1@news.apple.com...
    > 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?
    > 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. 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?

    DS


    David Schwartz Guest

  5. #4

    Default Re: Managing read/write socket without polling

    On Mon, 07 Jul 2003 14:19:43 -0400, Peter Ammon wrote:
    > 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).
    > 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.
    > 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:

    [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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139