in reply to Re^2: Forking child processes.
in thread Forking child processes.
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); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Forking child processes.
by sojourn548 (Acolyte) on Nov 06, 2009 at 20:29 UTC | |
by ikegami (Patriarch) on Nov 06, 2009 at 20:59 UTC |