我正在编写一个Perl脚本,它将写入一些输入并将这些输入发送到外部程序.这个程序有一个小但非零的机会,我想把它计时:
my $pid = fork; if ($pid > 0){ eval{ local $SIG{ALRM} = sub { die "TIMEOUT!"}; alarm $num_secs_to_timeout; waitpid($pid,0); alarm 0; }; } elsif ($pid == 0){ exec('echo blahblah | program_of_interest'); exit(0); }
现在,在$num_secs_to_timeout之后,program_of_interest仍然存在.我试图在$SIG {ALRM}的匿名子程序中杀死它,如下所示:
local $SIG{ALRM} = sub{kill 9,$pid; die "TIMEOUT!"}
但这没有做任何事情. program_of_interest仍然存在.我如何杀死这个过程?
@R_404_323@
通过终止进程组,我能够成功杀死我的exec()ed进程,如问题
In perl,killing child and its children when child was created using open的答案所示.我修改了我的代码如下:
my $pid = fork; if ($pid > 0){ eval{ local $SIG{ALRM} = sub {kill 9,-$PID; die "TIMEOUT!"}; alarm $num_secs_to_timeout; waitpid($pid,0); alarm 0; }; } elsif ($pid == 0){ setpgrp(0,0); exec('echo blahblah | program_of_interest'); exit(0); }
超时后,program_of_interest被成功杀死.