Chris Rodriguez wrote: > > If the alarm function now works on Windows, why doesn't this code give > me the results I expect? Might it matter than I'm using Windows ME? > What about my version of PERL? It's the one from the CD that came with > that book. 5.6 I think - I can check if it matters.
You probably need a later version, but don't bother with alarm. Try something like this (run exactly as is from commandline: perl test.pl or whatever you call it [2 infinite scripts at end]): use strict; use warnings; use Win32::Process; my $timeout = 10 * 1000; # timeout after 10 seconds my @children = (); # you could make this a hash and store more than # the object (script name and pid for example) for (1 .. 2) { # start 2 children scripts my $obj = run_process ("infinite${_}.pl"); push @children, $obj; } my $start = Win32::GetTickCount(); # start timer while (1) { # check for children still running or timeout my $num = 0; for (my $ii = 0; $ii < @children; ++$ii) { next if $children[$ii] == 0; ++$num; my $res = $children[$ii]->Wait(200); $children[$ii] = 0 if $res == 1; } last if not $num; # none running if 0 my $diff = Win32::GetTickCount() - $start; print "num=$num, diff=$diff\n"; last if $diff > $timeout; # timeout after n seconds sleep 1; # give up some CPU } print "kill remaining scripts if any\n"; foreach my $obj (@children) { # kill any scripts still running next if $obj == 0; my $pid = $obj->GetProcessID(); my $exit = 0; Win32::Process::KillProcess($pid, $exit); print "kill $pid ($exit)\n"; } exit 0; sub run_process { my $script = shift; print "Starting $script\n"; my $pobj; Win32::Process::Create($pobj, 'C:\perl\bin\perl.exe', "perl $script", 0, DETACHED_PROCESS, '.') or die "Win32::Process::Create $script: $!"; return $pobj; } __END__ infinite1.pl (this one should finish): #!perl -- open OUT, ">C:/tmp/foo1"; # check file for number of writes binmode OUT; select ((select (OUT), $| = 1)[0]); for (1 .. 2) { print OUT "$0\n"; sleep 2; } close OUT; __END__ infinite2.pl (this one should still be runnig and need killing): #!perl -- open OUT, ">C:/tmp/foo2"; # check file for number of writes binmode OUT; select ((select (OUT), $| = 1)[0]); for (1 .. 2000) { print OUT "$0\n"; sleep 2; } close OUT; __END__ _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs