in reply to Getting around Win32 limitations

What provided in this reply is garbage, but a small change should largely improve it. Use waitpid, instead of wait. If you call waitpid in this way: waitpid($child, 1), it will not blocking the main process. You just from time to time, come back and checking return code form this call, if it is -1, it means the child process died, and you get an alarm. This can be wrapped into an implementation of alarm, which is not implemented in active perl.
my $child_pid = fork(); if ($child_pid) { $ret = wait($child_pid, 1); do { ... #whatever you want; } until ($ret == -1); print "got alarm\n"; } else { sleep($seconds); }

Replies are listed 'Best First'.
Re: Re: Getting around Win32 limitations
by pg (Canon) on Nov 10, 2002 at 06:37 UTC
    I wrapped the thing up:
    alarm_pg.pm: package pg_alarm; use strict; sub new { shift; my $seconds = shift; my $self = {}; $self->{CHILD_PID} = fork(); if ($self->{CHILD_PID} == 0) { sleep($seconds); } bless $self; return $self; } sub alarmed { my $self = shift; my $result = 0; if (defined($self->{CHILD_PID})) { my $ret = waitpid($self->{CHILD_PID}, 1); if ($ret == -1) { $result = 1; } } return $result; } 1; test.pl: use pg_alarm; use strict; my $a = pg_alarm->new(10); while (!$a->alarmed()) { #print "haven't gotten alarm yet\n"; } print "got alarm\n";