Using fork to create child processes : fork « System Functions « Perl






Using fork to create child processes

     


#!/usr/bin/perl

use warnings;
use strict;

$| = 1;
my $pid = 0;
if ( $pid = fork() ) {
   print( "Parent executing.\n" );
   sleep( 2 );
   print( "Parent finished.\n" );
}
elsif ( defined( $pid ) ) {
   print( "Child executing.\n" );
   sleep( 2 );
   print( "Child finished.\n" );
   exit();
}
else {
   die( "Could not fork" );
}

   
    
    
    
    
  








Related examples in the same category

1.fork returns the child's process ID
2.Using fork and pipe.
3.To retrieve the process ID for the parent process for your program, call the function getppid.
4.Distinguish between the parent and child processes