For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > fork question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fork question
Travis Thornhill

2006-07-22, 3:57 am

I thought I understood this but maybe I don't.
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?

What's wrong with how I'm trying to use the $children variable to track whether or
not I still have processes running?

#!/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 {
#child
$children++;
system("my_command -a $arg");
$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...
}

#end of script

Thanks in advance for any insights,
Travis.






---------------------------------
Do you Yahoo!?
Next-gen email? Have it all with the all-new Yahoo! Mail Beta.
John W. Krahn

2006-07-22, 3:57 am

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
Venkat Saranathan

2006-07-22, 6:57 pm

No, Remember fork creates a copy of the parent's environment in the child's
process, so whenever you change the child's variable, it won't have any
effect on the parent's and vice versa.

One idea is to increase the child counter in the parent process and
decrement in the SIGCHLD handler which will get invoked when the child
process exits. Please refer to the Camel book by Larry Wall or Perlipc
manpage, it contains information about how to do it. Hint. Look for
"Reaper".

If you are running latest Perl version, you should consider threading
instead of fork. It is platform independent, light-weight and easy to pass
information back and forth. Please refer to perlthrtut for more
information.

Hth,

with warm regards,
Venkat Saranathan
Gulf Breeze Software
www.gulfsoft.com

GulfBreeze Blog
www.gulfsoft.com/blog


-----Original Message-----
From: Travis Thornhill [mailto:madslashers2002@yahoo.com]
Sent: Friday, July 21, 2006 11:47 PM
To: beginners@perl.org
Subject: fork question


I thought I understood this but maybe I don't.
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?

What's wrong with how I'm trying to use the $children variable to track
whether or
not I still have processes running?

#!/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 {
#child
$children++;
system("my_command -a $arg");
$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...
}

#end of script

Thanks in advance for any insights,
Travis.






---------------------------------
Do you Yahoo!?
Next-gen email? Have it all with the all-new Yahoo! Mail Beta.


Travis Thornhill

2006-07-26, 6:57 pm

Yes that helped. I was able to sort out the proper way to handle
processes.

Thanks,
Travis.

Venkat Saranathan <venkat_saranathan@gulfsoft.com> wrote:
No, Remember fork creates a copy of the parent's environment in the child's
process, so whenever you change the child's variable, it won't have any
effect on the parent's and vice versa.

One idea is to increase the child counter in the parent process and
decrement in the SIGCHLD handler which will get invoked when the child
process exits. Please refer to the Camel book by Larry Wall or Perlipc
manpage, it contains information about how to do it. Hint. Look for
"Reaper".

If you are running latest Perl version, you should consider threading
instead of fork. It is platform independent, light-weight and easy to pass
information back and forth. Please refer to perlthrtut for more
information.

Hth,

with warm regards,
Venkat Saranathan
Gulf Breeze Software
www.gulfsoft.com

GulfBreeze Blog
www.gulfsoft.com/blog


-----Original Message-----
From: Travis Thornhill [mailto:madslashers2002@yahoo.com]
Sent: Friday, July 21, 2006 11:47 PM
To: beginners@perl.org
Subject: fork question


I thought I understood this but maybe I don't.
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?

What's wrong with how I'm trying to use the $children variable to track
whether or
not I still have processes running?

#!/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 {
#child
$children++;
system("my_command -a $arg");
$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...
}

#end of script

Thanks in advance for any insights,
Travis.






---------------------------------
Do you Yahoo!?
Next-gen email? Have it all with the all-new Yahoo! Mail Beta.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org






---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com