あなたの質問に文脈を与えることは重要です。あなたはすでに親と子の2つのプロセスを持っています。子はexecと置き換えられているので、子を使って監視することはできませんが、親は利用可能です。 waitpid
コールをノンブロッキングにするだけです(つまり、正常に終了するまで待つことはなく、すぐに失敗します)。また、これはeval
とalarm
機能の必要性を取り除きます:スレッドとしてそれを産卵して、設定する値を待っている(あなたがPerlを有効にスレッドを持っていると仮定)について
#!/usr/bin/perl
use strict;
use warnings;
use POSIX ":sys_wait_h";
my $timeout = 180;
my $program = "simulator --shell";
die "could not fork: $!" unless defined (my $pid = fork);
#this is the child process
unless ($pid) {
exec $program;
#if we reach this code the exec failed
die "exec of simulator failed: $!";
}
#this is the parent process
my $tries = 0;
#check to see if $pid is done, but don't block if it isn't
until (waitpid(-1, WNOHANG) == $pid) {
#put what you want to print while waiting here:
print scalar localtime, "\n";
if ($tries++ > $timeout) {
warn "timed out, sending SIGKILL to simulator\n";
kill 9, $pid;
waitpid($pid, 0);
last;
}
} continue {
sleep 1;
}
進捗バーを作成しようとしていますか? –
オプションになります。 – YoDar