nikolay has asked for the wisdom of the Perl Monks concerning the following question:

Hi, the great!

I got a problem w/ the pipes that is concluded in necessity to repeat data, sent into pipe in order to achieve the desired action.

In my script i have two parts -- a parent and a child. A parent sends signals to pipe, that are recieved by a process, run by the child -- i.e. pipe is used not between the parent and child, but between parent and another process, that the child creates (the process is the mplayer2).

So, when the parent sends signals (the key pressed by a user) to pipe (to the mplayer2), the mplayer2 not always receives it -- so that the users has to repeat h(is/er) pressings. -- And this is the problem that i want to solve -- to remove the repeatings.

Here is the code (relevant pieces only).

use POSIX qw( mkfifo ); mkfifo( $svitok_truby, 0700 ) || die $svitok_truby.' : '.$!; if( $nomer_vosproizvodyshchego_protsessu=fork ){ # forces a flush right away and after every write or print on the +currently selected output channel. $|=1; system( '/bin/echo \'seek -10\' >'.$svitok_truby ); }else{ system( '/usr/bin/mplayer -slave -input file='.$svitok_truby.' /tm +p/1.flac' ); }

Thank you for help!

Replies are listed 'Best First'.
Re: To organize pipe right way.
by kennethk (Abbot) on May 04, 2016 at 13:48 UTC
    Is there a particular reason you've used system instead of an exec? How do you expect data to get into your child if you never read or print? Do you expect the child to do anything after its child returns? I probably would have written this as (untested for your target):
    use POSIX qw( mkfifo ); my $svitok_truby; mkfifo( $svitok_truby, 0700 ) || die "Ошиб +ка создани&#110 +3; трубы $svitok_truby : $!"; my @args = ('/usr/bin/mplayer', '-slave', "-input file=$svitok_tru +by", '/tmp/1.flac'); local $SIG{CHLD} = "IGNORE"; my $pid = fork(); die "Fork failed\n" unless defined $pid; if ($pid == 0) { $| = 1; exec @args; } open (my $FIFO, '>', $svitok_truby) || die "can't open $svitok_tr +uby: $!"; while(read STDIN, my($char), 1) { print $FIFO $char; } close($FIFO);

    See also Named Pipes and note loop terminates on ^D.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I did understand your code not fully. -- It would be far better for me (to understand your answer), had you applied only necessary changes to my code structure -- not changing everything by the way. I would not ask you to explain many things because, it could be hard, so, i did apply what is seems me relevant, and can say, that it is works, but worse -- insted of sometimes, i had to press a key twice-thrice, now the number went up to seven! -- If i but write to pipe the PERLs' way instead of Shell's. Here is the code.
      my $FIFO=undef; mkfifo( $svitok_truby, 0700 ) || die '&#1054;&#1096;&#1080;&#1073;&#10 +82;&#1072; &#1089;&#1086;&#1079;&#1076;&#1072;&#1085;&#1080;&#1103; & +#1090;&#1088;&#1091;&#1073;&#1099; '.$svitok_truby.' : '.$!; local $SIG{CHLD} = "IGNORE"; if( $nomer_vosproizvodyshchego_protsessu=fork ){ $|=1; open( $FIFO, '>', $svitok_truby ) || die '&#1053;&#1077; &#1086;&#10 +90;&#1082;&#1088;&#1099;&#1090;&#1100; &#1090;&#1088;&#1091;&#1073;&# +1091; '.$svitok_truby.' &#1085;&#1072; &#1079;&#1072;&#1087;&#1080;&# +1089;&#1100; : '.$!; <sniped> print $FIFO 'pause'; <sniped> }else{ exec '/usr/bin/mplayer -slave -input file='.$svitok_truby.' /tmp/1.f +lac'; } close( $FIFO ); unlink $svitok_truby;

      Just one question, is there a flush command -- to execute after each writing to pipe?

      I did test more and have noticed weird connection between input in parent and pausing of mplayer, though to pipe was sent nothing at all -- parent just does its own work, w/o sending a thing to pipe, for example:

      mkfifo( $svitok_truby, 0700 ) || die '&#1054;&#1096;&#1080;&#1073;&#10 +82;&#1072; &#1089;&#1086;&#1079;&#1076;&#1072;&#1085;&#1080;&#1103; & +#1090;&#1088;&#1091;&#1073;&#1099; '.$svitok_truby.' : '.$!; # &#1059;&#1087;&#1088;&#1072;&#1074;&#1083;&#1103;&#1102;&#1097;&#107 +2;&#1103; &#1095;&#1072;&#1089;&#1090;&#1100; ("&#1088;&#1086;&#1076; +&#1080;&#1090;&#1077;&#1083;&#1100;"). if( $nomer_vosproizvodyshchego_protsessu=fork ){ $|=1; use Term::ReadKey; ReadMode 'cbreak'; # Parent while( 1 ){ $knopka=&ReadKey( 0 ); if( $knopka eq '/' ){ system( '/bin/date' ); }elsif( $knopka eq $knopka_vyhodu ){ system( '/bin/echo quit >'.$svitok_truby ); last; } } }else{ ## Child for( $i=0; $i<=$#svitok_na_vosproizvedenie; $i++ ){ system( '/usr/bin/mplayer -slave -input file='.$svitok_truby.' '.$ +svitok_na_vosproizvedenie_kom[$i] ); } } ReadMode 'normal'; unlink $svitok_truby;

      So, when i press key / -- parent just outputs date. But many times pressing, once, «mplayer2» will pause playing -- though nothing was sent to pipe! -- And that input-pipe relation i can not understand. Module «Term» does not have influence -- i had the same w/ the «read» function.

      Oh! The site terribly corrupts my input!

      Thanks for your answers.

        Sorry, I neglected to set flush on the pipe, which is why you saw the issue. That can be accomplished by explicitly importing IO::Handle and calling autoflush. If I write a complete, self-contained script wrapping date, it might look like:
        #!/usr/bin/perl use strict; use warnings; use POSIX qw( mkfifo ); use IO::Handle; my $svitok_truby = 'tmp.pp'; mkfifo( $svitok_truby, 0700 ) || die "Pipe fail; $svitok_truby : $!" +; local $SIG{CHLD} = "IGNORE"; my @args = ('perl','-MIO::Handle','-E',<<'EOC',$svitok_truby); $| = 1; open my $fh, "<", $ARGV[0]; while(read $fh, my($char), 1) { print $char; } EOC my $pid = fork(); die "Fork failed\n" unless defined $pid; if ($pid == 0) { exec @args; } open (my $FIFO, '>', $svitok_truby) || die "can't open $svitok_truby: + $!"; $FIFO->autoflush(1); while(read STDIN, my($char), 1) { print $FIFO $char; } close($FIFO); END { unlink $svitok_truby; }
        To modify this from piping into the toy echo program, you would swap @args (including the here-doc, which runs until EOC) to whatever is appropriate for your program, probably (untested):
        my @args = ('/usr/bin/mplayer', '-slave', '-input', "file=$svitok_trub +y", $svitok_na_vosproizvedenie_kom[$i]);
        It's possible you will still see buffering issues depending on your terminal, because many terminal programs buffer STDIN by default. To test if that is the problem, you can pipe in commands. So, for my toy above,
        perl -E'$|=1;print,sleep 1 for 0..9' | perl script.pl
        will slowly print 0 - 9 on the screen, one character a second. For your case, you might want to try pausing and unpausing music once per second.

        TLDR: use IO::Handle; and $FIFO->autoflush(1); will set the pipe to hot.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: To organize pipe right way.
by Anonymous Monk on May 04, 2016 at 13:27 UTC
    Nothing in your code interacts with any user, and why are you using system to write to the FIFO?
      Interaction w/ a user is out of the problem scope. I used system because: tried and it worked, whereas the PERL's documentation is purely written/understood to/by me.