Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Interprocess using Safe Pipes Opens

by Anonymous Monk
on Aug 02, 2007 at 16:38 UTC ( #630326=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks !

I am trying to write some code that will handle several child process at once.
I am working in Linux/Solaris environment, using Perl 5.6.1.
After reading all of the IPC documentation I could find, I came to the decision that using Safe Pipes Opens would be the right answer for me, since the parent process suppose to update the childs with information during the execution.

I created a skeleton to try and see how it works, but for some reason I have a problem - only the last child behaves properly and get the input sent by the parent.

Help would be appreciated,

Below is the code itself (I guess a little ugly...).
TIA,
Shushu
use strict; use warnings; use Carp; use Data::Dumper; use English '-no_match_vars'; use FileHandle; $| = 1; my $no_of_childs = 3; my @fh; my $index = 0; while ($index < $no_of_childs) { print "PID $PID\n"; my $pid; $fh[$index]{'fh'} = new FileHandle; $fh[$index]{'fh'}->autoflush(1); $pid = $fh[$index]{'fh'}->open("|-"); if ($pid) { # parent $fh[$index]{'pid'} = $pid; print "parent pid $PID creating child " . $index+ ++ . "\n"; } else { # child print "child pid $PID creating child " . $index++ . "\n +"; while (1) { # Here we will do the child work, and read inpu +t from STDIN print "$PID checking for input\n"; my $line = <STDIN>; if ($line) { print "$PID got something from father ! +!! $line\n"; } sleep 1; } exit; } } print Dumper \@fh; my $fh; sleep 5; $fh = $fh[0]{'fh'}; print "send hi to child 1\n"; print $fh "shalom yeled 1"; sleep 2; $fh = $fh[1]{'fh'}; print "send hi to child 2\n"; print $fh "shalom yeled 2"; sleep 2; $fh = $fh[2]{'fh'}; print "send hi to child 3\n"; print $fh "shalom yeled 3";

Comment on Interprocess using Safe Pipes Opens
Download Code
Re: Interprocess using Safe Pipes Opens
by liverpole (Monsignor) on Aug 02, 2007 at 17:10 UTC
    Without digging deeply into the code, I think what's happening is that the data is never getting flushed.

    It's probably just a matter of needing to terminate your strings with newlines:

    sleep 5; $fh = $fh[0]{'fh'}; print "send hi to child 1\n"; print $fh "shalom yeled 1\n"; # <= Note the newline to flush output sleep 2; $fh = $fh[1]{'fh'}; print "send hi to child 2\n"; print $fh "shalom yeled 2\n"; # <= Note the newline to flush output sleep 2; $fh = $fh[2]{'fh'}; print "send hi to child 3\n"; print $fh "shalom yeled 3\n"; # <= Note the newline to flush output

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Interprocess using Safe Pipes Opens
by krishnoid (Novice) on Aug 02, 2007 at 22:56 UTC
    I struggled with this for quite a while until I decided to check the FAQ again after many years. This entry:

    http://faq.perl.org/perlfaq8.html#How_do_I_start_a_pro

    pointed me to IPC::Run, and other entries pointed me to IPC::Open2 and IPC::Open3. IPC::Run seems to allow all kinds of child creation and I/O redirection without having to start a shell or do your own forking. The examples at the beginning of the page are very helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://630326]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-10-21 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (100 votes), past polls