This is strange!!! What happens in here???

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
 
A

anno4000

trxrse said:
I have a piece of code and I really don't understand what's
happening...

Nor do I, finding your code a bit hard to follow. I noticed

if ($finished_child=$_){

in your code. That should almost certainly be "$finished_child == $_".
The code:

[snipped]

Anno
 
H

hymie!

In our last episode, the evil Dr. Lacto had captured our hero,
I have a piece of code and I really don't understand what's
happening...

Found it.
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){ vvvvv
if ($finished_child=$_){ ^^^^^
print "\$finished_child=",$finished_child,"\n";
print "\$_=",$_,"\n";
print "I am the parent and process $_ finished!!!\n";
$_=0;
}
}
}

As soon as you finish your first child, you declare them all finished.
Thus the while loop only runs once.

hymie! http://www.smart.net/~hymowitz (e-mail address removed)
===============================================================================
When speaking to a Bear of Very Little Brain, remember that long words may
Bother him. -- Winnie the Pooh
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top