unbuffered i/o to a pipe

Discussion in 'Perl Misc' started by Dave Saville, Mar 26, 2014.

  1. Dave Saville

    Dave Saville Guest

    I am playing with a raspberry pi with some generated stereo piped to
    pacat. This is for a larger project but for this I just have a sine
    wave that moves across the speakers for a few seconds. It stutters so
    I wondered if there is any buffering going on when you open like so:

    my $pacat = sprintf "| pacat --................
    open my $PACAT, $pacat or die "Can't open $pacat $!";

    If there is buffering how does one turn it off?

    my $old_fh = select $PACAT; $| = 1; select $old_fh;

    Does not make any difference. Of course it may be something completely
    different causing the stutter - but things like mplayer work fine with
    no stutter.

    TIA
    --
    Regards
    Dave Saville
    Dave Saville, Mar 26, 2014
    #1
    1. Advertising

  2. "Dave Saville" <> writes:
    > I am playing with a raspberry pi with some generated stereo piped to
    > pacat. This is for a larger project but for this I just have a sine
    > wave that moves across the speakers for a few seconds. It stutters so
    > I wondered if there is any buffering going on when you open like so:
    >
    > my $pacat = sprintf "| pacat --................
    > open my $PACAT, $pacat or die "Can't open $pacat $!";
    >
    > If there is buffering how does one turn it off?
    >
    > my $old_fh = select $PACAT; $| = 1; select $old_fh;
    >
    > Does not make any difference.


    It does, cf

    ----------
    use POSIX;

    my ($fh, $rc);
    my ($rd, $wr, $fh);

    pipe($rd, $wr);

    if (fork() == 0) {
    close($wr);
    while ($rc = <$rd>) {
    print STDERR ("pong\n");
    }

    _exit(0);
    }

    close($rd);

    $fh = select($wr);
    $| = 1;
    select($fh);

    for (0 .. 9) {
    print STDERR ("ping\n");
    print $wr ("ping\n");

    sleep(1);
    }
    ----------

    Depending on whether autoflush is set or not, this will either print
    ping - pong alternatingly or print 10 pings followed by 10 pongs. But
    you should really bypass the buffering altogether for 'realtime
    communication', aka 'use syswrite'.
    Rainer Weikusat, Mar 26, 2014
    #2
    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:
    689
    Jürgen Exner
    Sep 11, 2004
  2. lee, wonsun
    Replies:
    1
    Views:
    477
    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:
    178
    Iñaki Baz Castillo
    Sep 25, 2008
  4. Replies:
    1
    Views:
    213
    Ben Morrow
    Jun 2, 2004
  5. Oliver

    unbuffered pipe

    Oliver, Jun 20, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    189
Loading...

Share This Page