Linux ホストで perl スクリプトを実行しています。分岐するスクリプトを作成しようとしています。このプログラムでは、子が永遠にかかるプログラムを開始し、親が 5 秒後にタイムアウトして子を殺します。ここに私が持っているものがあります:
my $start = time();
my $timeOut = 5;
my $childPid = fork();
if ($childPid) {
# I am the parent, and $childPid is my child
while (kill(0, $childPid)) {
if (time() > $start + $timeOut) {
$numKilled = kill(SIGTERM, $childPid);
printf("numKilled: %s\n", $numKilled);
}
sleep 1;
}
}
else {
# I am the child - do something that blocks forever
`adb -s 410bf6c1 logcat`;
exit;
}
出力:
aschirma@graphics9-lnx:~$ perl perl-fork-test.pl
numKilled: 1
numKilled: 1
numKilled: 1
numKilled: 1
numKilled: 1
...
私が期待する動作は、「numKilled: 1」が 1 回だけ表示され、子プロセス (およびそのすべての子プロセス) が約 5 秒後に強制終了されることです。しかし、実験から、子供もその子供も殺されていないことがわかります。はkill(SIGTERM, $childPid)
何もしていないように見えます。
どうすれば実際に子供を殺すことができますか?