unbuffered i/o to a pipe

D

Dave Saville

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
 
R

Rainer Weikusat

Dave Saville said:
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'.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top