I'm trying to write a script that'll daemonize and allow no more than 6 children to fork but also keep 6 children running while there's work to be done. I'm processing some large data files that get inserted into a db and I've found that six is the max number I can comfortably process at once (I'm continually receiving them around 400/day), eventhough there maybe 20 or more in queue to process. Basically here's the code I have. As is I end up with zombies, but if I use a reaper function I end up getting a -1 back from wait. Any ideas/examples would be greatly appreciated.
while(1) { $processes = 0; foreach $compressedFile ( @pdfFiles ) { $pdfFile = $compressedFile; $pdfFile =~ s/\.Z//; # - For each pdf file fork a new process upto MAX_PROCESSES. if( $my_pid = fork()) { $processes++; if( $processes >= $MAX_PROCESSES ) { $wait = wait(); print "WAIT => $wait\n" if( $debug ); print "PID => $my_pid\n" if( $debug ); if( $wait ) { $processes-- ; } } elsif ( !defined $my_pid ) { die "Error: Cannot fork process: $!"; } } elsif ( $my_pid == 0 ) { &load_file($pdfFile); exit 0; } } if( $daemon_mode ) { $sleep = $sleepTime; print "LOAD_PDF => Sleeping $sleep seconds\n" if( $debug ); sleep $sleep; } else { print "LOAD_PDF => Finished!\n" if ($debug); exit 0; } }

In reply to Daemonized with max processes by TeKk9

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.