2

I'm using Windows 7 and php 5.

I have a problem with executing a process by proc_open and check the timeout. I used stream_select to check the timeout with this code:

<?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);
?>

This is the C++ code I used to test that:

#include<windows.h>

int main(){
    Sleep(2000);
    return 0;
}

In that code, there's no output at all, but stream_select doesn't wait 1 sec and return 1.

How can i fix this?

박수찬
  • 21
  • 1
  • What do `$ret` and `$streams` contain after `stream_select` returns? Please provide a `var_dump`. If `$ret` > 0 then then `$streams` should not be empty. If `$streams` is not empty, what do you get when you read from the streams contained within it? – Leigh Jul 04 '12 at 15:08
  • Also are you sure `$pipes[1]` is correct? You are specifying only 2 pipes, maybe `$pipes[0]` is what you need. – Leigh Jul 04 '12 at 15:13
  • $ret == 1 and $streams is not empty. I can read nothing. – 박수찬 Jul 04 '12 at 15:16
  • I'm sure that $pipes[1] is correct, I confirmed that is right. – 박수찬 Jul 04 '12 at 15:17
  • Use `stream_set_blocking($pipes[1], 0)`. Although how that would affect already the select is beyond me. – Jirka Hanika Jul 04 '12 at 15:39
  • it returns FALSE, and nothing changed. – 박수찬 Jul 04 '12 at 15:48

1 Answers1

0

I suspect that your C++ executable might not be really executed for some reason, but that the exit code could be hidden from you by cmd.exe.

  • Get a proof that the C++ executable actually runs at all. Attempt to pass some data from C++ to PHP already before calling stream_select.

  • Get cmd.exe out of your way:

    $opts = array('suppress_errors' => false, 'bypass_shell' => true);

    $prog = proc_open ($objname.".exe", $descriptorspec, $pipes, "$DOCUMENT_ROOT/judge/temp", null, null, $opts);

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75