T
trxrse
I have a piece of code and I really don't understand what's
happening...
The code:
#! /bin/perl
# Forking out a series of processes which may take
# a while to run. System waits for any SIGCHILD signal to be
# received back so that it can get the faster ones
# finalised quickly! Once a SIGCHILD signal is received back
# it accounts for it in an array
use strict;
use warnings;
use POSIX ":sys_wait_h";
my @waitlist = (20,10,35);
$SIG{CHLD} = \&REAPER;
my $start = localtime();
print "Started $start\n";
my $parent = $$;
my $child=0;
my @kids=();
my $waitedpid;
my $gotone;
my $finished_child;
my $pid;
my $child_pid;
foreach $child (@waitlist) {
unless ($pid = fork()) {
# Child process
sleep $child;
print "I am the kid # $child and I am completed\n";
exit();
}
# Parent process - loop to start others
push @kids, $pid;
}
print "The kids array\n";
foreach (@kids){
print $_,"\n";
}
print "Parent continues now...\n";
# Parent - wait for returned data
while (&sum_pids > 0) {
print "sum_pids=",&sum_pids,"\n";
while (! $gotone){
sleep 1;
print "Slept 1\n";
}
$gotone=0;
print "finished_child=$finished_child","\n";
foreach (@kids){
if ($finished_child=$_){
print "\$finished_child=",$finished_child,"\n";
print "\$_=",$_,"\n";
print "I am the parent and process $_ finished!!!\n";
$_=0;
}
}
}
print "And in the end sum_pids=",&sum_pids,"\n";
print "Completed!!!\n";
sub REAPER {
if (($finished_child = waitpid(-1, &WNOHANG)) > 0){
$gotone = 1;
}
# $SIG{CHLD} =\&REAPER;
}
sub sum_pids{
my $temp;
foreach $child_pid (@kids){
$temp+=$child_pid;
}
$temp;
}
The output:
Started Fri Jan 12 11:03:53 2007
I am the kid # 10 and I am completed
The kids array
5332
5333
5335
Parent continues now...
sum_pids=16000
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
finished_child=5333
$finished_child=5332
$_=5332
I am the parent and process 5332 finished!!!
$finished_child=5333
$_=5333
I am the parent and process 5333 finished!!!
$finished_child=5335
$_=5335
I am the parent and process 5335 finished!!!
And in the end sum_pids=0
Completed!!!
I am the kid # 20 and I am completed
I am the kid # 35 and I am completed
The questions: How is it possible to display only once
"finished_child=5333" and then to display $finished_child three times?
That would mean that it's catching only one SIGCHLD but somehow it's
executing that "if ($finished_child) three times. How come?
Thanks
trxrse
happening...
The code:
#! /bin/perl
# Forking out a series of processes which may take
# a while to run. System waits for any SIGCHILD signal to be
# received back so that it can get the faster ones
# finalised quickly! Once a SIGCHILD signal is received back
# it accounts for it in an array
use strict;
use warnings;
use POSIX ":sys_wait_h";
my @waitlist = (20,10,35);
$SIG{CHLD} = \&REAPER;
my $start = localtime();
print "Started $start\n";
my $parent = $$;
my $child=0;
my @kids=();
my $waitedpid;
my $gotone;
my $finished_child;
my $pid;
my $child_pid;
foreach $child (@waitlist) {
unless ($pid = fork()) {
# Child process
sleep $child;
print "I am the kid # $child and I am completed\n";
exit();
}
# Parent process - loop to start others
push @kids, $pid;
}
print "The kids array\n";
foreach (@kids){
print $_,"\n";
}
print "Parent continues now...\n";
# Parent - wait for returned data
while (&sum_pids > 0) {
print "sum_pids=",&sum_pids,"\n";
while (! $gotone){
sleep 1;
print "Slept 1\n";
}
$gotone=0;
print "finished_child=$finished_child","\n";
foreach (@kids){
if ($finished_child=$_){
print "\$finished_child=",$finished_child,"\n";
print "\$_=",$_,"\n";
print "I am the parent and process $_ finished!!!\n";
$_=0;
}
}
}
print "And in the end sum_pids=",&sum_pids,"\n";
print "Completed!!!\n";
sub REAPER {
if (($finished_child = waitpid(-1, &WNOHANG)) > 0){
$gotone = 1;
}
# $SIG{CHLD} =\&REAPER;
}
sub sum_pids{
my $temp;
foreach $child_pid (@kids){
$temp+=$child_pid;
}
$temp;
}
The output:
Started Fri Jan 12 11:03:53 2007
I am the kid # 10 and I am completed
The kids array
5332
5333
5335
Parent continues now...
sum_pids=16000
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
Slept 1
finished_child=5333
$finished_child=5332
$_=5332
I am the parent and process 5332 finished!!!
$finished_child=5333
$_=5333
I am the parent and process 5333 finished!!!
$finished_child=5335
$_=5335
I am the parent and process 5335 finished!!!
And in the end sum_pids=0
Completed!!!
I am the kid # 20 and I am completed
I am the kid # 35 and I am completed
The questions: How is it possible to display only once
"finished_child=5333" and then to display $finished_child three times?
That would mean that it's catching only one SIGCHLD but somehow it's
executing that "if ($finished_child) three times. How come?
Thanks
trxrse