我正在尝试制作一个将在 Windows 中运行一组其他程序的 Perl 脚本。我需要能够捕获进程的标准输出、标准错误和退出代码,并且我需要能够查看进程是否超过了分配的执行时间。现在,我的代码的相关部分如下所示:
...
$pid = open3($wtr, $stdout, $stderr, $command);
if($time < 0){
waitpid($pid, 0);
$return = $? >> 8;
$death_sig = $? & 127;
$core_dump = $? & 128;
}
else{
# Do timeout stuff, currently not working as planned
print "pid: $pid\n";
my $elapsed = 0;
#THIS LOOP ONLY TERMINATES WHEN $time > $elapsed ...?
while(kill 0, $pid and $time > $elapsed){
Time::HiRes::usleep(1000); # sleep for milliseconds
$elapsed += 1;
$return = $? >> 8;
$death_sig = $? & 127;
$core_dump = $? & 128;
}
if($elapsed >= $time){
$status = "FAIL";
print $log "TIME LIMIT EXCEEDED\n";
}
}
#these lines are needed to grab the stdout and stderr in arrays so
# I may reuse them in multiple logs
if(fileno $stdout){
@stdout = <$stdout>;
}
if(fileno $stderr){
@stderr = <$stderr>;
}
...
$time = -1
如果(不需要超时)一切正常,但系统认为它kill 0, $pid
始终为 1。这使得我的循环在允许的整个时间内运行。
一些额外的细节只是为了清楚起见:
- 这是在 Windows 上运行的。
- 我知道我的进程确实终止了,因为我已经获得了所有预期的输出。
- Perl 版本:这是为 MSWin32-x86-multi-thread 构建的 perl,v5.10.1(带有 2 个注册补丁,有关更多详细信息,请参见 perl -V)版权所有 1987-2009,Larry Wall Binary build 1007 [291969] 由 ActiveState http提供://www.ActiveState.com 建于 2010 年 1 月 26 日 23:15:11
- 感谢您的帮助:D
对于可能有类似问题的未来人, 我让代码工作,这里是修改后的代码部分:
$pid = open3($wtr, $stdout, $stderr, $command);
close($wtr);
if($time < 0){
waitpid($pid, 0);
}
else{
print "pid: $pid\n";
my $elapsed = 0;
while(waitpid($pid, WNOHANG) <= 0 and $time > $elapsed){
Time::HiRes::usleep(1000); # sleep for milliseconds
$elapsed += 1;
}
if($elapsed >= $time){
$status = "FAIL";
print $log "TIME LIMIT EXCEEDED\n";
}
}
$return = $? >> 8;
$death_sig = $? & 127;
$core_dump = $? & 128;
if(fileno $stdout){
@stdout = <$stdout>;
}
if(fileno $stderr){
@stderr = <$stderr>;
}
close($stdout);
close($stderr);