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:[EMAIL PROTECTED] 
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to