It seems the problem can be solved by using POSIX exit.
(http://perldoc.perl.org/functions/exit.html )
Thanks to Chris who is in our IT department for pointing me to this. I
should have read it more carefully!

" The exit() function does not always exit immediately. It calls any
defined END routines first, but these END routines may not themselves
abort the exit. Likewise any object destructors that need to be called
are called before the real exit. If this is a problem, you can call
POSIX:_exit($status) to avoid END and destructor processing."

Thanks all!

-----Original Message-----
From: TAO, NENGBING [AG/1005] 
Sent: Saturday, April 02, 2011 11:07 AM
To: 'beginners-digest-h...@perl.org'; beginners@perl.org
Subject: Excessive wait time after fork

Hi,

        I noticed that after fork, the wait time is excessive (my other
test waited for hours). Is this a known problem, how to get around it?

        This happens with ForkManager module also.

        
        Any pointers on what's going on is appreciated!

        Bob


        ########code########
#!/usr/bin/env perl

my @in = @ARGV;
my $num = scalar @in;

my @children;

for (my $i = 0; $i < $num; $i++) {
  my $pid = fork();
  if ($pid) { # parent
    push @children, $pid;
    print "parent $pid ", scalar localtime(),"\n";
  } elsif ($pid == 0) { # child
    print "child $i BEFORE sub call : ", scalar localtime(),"\n";
    #sub
    my $ref=longP($in[$i]);
    print "child $i AFTER  sub call :", scalar localtime()," numKeys:",
scalar keys %{$ref},"\n";
    #sleep $in[$i];
    exit;
  } else {
    print STDERR "couldn't fork\n";
  }
}

print "before WAIT : ", scalar localtime(),"\n";
foreach my $child (@children) {
  waitpid($child, 0);
  print "Child $child done at ", scalar localtime(),"\n";
}
print "after  WAIT : ", scalar localtime(),"\n";

print "Finished at ", scalar localtime(),"\n";

sub longP {
  my ($i)=@_;
  my %h=();
  for my $j (0 .. $i * 1000000) {
    $h{$j}=$j*100;
  }
  return \%h;
}


########command########
myTestOfFork 100 20 30 4 5 6 > O_myTestOfFork

########output########
parent 3052 Sat Apr  2 10:44:54 2011
parent 3053 Sat Apr  2 10:44:54 2011
parent 3054 Sat Apr  2 10:44:54 2011
parent 3055 Sat Apr  2 10:44:54 2011
parent 3056 Sat Apr  2 10:44:54 2011
child 3 BEFORE sub call : Sat Apr  2 10:44:54 2011
child 3 AFTER  sub call :Sat Apr  2 10:45:01 2011 numKeys:4000001
child 4 BEFORE sub call : Sat Apr  2 10:44:54 2011
child 4 AFTER  sub call :Sat Apr  2 10:45:04 2011 numKeys:5000001
child 5 BEFORE sub call : Sat Apr  2 10:44:54 2011
child 5 AFTER  sub call :Sat Apr  2 10:45:06 2011 numKeys:6000001
child 1 BEFORE sub call : Sat Apr  2 10:44:54 2011
child 1 AFTER  sub call :Sat Apr  2 10:45:41 2011 numKeys:20000001
child 2 BEFORE sub call : Sat Apr  2 10:44:54 2011
child 2 AFTER  sub call :Sat Apr  2 10:45:56 2011 numKeys:30000001
child 0 BEFORE sub call : Sat Apr  2 10:44:54 2011
child 0 AFTER  sub call :Sat Apr  2 10:48:45 2011 numKeys:100000001   #
all sub calls are finished at this time
parent 3057 Sat Apr  2 10:44:54 2011
before WAIT : Sat Apr  2 10:44:54 2011
Child 3052 done at Sat Apr  2 10:50:21 2011
Child 3053 done at Sat Apr  2 10:50:21 2011
Child 3054 done at Sat Apr  2 10:50:21 2011
Child 3055 done at Sat Apr  2 10:50:21 2011
Child 3056 done at Sat Apr  2 10:50:21 2011
Child 3057 done at Sat Apr  2 10:50:21 2011
after  WAIT : Sat Apr  2 10:50:21 2011                                #
why wait for additional 1:36 ? 
Finished at Sat Apr  2 10:50:21 2011

                               
This e-mail message may contain privileged and/or confidential information, and 
is intended to be received only by persons entitled
to receive such information. If you have received this e-mail in error, please 
notify the sender immediately. Please delete it and
all attachments from any servers, hard drives or any other media. Other use of 
this e-mail by you is strictly prohibited.

All e-mails and attachments sent and received are subject to monitoring, 
reading and archival by Monsanto, including its
subsidiaries. The recipient of this e-mail is solely responsible for checking 
for the presence of "Viruses" or other "Malware".
Monsanto, along with its subsidiaries, accepts no liability for any damage 
caused by any such code transmitted by or accompanying
this e-mail or any attachment.


The information contained in this email may be subject to the export control 
laws and regulations of the United States, potentially
including but not limited to the Export Administration Regulations (EAR) and 
sanctions regulations issued by the U.S. Department of
Treasury, Office of Foreign Asset Controls (OFAC).  As a recipient of this 
information you are obligated to comply with all
applicable U.S. export laws and regulations.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to