0
我想生成一個進程,執行一些操作並在完成後手動將其終止。在Perl中刪除分叉的Windows進程
雖然它不工作。這個過程開始,我看到pid和while循環一直運行,直到我殺死它。
難道是Perl產生了一個新的shell,然後衍生出UI進程,當shell(Perl產生的)被殺死時,它不會被Perl殺死?
my $cmd = "notepad.exe";
my $sleepTime = 10;#60 * 10;
$childPid = fork();
if ($childPid)
{ # Parent
print "Started child process id: $childPid\n";
sleep $sleepTime;
$SIG{CHLD} = 'IGNORE';
while (kill(15, $childPid))
{
my $numKilled = kill(SIGTERM, $childPid);
printf("numKilled: %s\n", $numKilled);
printf("numKilled: %s\n", $childPid);
sleep 1;
}
}
elsif ($childPid == 0)
{ # Child
system($cmd);
exit 0; # will never get here
}
else
{ # Unable to fork
die "ERROR: Could not fork new process: $!\n\n";
}
鑑於你的子進程是'notepad.exe',我猜你在Windows上。如果是這樣,你可能要明確提到,因爲Windows上的進程管理可能與在與Perl定型相關的* nix環境中有所不同。 –
如果'$ childPid'爲'undef',那麼'$ childPid == 0'將計算爲* true *並且如果您啓用了警告,將導致'在數字eq(==)'警告中使用未初始化值$ childPid。這意味着最終的'else'子句將永遠不會被執行。您應該使用'elsif(定義的$ childPid)'代替。 – Borodin
fork和signal都是在Windows上由Perl模擬的unix概念。這可能不是最好的方法。你有沒有考慮過使用Win32 :: Process? – ikegami