Travis Thornhill wrote:
> I thought I understood this but maybe I don't.
Have you read the perlipc doc:
perldoc perlipc
It has examples on how to use fork.
> When perl forks it creates an exact copy of itself with open files, same
> variables, hashes, arrays, etc.
>
> But when a variable in one changes, do they all change?
fork() creates a separate process which is a copy of the parent process but
any changes in the child will not effect the parent.
> What's wrong with how I'm trying to use the $children variable to track
> whether or not I still have processes running?
Use waitpid to track the children:
perldoc -f waitpid
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $pid;
> my $children = 0;
>
> my @args = (
> "arg1",
> "arg2",
> "arg3",
> "arg4",
> );
>
> foreach my $arg ( @args ) {
> if ( $pid = fork() ) {
> #parent
> #do nothing yet
> } else {
You should verify that fork worked:
die "Cannot fork: $!" unless defined $pid;
> #child
> $children++;
> system("my_command -a $arg");
system() forks another process to run 'my_command'. You should just use exec
here:
exec 'my_command', '-a', $arg or die "Cannot exec 'my_command' $!";
> $children--;
> exit;
> }
> } # end foreach
>
> # didn't let children fall through, so this is in the parent process
> # this is where I do lots of stuff waiting for $children to equal zero
>
> while ( $children ) {
> # gather some data from "ps" and parsing log files, etc...
> }
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>