rhumbliner has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use Unix::PID; ## problem code: my $pid = Unix::PID->new(); #my @pids = $pid->get_pidof('fork.pl'); #die "one copy of me is already running\n" if scalar(@pids) > 0; ## :problem code print "<parent>: my process id: $$\n"; my $childcnt = 0; foreach my $process (@ARGV) { my $pid = fork; if (defined $pid ) { if ($pid ) { # parent process if (++$childcnt >= 2) { # hit child limit print "<parent>: must wait to start more\n"; my $completed_pid = wait; $childcnt--; print "<parent>: $completed_pid is done; $childcnt run +ning\n"; } else { # more children ok print "<parent>: $childcnt running; can start more\n"; } } else { # child process print "<$process>: my id: $$\n"; sleep int(rand(10)); print "<$process>: end: $$\n"; exit 0; } } else { # fork failed warn "<parent>: fork failed $!\n"; last; } } print "<parent>: waiting for $childcnt to complete\n"; until ((my $completed_pid = wait) == -1) { $childcnt--; print "<parent>: $completed_pid is done; $childcnt running\n"; }; print "<parent>: complete; $childcnt process(es) left\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: get_pidof() messes with wait()?
by liverpole (Monsignor) on Mar 08, 2006 at 12:53 UTC | |
by rhumbliner (Sexton) on Apr 18, 2006 at 17:13 UTC |