我使用的是 Windows 7 和 php 5。
我在通过 proc_open 执行进程并检查超时时遇到问题。 我使用 stream_select 通过以下代码检查超时:
<?php
$descriptorspec = array(
0 => array("file", $infile, "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$prog = @proc_open ($objname.".exe", $descriptorspec, $pipes, "$DOCUMENT_ROOT/judge/temp", null, null);
if(!is_resource($prog)) ErrorResult("","proc_open err");
$streams = array($pipes[1]);
$start = microtime(true);
$ret = stream_select($streams, $a = null,$a = null, 1);
$stop = microtime(true);
?>
这是我用来测试的 C++ 代码:
#include<windows.h>
int main(){
Sleep(2000);
return 0;
}
在该代码中,根本没有输出,但 stream_select 不会等待 1 秒并返回 1。
我该如何解决这个问题?
最佳答案
我怀疑您的 C++ 可执行文件可能由于某种原因并没有真正执行,但是 cmd.exe
可能对您隐藏了退出代码。
获得 C++ 可执行文件实际运行的证明。在调用
stream_select
之前尝试将一些数据从 C++ 传递到 PHP。让
cmd.exe
别挡路:$opts = array('suppress_errors' => false, 'bypass_shell' => true);
$prog = proc_open ($objname.".exe", $descriptorspec, $pipes, "$DOCUMENT_ROOT/judge/temp", null, null, $opts);
关于PHP stream_select 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329533/