One way of doing so is using pipes. Before you fork() open a pipe. After the fork(), the child writes, the parent reads. Luckely perl will all wrap that for you if you use open with a -| argument.

The following example forks off 41 children using pipes. The children sleep for some time, then write data (this should be similar to your children). After having spawned off all the children, the parent is going to read all the pipes. It could very well be that the children are done in a different order than the parent is going to inspect them, but since you have to wait till the last one is finished, it doesn't matter (well, it would if there would be huge amounts of data involved). After reading all the data, we reap the children using wait, although that shouldn't be necessary since we didn't install a signal handler.

#!/usr/bin/perl use strict; use warnings 'all'; my @kids; foreach my $count (0 .. 40) { my $pid = open $kids [$count] => "-|"; die "Failed to fork: $!" unless defined $pid; unless ($pid) { # Child. sleep rand 60; # Sleep for some random time. print "$$: ", int rand 0x7FFFFFFF, "\n"; exit; } } foreach my $fh (@kids) { my @lines = <$fh>; print "Got: @lines"; } 1 while -1 != wait;

Abigail


In reply to Re: How do I get the child's data? by Abigail-II
in thread How do I get the child's data? by pepik_knize

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.