将输出以下内容:
Start of script
PID=29688
Start of script
PID=0
Running child process 1
Done with child process
PID=29689
Start of script
PID=0
Running child process 1
Done with child process
它正在按预期工作,但是我想杀死以前的子PID。
如何不杀死MAIN就能杀死孩子的PID?
谢谢 !
my $bla = 1;
while (1) {
print "Start of script\n";
run_sleep();
}
sub run_sleep {
sleep(3);
my $pid = fork;
return if $pid; # in the parent process
print("PID=" . $pid . "\n");
print "Running child process " . $bla++ . "\n";
exit(0); # end child process
}
当您派生一个孩子,然后未能在其上等待()时,它退出时将成为已失效的进程(在Unix中是僵尸)。您会注意到它的父进程ID为1,并且直到操作系统重新启动后它才会消失。
因此,用于分叉的传统伪代码如下所示:
if ($pid = fork()) {
# pid is non-zero, this is the parent
waitpid($pid) # tell OS that we care about the child
do other parental stuff
}
else {
# pid is 0 so this is the child process
do_childish_things()
}
您的代码无法做到这一点,因此您可能会遇到僵尸,然后沮丧地发现您无法摆脱它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句