(I've been out of the game for a bit, so all of this goes into the "if I recall correctly" category)

Whenever you do a fork() call, the system:

  1. Creates a new system-level thread.
  2. Makes a duplicate of your existing process.
  3. Does some other housekeeping.

This is an oversimplification, and as such is totally inaccurate. :) Even so, it shows that every time you do a fork(), it costs time and (frequently) memory. Also, if you're processing a bunch of items and have to wait until they're all done before you move on to the next step, it's frequently no faster to fork off subprocesses than it is to do the whole thing step-by-step. (This is especially true if you're on a single-processor machine-- multithreading won't do it faster. It will just make all of your processes fight for time on the processor.)

This is only my opinion, but I would REALLY not fork off one process per record. Whether it would bog down an average PC would depend on how processor-intensive the task is.

If forking off background processes still looks good, it's generally a good idea to decide on a maximum number of threads, create them as necessary, and parcel out tasks to them as appropriate. I'll leave exactly how to do that to you. :)

By "how do I store this for use in the parent process", I think you mean "How do I get the value of a changed variable in a child process back to the parent process?" The answer is, not directly. Your child process is running in a completely different Perl interpreter, so there's no way to pass a variable. If you want to write a result back to the parent process, you'll need to write it via an open filehandle or named pipe. Look at the perlipc manpage for details.

The function you're looking for to wait until all the child processes are finished is the wait() function. You'd do it like so:

use strict; sub ret($); MAIN: { my @array=(0 .. 100); foreach my $line (@array) { my $pid = fork and next; ret($line); sleep(1); exit(); } wait(); print "Task done\n"; } sub ret($) { my ($line) = @_; print "$line\n"; }

Trying to run it from 1 to 1000 bombs matters out at around 571 with this script, so once again, forking off 1000 children is a Bad Idea. :)

Good luck!

stephen


In reply to Re: basic fork question by stephen
in thread basic fork question by sickboy

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.