一定時間経過したら子プロセスをkillする

IPC::Cmdを使えばすっきり書けそうなのだが、コアモジュールに入ったのはv5.9.5から。

$ corelist -a IPC::Cmd
IPC::Cmd was first released with perl v5.9.5

IPC::Cmdを使わないとするならこんな感じか。

#!/usr/bin/perl
use strict;
use warnings;

my $pid = fork;
die $! unless defined $pid;

# child proc
if ( $pid == 0 ) {
    exec('./child.sh'); # just sleep(10)
}

# parent
else {
    local $SIG{ALRM} = sub {
        kill(9, $pid);
        die "child proc timed out. killed $pid";
    };

    alarm 3;
    wait;

    print 'child proc successfull finished.';
}

ついでにman 2 waitしてみると..

 wait, wait3, wait4, waitpid -- wait for process termination

いっぱいある。。 よき機会なので、Advanced Programming in the UNIX EnvironmentのChapter8をちゃんと読むか。