My guess is that you have one or more logic errors in the program.    I'm also not sure why you're using @ARGV -- is this just to control how many processes get started?  If so, that's okay, but it's a little misleading that you call the loop variable $process.

You may also be expecting that get_pidof is returning the process id only of any running fork.pl programs, but you may have others -- for example, if you're editing fork.pl in the background, then get_pid can match it instead.  So first of all, try passing a second argument to get_pidof:

my @pids = $get_pidof('fork.pl', 1);
which should cause it to find processes which exactly match fork.pl.

Next of all, why are you decrementing $childcnt when the return from the wait is -1?  According to perldoc -f wait:

wait Behaves like the wait(2) system call on your system: it + waits for a child process to terminate and returns the pid of + the deceased process, or "-1" if there are no child process +es. The status is returned in $?. Note that a return value of +"-1" could mean that child processes are being automatically + reaped, as described in perlipc.
Which means you probably want to decrement only when you get a value greater than zero for $completed_pid.  Additionally, you are executing a blocking wait, so the wait will block until it succeeds, or returns -1 for "no more children".  If you want to make it non-blocking, try something like this:
while (1) { my $completed_pid = waitpid(-1, WNOHANG); if ($completed_pid == -1) { # No more children -- exit loop last; } if ($completed_pid > 0) { print "<parent>: $completed_pid is done; $childcnt running\n"; $childcnt--; sleep 1; } }

You'll also need use POSIX ":sys_wait_h"; at the top of the program.  You can read more about non-blocking waits with perldoc -f waitpid.

Update:  I discovered another possible reason why you're getting a negative $childcount when you use get_pidof() ... it's because get_pidof is itself spawning a process which is a child of fork.pl.  This causes your program to wait for it and it decrements $childcount one too many times.


@ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"

In reply to Re: get_pidof() messes with wait()? by liverpole
in thread get_pidof() messes with wait()? by rhumbliner

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.