in reply to Re^2: Forking child processes.
in thread Forking child processes.

I'd try the following:

#!/usr/bin/perl use strict; use warnings; use POSIX qw( _exit ); use Time::HiRes qw( sleep time ); # Optional. use constant PERIOD => 10.0; sub tick { my $pm = ...; ... } sub sleep_till { my ($sleep_till) = @_; for (;;) { my $duration = $sleep_till - time(); last if $duration <= 0; sleep($duration); } } { $SIG{CHLD} = 'IGNORE'; # Autoreap children my $time = time(); for (;;) { $time += PERIOD; sleep_till($time); my $pid = fork(); if (!defined($pid)) { warn("fork: $!\n"); } elsif (!$pid) { $SIG{CHLD} = 'DEFAULT'; _exit(0) if eval { tick(); 1 }; print STDERR $@; _exit($! || ($?>>8) || 255); } } }

Replies are listed 'Best First'.
Re^4: Forking child processes.
by sojourn548 (Acolyte) on Nov 06, 2009 at 20:29 UTC

    ikegami, question about the sleep_till() function. I am sure there is a good reason, but why wouldn't you just do:

    sub sleep_till { my ($sleep_till) = @_; my $duration = $sleep_till - time(); if ($duration > 0){ sleep($duration); } }
      Signals interrupt sleep. Mind you, no signals are being handled here, so your solution would work too.