Monks,

I have written a script that forks up to a maximum number of processes and then monitors those processes when they are completed to start more up to the maximum number of processes. The script works great except for one issue. After it runs for a while it cores after throwing the following error(s):

Attempt to free unreferenced scalar at <script name> line <line number>

Here is a short example of my script.

#!/usr/bin/perl -w use strict; ###################################################################### +###### ## Load the neccesary modules ###################################################################### +###### use POSIX 'setsid'; use strict; use POSIX ":sys_wait_h"; ################################################################# # Global variables used in the script # ################################################################# my $SLEEP_TIME=120; my $pid = 0; my $counter = 0; my $MAX_PROCESS = 75; my $STOP_FILE = 'stopfile'; my $num_processes = 0; my $process_name = $0; ################################################################ # Handle any zombies # ################################################################ $SIG{CHLD} = \&REAPER; sub REAPER { my $stiff; while (($stiff = waitpid(-1, &WNOHANG)) > 0) { # do something with $stiff if you want } $SIG{CHLD} = \&REAPER; # install *after* calling +waitpid } ############################################################### # This subroutine stops the processes gracefully allowing the # # processes that are running to complete and then it shuts # # down the script. # ############################################################### sub stopGraceful() { while(-e $STOP_FILE && ($num_processes > 1)) { $num_processes = `ps -ef | grep $process_name | grep - +v grep | wc -l`; chomp($num_processes); print "Process number $num_processes.\n"; if($num_processes == 1) { exit(0); } } } sub mainLoop() { while(!(-e $STOP_FILE)) { $counter++; $num_processes = `ps -ef | grep $process_name | grep - +v grep | wc -l`; chomp($num_processes); if($num_processes <= $MAX_PROCESS) { $pid = fork(); die "Cannot fork: $!" unless defined($pid); if ($pid == 0) { # Child process # Insert database_worker script here. print $counter . " : Process number $n +um_processes.\n"; sleep(int(rand($SLEEP_TIME))); exit(0); # Child process exits when +it is done. } # else 'tis the parent process } } if (-e $STOP_FILE) { stopGraceful(); exit(0); } } ############################# ## ## ## ## ## Execution Begins Here ## ## Begin Main Loop ## ## ## ## ## ############################# mainLoop();

The error is centered around the line:

$num_processes = `ps -ef | grep $process_name | grep -v grep | wc -l`;


This is being run on a solaris (sparc) environment on an old PERL version (5.0 patchlevel 4 subversion 4). Like I said it runs fine for a while and then dumps core after those errors.

Any help is appreciated.

Edit: g0n - readmore tags


In reply to Controlling number of processes Forked by raynmaker

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.