in reply to Re: Setting an alarm
in thread Setting an alarm

I am on UNIX.

Forking kids is new to me. After looking around, I came up with this:

my $pid; ... if (!stat($png)) { $SIG{'CHLD'} = "wait_for_child"; $pid = fork(); if ($pid) { "Creating thumbnail for: $pdf (pid $pid)\n"; } else { create_thumbnail($pdf,$png); exit(0); } } sub wait_for_child { print "Waiting on $pid..."; sleep(1); }

I haven't actually run the code yet for two reasons. This will generate all of them at the same time (forking a child for each PDF). And I don't have anything to check how long they are taking.

Can you add some pseudo code that would point me in the right direction?

Thanks!

Replies are listed 'Best First'.
Re^3: Setting an alarm
by ikegami (Patriarch) on Mar 10, 2010 at 17:34 UTC

    The $SIG{CHLD} + sleep approach you took (instead of wait/waitpid) is interesting, since it saves you from using alarm. While an alarm would interrupt wait/waitpid, why use signals when they're not needed.

    use POSIX qw( _exit ); if (!stat($png)) { my $child_code; local $SIG{CHLD} = sub { wait(); # Reap child $child_code = $?; }; defined( my $pid = fork() ) or die("Can't fork: $!\n"); if (!$pid) { # Child if (eval { create_thumbnail($pdf,$png); 1 }) { _exit(0); } else { # Especially deadly version of die() print STDERR $@; _exit($! || $?>>8 || 255); } } print "Waiting on $pid...\n"; sleep($timeout); # Interrupted by SIGCHLD since we have a handler if (defined($child_code)) { die("Child failure\n") if $child_code; } else { $SIG{CHLD} = 'DEFAULT'; kill KILL => $pid; wait(); # Reap child die("Child timeout\n"); } }
      Thanks for taking the time to write this, clears a lot of things up too! I've got it working nicely.
        I had if (!defined($child_code)) where I needed if (defined($child_code)). Fixed.