Re: Parent process unable to read messages from child process



Hi,
Thanks for the quick. I tried to implement the solution you suggested
exactly as you've posted here, However, the parent process populates
the @detail array with only one pipe output. i.e. the output of the
child which exited first. Perhaps there is some delay between the
time, Child process writes to the pipe and parent reads from the
pipe.
Could someone suggest something more?

-Gauri



If the amount of output of each child process is small, as your code
seems to indicate, a simple solution (without select) is possible.
It relies on the assumption that the kids never block on output and
*is* fragile. For a better solution, fix your original code.

use Vi::QuickFix;
use Data::Dumper;

my $numparts = 5;
my %pipes;
for (my $splitnum = 1; $splitnum <= $numparts; $splitnum++) {
pipe my ( $uncompread, $uncompwrite) or die "Cannot pipe: $!";
defined( my $pid = fork()) or die "fork: $!";
if ( $pid ) { # parent
close $uncompwrite;
$pipes{ $pid} = $uncompread;
} else { # child
close $uncompread;
sleep rand 5; # random execution time
print $uncompwrite "result of part $splitnum\n"; # some output
exit;
}
}

while ( ( my $pid = waitpid( -1, 0)) > 0 ) { # wait for next kid to be done
my $pipe = $pipes{ $pid}; # find its pipe
push @detail, scalar <$pipe>; # read output
}
chomp @detail; # make output more readable

print Dumper \ @detail;
__END__

Anno- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -


.



Relevant Pages