I don't have Proc::Fork installed on my box, but using just ordinary fork (and swapping to Indirect Filehandles to avoid possible global collision issues) the following code generates 4 files that contain "Output of step 1" and 16 files that contain "Output of step 2". Assuming this is the intended result, this should hopefully be a helpful guide toward your real use case.
use strict; use warnings; my @array1 = (1..4); my @array2 = (1..4); for my $val1 (@array1) { my $outer_pid = fork(); die "fork failed ($val1)" unless defined $outer_pid; if ($outer_pid == 0) { open my $fh, ">", "$val1.txt" or die "Open fail: $!"; print $fh "Output of step 1\n"; exit 0; } else { waitpid $outer_pid, 0; for my $val2 (@array2) { my $inner_pid = fork(); die "fork failed ($val1,$val2)" unless defined $inner_pid; if ($inner_pid == 0) { open my $fh1, '<', "$val1.txt" or die "Open fail: $!" +; open my $fh2, ">", "$val2$val1.txt" or die "Open fail +: $!"; while (my $line = <$fh1>) { $line =~ s/1/2/; print $fh2 $line . "\n"; } exit 0; } else { waitpid $inner_pid, 0; } } } }

Update: It occurs to me that you likely don't want a blocking wait on your children, since this is specifically not generating simultaneous workers. You have a potential race condition in that scenario, since generating the first set of files may take more time than is required to get to generating the second set. You can resolve this in classic style using flock. You can also just ignore the problem of reaping kids using local $SIG{CHLD} = 'IGNORE';. The following code includes a 1 second sleep in the first loop to demonstrate blocking, and generates 21 simultaneous processes at max:

use strict; use warnings; use Fcntl ":flock"; my @array1 = (1..4); my @array2 = (1..4); local $SIG{CHLD} = 'IGNORE'; for my $val1 (@array1) { my $pid = fork(); die "fork failed ($val1)" unless defined $pid; if ($pid == 0) { open my $fh, ">", "$val1.txt" or die "Open fail: $!"; flock($fh, LOCK_EX) or die "Cannot lock $val1.txt - $!\n"; print $fh "Output of step 1\n"; sleep 1; flock($fh, LOCK_UN) or die "Cannot unlock $val1.txt - $!\n"; exit 0; } } for my $val1 (@array1) { for my $val2 (@array2) { my $pid = fork(); die "fork failed ($val1, $val2)" unless defined $pid; if ($pid == 0) { open my $fh1, '<', "$val1.txt" or die "Open fail: $!"; flock($fh1, LOCK_SH) or die "Cannot lock $val1.txt ($val2) + - $!\n"; open my $fh2, ">", "$val2$val1.txt" or die "Open fail: $!" +; while (my $line = <$fh1>) { $line =~ s/1/2/; print $fh2 "$line\n"; } flock($fh1, LOCK_UN) or die "Cannot unlock $val1$val2.txt +- $!\n"; exit 0; } } }

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


In reply to Re: Help with multiple forks by kennethk
in thread Help with multiple forks by Anonymous Monk

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.