Connecting Tech Pros Worldwide Help | Site Map

proc_open and blocking fread / tri-directional pipes

Christian Hammers
Guest
 
Posts: n/a
#1: Jul 17 '05
Hello

I would like to call a unix shellscript from within a PHP script and
- write data to its STDIN
- read data from its STDOUT *and* STDERR
- get its exit code afterwards

proc_open seems to be the right thing to use but I have the problem that the called program gives >8kb data on both stdout/stderr back which causes my PHP script to simply hang in the fread call. To be precise the first 4096 "O" characters are read and displayed.

The problem is described in the first user comment to proc_open at www.php.net but the solution there writes stderr to a file (which never blocks) and therefore does not work for me.

Does anybody know a solution? stream_set_blocking sounds good but
simply calling it with $pipes[1] and $pipes[2] as "stream" argument does not have the wished effect.

bye & TIA,

-christian-

My test program:

$pipe_spec = array(
0 => array("pipe", "r"), # stdin
1 => array("pipe", "w"), # stdout
2 => array("pipe", "w") # stderr
);
$process = proc_open("/tmp/testshellskript", $pipe_spec, $pipes);

# stdin
if (fclose($pipes[0]) == 0) trigger_error("fclose0", E_USER_ERROR);

# stdout
while ( ! feof($pipes[1])) print fread($pipes[1], 8192);
if (fclose($pipes[1]) == 0) trigger_error("fclose1", E_USER_ERROR);

# stderr
if (fclose($pipes[2]) == 0) trigger_error("fclose2", E_USER_ERROR);

And the called shell script (writes the chars each 7000 times):

#!/usr/bin/perl
print(STDOUT ("O"x7000)."\n");
print(STDERR ("E"x7000)."\n");
Closed Thread