maa has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I'm clearly doing something stupid but I can't see what it is. I've broken the test code down to a small case that exhibits the odd behaviour. Can anyone see/explain what I'm doing wrong here? (Apart from using fork() on winnt at all, that is.)
#!C:/Activeperl/bin/perl.exe use strict; use warnings; open (IN, "fork2.ini") or die("Can't open input file $!\n"); my $pid=0; my $x=0; while (my $computer=<IN>) { chomp($computer); $x++; $pid = fork(); if ($pid) { print STDERR "Parent $$ got PID $pid from fork($$ $computer)\n +"; sleep(1); }else { print STDERR "\t$$ $computer\n"; exit(0); } if($x > 4) { do { $pid=wait() } while ($pid != -1); $x=0; } } do { $pid=wait() } while ($pid != -1); print "wait() ed until $pid was returned.\n"; close(IN); __DATA__ Computer1 Computer2 Computer3
The file "fork2.ini" contains the asme as the __DATA__ part... another weirdness is that if I don't use <IN> and use <DATA> instead it prints out the source code!
The output I get from running this is:
C:\logchecks>fork3
Parent 433 got PID -605 from fork(433 test1)
-605 test1
Parent 433 got PID -461 from fork(433 test2)
-461 test2
Parent 433 got PID -533 from fork(433 test3)
-533 test3
Parent 433 got PID -105 from fork(433 test2)
-105 test2
Parent 433 got PID -521 from fork(433 test3)
-521 test3
Parent 433 got PID -670 from fork(433 test3)
-670 test3
wait() ed until -1 was returned.
Done
As you can see, it's printing values from the files out more than once. (Which I don't think it should.)
Seeking enlightenment... Mark.
UPDATE: if I change a couple of things it works...
my (@Computers=(<IN>); #Do this #while (my $computer=<IN>) { #change this while (my $computer=pop @Computers) { #to this
I still don't understand what was going wrong in the original, however.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win32 fork() "problem"
by dawn (Novice) on Mar 26, 2004 at 11:49 UTC | |
by maa (Pilgrim) on Mar 26, 2004 at 12:13 UTC | |
by tilly (Archbishop) on Mar 27, 2004 at 08:49 UTC | |
|
Re: Win32 fork() "problem"
by runrig (Abbot) on Mar 27, 2004 at 09:33 UTC | |
|
Re: Win32 fork() "problem"
by terris (Initiate) on Mar 28, 2004 at 16:14 UTC |