An alternative (best is to use modules, it saves you from deadlock pitfalls) is using pipes:

perlipc#Safe-Pipe-Opens

The problem with pipes is that you can kill your child, or the child hangs or takes too long, without the parent knowing, as it is "reading". So you need an alarm ($SIG{'ALRM'}) in your parent. A way I do it (so the parent stays responsive)

#!/usr/bin/perl use strict; use warnings; my $parent_pid = $$; my $child_pid = 0; my @child_output; my $read_from_child = 0; my $max_timeout = 60; my $timeout; print "Parent is $parent_pid\n"; my $pid = open(KID_TO_READ, "-|"); defined($pid) || die "can't fork: $!"; #local $SIG{CHLD}; if ($pid) { # parent local $SIG{'CHLD'} = sub { print "dead child\n"; $timeout=0 }; local $SIG{'USR1'} = sub { ++$read_from_child }; print "reading kid...\n"; $timeout = $max_timeout; while($timeout >0){ print "parent $parent_pid timeout=$timeout read_from_child=$re +ad_from_child - child_pid=$child_pid\n"; if($read_from_child>0){ --$read_from_child; $_=<KID_TO_READ>; print "parent $$ just read: $_"; unless( $child_pid>0 ){ chomp($child_pid = $_); }else{ push @child_output, $_; } }else{ sleep 1; --$timeout; } } close(KID_TO_READ); } else { # child print $$ . "\n"; # send child pid kill('USR1', $parent_pid); # warn parent there is data print STDERR "child $$ is running\n"; # print to screen (DEBUG) my $program = "/bin/sleep"; my @args = qw(3); my $ret = system($program, @args); print "returncode is $ret\n"; # is $? print STDERR "returncode is $ret\n"; # print to screen (DEBUG) kill('USR1', $parent_pid); # warn parent there is data exit 0; } print "\nChild $child_pid is done, we now have:\n@child_output\n";

which prints something like:

Parent is 5323 reading kid... parent 5323 timeout=60 read_from_child=0 - child_pid=0 child 5324 is running parent 5323 timeout=59 read_from_child=1 - child_pid=0 parent 5323 just read: 5324 parent 5323 timeout=59 read_from_child=0 - child_pid=5324 parent 5323 timeout=58 read_from_child=0 - child_pid=5324 parent 5323 timeout=57 read_from_child=0 - child_pid=5324 parent 5323 timeout=56 read_from_child=0 - child_pid=5324 returncode is 0 parent 5323 timeout=55 read_from_child=1 - child_pid=5324 parent 5323 just read: returncode is 0 parent 5323 timeout=55 read_from_child=0 - child_pid=5324 dead child Child 5324 is done, we now have: returncode is 0

See also:

Re: How to make parent wait for all the child processes.


In reply to Re: get same return that system gives and get pid by FreeBeerReekingMonk
in thread get same return that system gives and get pid by Raiden690

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.