http://qs1969.pair.com?node_id=445047


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

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."

Replies are listed 'Best First'.
Re^4: Fork Wait and probably grandchildren
by tlm (Prior) on Apr 05, 2005 at 17:33 UTC

    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."