The waitpid function : waitpid « System Functions « Perl






The waitpid function

     

#!/usr/bin/perl
use warnings;
use strict;

my ( $pid, $pid2 );
$| = 1;

if ( ( $pid = fork() ) && ( $pid2 = fork() ) ) {
   print( "I have to wait for my kids.\n" );
   my $straggler = waitpid( $pid, 0 );
   print( "Finally $straggler finished, now I can go.\n" );
}
elsif ( $pid && defined( $pid2 ) ) {
   print( "Kid 2: Mine is not...\n" );
   print( "Kid 2: Hey! Wait for me!!!\n" );
   exit();
}
elsif ( defined( $pid ) ) {
   print( "Kid 1: My parent is very patient...\n" );
}
else {
   die( "Forking problems: " );
}

   
    
    
    
    
  








Related examples in the same category

1.Using waitpid.
2.waitpid waits for a particular child process to complete, returning 1 if the process is found and -1 on an error.