I have a large list of urls that need to be tested for response time every x number of seconds. I already have a script that iterates through the list, however I would like to fork it out to speed things up.

I came across some code that appears to do what I need to do, and I've trimmed it up for example sake.. However when I run it just with a for loop to test the "iteration" part, I find that each child runs through the entire list, when I really just want the child to run through one of rows in the array -> do it's thing -> and then die..

I've already forkbombed my server once - any help with quickly parsing an array with fork would be greatly appreciated.. The example is below:

#!/usr/bin/perl #... $maxfork= 20; # maximum number of procs $stime = time(); # get current time in secs since 1970 # --- check if older proc is already running ------ stat $lock; if (-e _) { $curtime = scalar localtime($stime); print STDERR "$curtime: LOCKED, skipping.\n"; exit 1; } # nope, so lock it open LOCK, ">$lock"; print LOCK "$$"; close LOCK; $loop = 0; $forked = 0; for ($count=1; $count<50; $count++){ if ( $forked > $maxfork) { wait; $forked--; } if ( $pid = fork ) { $forked++; # parent } elsif ( defined $pid) { # child # The place where I would like to do the work against one of t +he array cells? print "Fork number is $forked - Counter is $count\n"; } else { die "error forking!"; } $loop++; } # wait for child procs while ( wait != -1) { ; };

In reply to Iteration through large array using a N number of forks. by Spesh00

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.