You've got several issues going on. First, if you're using fork on Windows, you're really using Perl threads behind the scenes. See perlfork. If you want to work with subprocesses on Windows, I recommend checking out Win32::Job. As mentined above, getting data back from a fork is harder than getting it back from a thread -- you probably need to use some sort of external file or pipe. See perlipc. If you're trying to do multiple child processes at the same time, you've got added complexity in handling returned data.

For managing multiple, forked processes in general, I've seen many recommendations for Parallel::ForkManager as a cleaner interface. There's also a tutorial on it: Introduction to Parallel::ForkManager. It looks like what you want can be boiled down to something like this (adapted from the docs for it):

use Parallel::ForkManager; $pm = new Parallel::ForkManager($MAX_PROCESSES); my @task_details = ( # data needed for each sub-process ); foreach my $task (@task_details) { $pm->start and next; # do the fork # do the sub process work using whatever details are in $task # write the results to a file somewhere, perhaps with a # pre-set name passed in $task $pm->finish; # do the exit in the child process } $pm->wait_all_children; # Continue in parent and read in the results

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: forks, how do that? by xdg
in thread forks, how do that? by apolo

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.