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
     
    Dave Saville, Mar 26, 2014
    #1
    1. Advertisements

  2. 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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.