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

Discussion in 'Perl Misc' started by trxrse, Jan 12, 2007.

  1. trxrse

    trxrse Guest

    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
    trxrse, Jan 12, 2007
    #1
    1. Advertising

  2. trxrse

    -berlin.de Guest

    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"};
    -berlin.de, Jan 12, 2007
    #2
    1. Advertising

  3. trxrse

    hymie! Guest

    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
    ===============================================================================
    hymie!, Jan 12, 2007
    #3
  4. trxrse

    trxrse Guest

    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
    > ===============================================================================
    trxrse, Jan 12, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    420
    Shapper
    May 20, 2005
  2. NM
    Replies:
    6
    Views:
    450
    Default User
    Sep 20, 2006
  3. Rafe
    Replies:
    2
    Views:
    400
  4. George Hester

    Try over here likely more to the point here

    George Hester, Sep 30, 2004, in forum: Javascript
    Replies:
    0
    Views:
    103
    George Hester
    Sep 30, 2004
  5. FAQ server
    Replies:
    0
    Views:
    145
    FAQ server
    Aug 10, 2006
Loading...

Share This Page