unbuffered pipe

Discussion in 'Perl Misc' started by Oliver, Jun 20, 2004.

  1. Oliver

    Oliver Guest

    I'm using a pipe to talk to a forked child process -

    I do not want any data to get lost if the child process exits
    unexpectedly -

    - so I was hoping to turn off buffering so that there was at most 1
    piece of data (i.e. one line) in the pipe.

    I thought if I did:

    select(PIPEHANDLE);
    $| = 1;

    then my pipe would not do buffering, and I was expecting that that
    would lead to blocking writes - i.e. if the data hadn't been read from
    the pipe, then a write to the pipe would block until it was.

    however - after doing the above - I find I can still write data to the
    pipe - regardless of how much is read out of the other end.

    could someone enlighten me as to what's going on here please?
    thanks muchly,
    Oliver.
     
    Oliver, Jun 20, 2004
    #1
    1. Advertising

  2. Oliver

    Ben Morrow Guest

    Quoth (Oliver):
    > I'm using a pipe to talk to a forked child process -
    >
    > I do not want any data to get lost if the child process exits
    > unexpectedly -
    >
    > - so I was hoping to turn off buffering so that there was at most 1
    > piece of data (i.e. one line) in the pipe.
    >
    > I thought if I did:
    >
    > select(PIPEHANDLE);
    > $| = 1;
    >
    > then my pipe would not do buffering, and I was expecting that that
    > would lead to blocking writes - i.e. if the data hadn't been read from
    > the pipe, then a write to the pipe would block until it was.
    >
    > however - after doing the above - I find I can still write data to the
    > pipe - regardless of how much is read out of the other end.
    >
    > could someone enlighten me as to what's going on here please?


    $| turns off perl's buffering of the data before it gets sent down the
    pipe. The data is still buffered inside the pipe: AFAIK, there is no way
    to prevent this.

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    The Levellers, 'Believers'
     
    Ben Morrow, Jun 20, 2004
    #2
    1. Advertising

  3. Oliver

    Ben Morrow Guest

    Quoth Ben Morrow <>:
    >
    > Quoth (Oliver):
    > > I'm using a pipe to talk to a forked child process -
    > >
    > > I do not want any data to get lost if the child process exits
    > > unexpectedly -
    > >
    > > - so I was hoping to turn off buffering so that there was at most 1
    > > piece of data (i.e. one line) in the pipe.

    <snop>
    > >
    > > however - after doing the above - I find I can still write data to the
    > > pipe - regardless of how much is read out of the other end.
    > >
    > > could someone enlighten me as to what's going on here please?

    >
    > $| turns off perl's buffering of the data before it gets sent down the
    > pipe. The data is still buffered inside the pipe: AFAIK, there is no way
    > to prevent this.


    ....however, having thought some more, you could use a socketpair instead
    and set the socketopts SO_SNDBUF and SO_RCVBUF to 1 byte.

    Ben

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
     
    Ben Morrow, Jun 20, 2004
    #3
  4. Oliver

    Guest

    (Oliver) wrote:
    > I'm using a pipe to talk to a forked child process -
    >
    > I do not want any data to get lost if the child process exits
    > unexpectedly -
    >
    > - so I was hoping to turn off buffering so that there was at most 1
    > piece of data (i.e. one line) in the pipe.
    >
    > I thought if I did:
    >
    > select(PIPEHANDLE);
    > $| = 1;
    >
    > then my pipe would not do buffering, and I was expecting that that
    > would lead to blocking writes - i.e. if the data hadn't been read from
    > the pipe, then a write to the pipe would block until it was.
    >
    > however - after doing the above - I find I can still write data to the
    > pipe - regardless of how much is read out of the other end.


    You have prevented Perl from buffing up it's output before handing it to
    the OS, but the OS still has it's own pipe buffer.

    If you want the two processes to proceed in lock-stop, you could require
    an acknowledgement from the child before the parent attempts the next
    print.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 20, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Starbase Commander

    Unbuffered keyboard input???

    Starbase Commander, Sep 10, 2004, in forum: Perl
    Replies:
    1
    Views:
    700
    Jürgen Exner
    Sep 11, 2004
  2. lee, wonsun
    Replies:
    1
    Views:
    490
    Jack Klein
    Nov 2, 2004
  3. Iñaki Baz Castillo

    How to read a PIPE in unbuffered way?

    Iñaki Baz Castillo, Sep 24, 2008, in forum: Ruby
    Replies:
    3
    Views:
    194
    Iñaki Baz Castillo
    Sep 25, 2008
  4. Replies:
    1
    Views:
    227
    Ben Morrow
    Jun 2, 2004
  5. Dave Saville

    unbuffered i/o to a pipe

    Dave Saville, Mar 26, 2014, in forum: Perl Misc
    Replies:
    1
    Views:
    71
    Rainer Weikusat
    Mar 26, 2014
Loading...

Share This Page