我正在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;
}
輸出:
[email protected]:~$ perl perl-fork-test.pl
numKilled: 1
numKilled: 1
numKilled: 1
numKilled: 1
numKilled: 1
...
我期望的行爲,我看到「numKilled:1」恰好一次,和子進程(及其任何子女)爲大約5秒後死亡。但是我從實驗中看到,孩子和孩子都沒有死亡。 kill(SIGTERM, $childPid)
似乎什麼都不做。
我怎麼能真正殺死孩子?
你是從某處導入SIGTERM常量嗎?我敢打賭,它的評估結果爲0. – friedo 2013-02-21 21:11:47
您是否在使用'use warnings;嚴格使用;'?爲什麼不? – melpomene 2013-02-21 21:27:49
嘿傢伙們,所以我發現我看到的問題實際上有些不同 - 殺死孩子的過程而不是孩子的任何過程。我不想只編輯整個問題,我應該開始一個新問題嗎? – 2013-02-21 22:40:33