Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Fork Wait and probably grandchildren

by tlm (Prior)
on Apr 05, 2005 at 17:19 UTC ( [id://445044]=note: print w/replies, xml ) Need Help??


in reply to Re: Fork Wait and probably grandchildren
in thread Fork Wait and probably grandchildren

frodo72, I've seen the 1 while wait > 0 idiom before, but I don't understand the rationale for it. It ends up calling wait at least once, maybe twice if all went well. But why the loop? Why not just collect the first value that wait returns if you care to know what it is, or else just call wait once in a void context?

the lowliest monk

Replies are listed 'Best First'.
Re^3: Fork Wait and probably grandchildren
by polettix (Vicar) on Apr 05, 2005 at 17:52 UTC
    Please consider that I've a Linux bias answering this kind of questions, so don't trust them too much :)

    wait basically waits until one of the children exits. It is used to avoid having Zombie processes: if you don't wait() for them, and you go a long time in your parent process before exiting, you'll see these processes in the process list even if they already exited, marked as "zombie" (this means that resources in the kernel associated to the process aren't freed). So, wait is the way by which the parent process acknowledges the kernel of the son's death. It's interesting that this mechanism actually tries to force the programmer not to ignore return values from child processes, much like the good practice of never throwing away return values from functions.

    Moreover, wait returns as soon as the first child exits; in the OP's post, (s)he created 8 of them, and had to wait until all of them have exited. According to perldoc -f wait, a call to the wait function returns -1 if, and only if, there are no more children alive(1) - thus the cycle.

    (1)Ok, ok, it's not exactly like this. If you happened to set $SIG{'CHLD'} to 'IGNORE', this zombie-ridding happens automatically, and you're likely to have wait always return -1 - but I guess it's not the case with this thread :) Moreover, in this case I would discourage the OP to do that, because (s)he's using wait as a synchronisation mechanism to understand when all the children exited. BTW, this is documented in perldoc perlipc, but I suggest taking a look in perldoc perlport as well.

    Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

    Don't fool yourself.

      Thanks. RazorbladeBidet's last post finally knocked some sense into my thick head. When I read your post originally, I didn't catch that the idiom you posted was supposed to appear outside the loop. In retrospect it is pretty obvious. Doh!

      the lowliest monk

      Update: Added clarification.

Re^3: Fork Wait and probably grandchildren
by RazorbladeBidet (Friar) on Apr 05, 2005 at 17:24 UTC
    Because wait ...
    ...waits for a child process to terminate and returns the pid of the deceased process...
    So while child process A dies and it returns some PID > 0 child process B continues to run. It will return -1 when there are no child processes. Unlikely to happen, but since it is linked to wait(2) - a possibility, perhaps?
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."

      But wait blocks, no? If the parent calls wait it can't do anything else while the child terminates, and by the time wait returns a value, all has been decided for the child process in question.

      the lowliest monk

        Running
        #!/usr/bin/perl -w use strict; for ( my $i = 0; $i < 11; $i++ ) { defined( my $pid = fork() ) or print STDERR "Unable to fork!"; if ( $pid ) { print "Child $i is $pid\n"; } else { print "Hi! I'm just a kid.. I'm going to sleep now..\n"; sleep rand(10); print "*Yawn* I'm awake! Huh? It's me! Kid $$! Bye bye! Time t +o die!\n"; exit 0; } } 1 while wait > 0; print "Screw this, I'm done waiting for those kids\n";
        on AIX 5.2 gives
        Child 0 is 876574 Hi! I'm just a kid.. I'm going to sleep now.. Child 1 is 1089612 Hi! I'm just a kid.. I'm going to sleep now.. Child 2 is 1216572 Hi! I'm just a kid.. I'm going to sleep now.. Child 3 is 913636 Hi! I'm just a kid.. I'm going to sleep now.. Child 4 is 1732802 Hi! I'm just a kid.. I'm going to sleep now.. Child 5 is 1470700 Hi! I'm just a kid.. I'm going to sleep now.. Child 6 is 2162768 Hi! I'm just a kid.. I'm going to sleep now.. Child 7 is 2555954 Hi! I'm just a kid.. I'm going to sleep now.. Child 8 is 2793494 Hi! I'm just a kid.. I'm going to sleep now.. Child 9 is 2863246 Hi! I'm just a kid.. I'm going to sleep now.. Child 10 is 3080302 Hi! I'm just a kid.. I'm going to sleep now.. *Yawn* I'm awake! Huh? It's me! Kid 1470700! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 2863246! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 2162768! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 876574! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1089612! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1732802! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 2555954! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 913636! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1216572! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 2793494! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 3080302! Bye bye! Time to die! Screw this, I'm done waiting for those kids
        changing that to just wait does
        Child 0 is 2793496 Hi! I'm just a kid.. I'm going to sleep now.. Child 1 is 1077260 Hi! I'm just a kid.. I'm going to sleep now.. Child 2 is 1216574 Hi! I'm just a kid.. I'm going to sleep now.. Child 3 is 913638 Hi! I'm just a kid.. I'm going to sleep now.. Child 4 is 2555956 Hi! I'm just a kid.. I'm going to sleep now.. Child 5 is 1732804 Hi! I'm just a kid.. I'm going to sleep now.. Child 6 is 1089614 Hi! I'm just a kid.. I'm going to sleep now.. Child 7 is 876576 Hi! I'm just a kid.. I'm going to sleep now.. Child 8 is 2162770 Hi! I'm just a kid.. I'm going to sleep now.. Child 9 is 2863248 Hi! I'm just a kid.. I'm going to sleep now.. *Yawn* I'm awake! Huh? It's me! Kid 2863248! Bye bye! Time to die! Child 10 is 1470702 Hi! I'm just a kid.. I'm going to sleep now.. Screw this, I'm done waiting for those kids *Yawn* I'm awake! Huh? It's me! Kid 913638! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1470702! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 2793496! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1077260! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 876576! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 2162770! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1216574! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1089614! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 1732804! Bye bye! Time to die! *Yawn* I'm awake! Huh? It's me! Kid 2555956! Bye bye! Time to die!
        --------------
        "But what of all those sweet words you spoke in private?"
        "Oh that's just what we call pillow talk, baby, that's all."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://445044]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found