Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > This is strange!!! What happens in here???

Reply
Thread Tools

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

 
 
trxrse
Guest
Posts: n/a
 
      01-12-2007
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

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      01-12-2007
trxrse <> wrote in comp.lang.perl.misc:
> 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

--
$anagram = 'Knuth heals rare project'; # by Abigail
push @{ $pos{ $_}}, $pos ++ for split //, lc $anagram;
print "print +(split //, '$anagram')[ $_]\n" for
join ', ', map shift @$_, @pos{ split //, lc "Just another Perl hacker"};
 
Reply With Quote
 
 
 
 
hymie!
Guest
Posts: n/a
 
      01-12-2007
In our last episode, the evil Dr. Lacto had captured our hero,
"trxrse" <>, who said:
>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
================================================== =============================
When speaking to a Bear of Very Little Brain, remember that long words may
Bother him. -- Winnie the Pooh
================================================== =============================
 
Reply With Quote
 
trxrse
Guest
Posts: n/a
 
      01-12-2007
Thanks very much both, that was the problem!!!


trxrse


hymie! wrote:
> In our last episode, the evil Dr. Lacto had captured our hero,
> "trxrse" <>, who said:
> >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
> ================================================== =============================
> When speaking to a Bear of Very Little Brain, remember that long words may
> Bother him. -- Winnie the Pooh
> ================================================== =============================


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What happens when type conversion between signed and unsigned happens? NM C++ 6 09-20-2006 05:39 PM
Re: OT: Here's What Happens When you Hire Paper Certs Pedro Simoes MCSE 0 11-24-2005 07:18 PM
OT: Here's What Happens When you Hire Paper Certs T-Bone MCSE 1 11-16-2005 08:02 PM
Overrides Function OnBubbleEvent As Boolean ---> What happens when Return True|False ? Andreas Klemt ASP .Net 0 07-04-2003 12:04 AM
Access to the path ...\\dynamic_images is denied. What Happens??? cg ASP .Net 0 07-02-2003 07:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57