To demonstrate the issue with Win32 fork emulation.... I adapted the following from Lincoln Stein's book "Network Programming with Perl" (page 45, figure 2.3, "Using pipe() to create linked filehandles"). Ok... so fork works under Win32 systems but it does not appear to behave in the same manner as it does under Unix / Linux systems. Under Win32 systems, you can run this script 100 times and it will produce the exact same results (except for the time stamps). The first forked process that calls factorial runs to completion and then the second forked process that calls fibonacci runs to completion. Under Unix / Linux, both processes will run concurrently and you'll see interleaved output that can change from run to run.
It could also be that I'm missing something fundamental to Win32 threads (as fork is reported to be implemented using threads under Win32) -- or just about anything else :-). If someone on this list can provide a "fork" example that demonstrates that forked processes do in fact run concurrently under Win32 (that doesn't resort to calling sleep to force context switches), please post the example code.
Regards,
... Dewey
----- begin Win32 fork demo perl script -----
use strict;
use English;
use Getopt::Long;
use Time::HiRes qw( time );
sub factorial($);
sub fibonacci($);
my $arg = 10;
my $result = GetOptions ('count|c=i' => \$arg, );
die "GetOptions failed\n" if !$result;
pipe (my $READER, my $WRITER) or die "can't open pipe: $!\n";
my $starttime = time();
if (fork == 0) {
close($READER);
select($WRITER);
$OUTPUT_AUTOFLUSH=1;
my $thistime = time() - $starttime;
printf "(%f): start factorial process\n", $thistime;
factorial($arg);
$thistime = time() - $starttime;
printf "(%f): done factorial process\n", $thistime;
close($WRITER);
exit(0);
}
if (fork == 0) {
close($READER);
select($WRITER);
$OUTPUT_AUTOFLUSH=1;
my $thistime = time() - $starttime;
printf "(%f): start fibonacci process\n", $thistime;
my $result = fibonacci($arg);
$thistime = time() - $starttime;
printf "(%f): done fibonacci process\n", $thistime;
close($WRITER);
exit(0);
}
close($WRITER);
my $linecount = 0;
my $thistime = time() - $starttime;
printf " (%f): starting read loop\n", $thistime;
while (my $line = <$READER>) {
$linecount++;
printf "(%02d) $line", $linecount;
}
$thistime = time() - $starttime;
printf " (%f): done read loop\n", $thistime;
close($READER);
exit(0);
sub factorial ($) {
my ($target) = @_;
my $result = 1;
foreach my $i ( 1..$target) {
$result *= $i;
my $thistime = time() - $starttime;
printf "(%f): factorial($i) => $result\n", $thistime;
}
}
sub fibonacci ($) {
my ($target) = @_;
my $a = 1;
my $b = 0;
foreach my $i ( 1 .. $target ) {
my $c = $a + $b;
my $thistime = time() - $starttime;
printf "(%f): fibonacci($i) => $c\n", $thistime;
$a = $b;
$b = $c;
}
}
---- end perl script ----
---- begin output ---
(0.023982): starting read loop
(01) (0.024213): start factorial process
(02) (0.024258): factorial(1) => 1
(03) (0.024278): factorial(2) => 2
(04) (0.024291): factorial(3) => 6
(05) (0.024303): factorial(4) => 24
(06) (0.024316): factorial(5) => 120
(07) (0.024328): factorial(6) => 720
(08) (0.024341): factorial(7) => 5040
(09) (0.024354): factorial(8) => 40320
(10) (0.024369): factorial(9) => 362880
(11) (0.024382): factorial(10) => 3628800
(12) (0.024401): done factorial process
(13) (0.032532): start fibonacci process
(14) (0.032598): fibonacci(1) => 1
(15) (0.032621): fibonacci(2) => 1
(16) (0.032634): fibonacci(3) => 2
(17) (0.032647): fibonacci(4) => 3
(18) (0.032659): fibonacci(5) => 5
(19) (0.032671): fibonacci(6) => 8
(20) (0.032683): fibonacci(7) => 13
(21) (0.032696): fibonacci(8) => 21
(22) (0.032709): fibonacci(9) => 34
(23) (0.032721): fibonacci(10) => 55
(24) (0.032738): done fibonacci process
(0.033163): done read loop
---- end output ----
_______________________________________________ Perl-Win32-Users mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
