#  Fork.pl
#  Example 7.12:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script illustrates using the fork() function.
#  This will only run on Win32 Perl 5.6 or higher.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


# Turn off output buffering (turn on auto flush)
$| = 1;

# Fork the process
defined( $Pid = fork() ) || die "Could not fork: $!\n";
if( 0 == $Pid )
{
    # We get here if we are the child process
    Count( "Child: " );
}
else
{
    # We get here if we are the parent process
    Count( "Parent: " );
}

# Both parent and child will get here
$String = ( ( $Pid )? "Parent" : "Child" ) . " process has finished.\n";
print $String;
	
sub Count
{
    my( $String ) = @_;
    my $iCount = 100;
    
    while( $iCount-- )
    {
        print "$String $iCount\n";
    }
}
