while (1) {}
is a wasteful way of doing
sleep while 1;
You never collect the child processes you create in the outer parent. You'll accumulate zombies and run out of resources.
Using signals adds fragility, and they're not necessary here.
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); } } }
In reply to Re^3: Forking child processes.
by ikegami
in thread Forking child processes.
by sojourn548
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |