469,604 Members | 2,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,604 developers. It's quick & easy.

need to close stdin pipe from proc_open() before reading stdout pipe??


I want to read/write commands and program input to/from /bin/bash
several times before I close the stdin pipe. However, reading
from cat hangs unless I first close the stdin pipe.
$descriptorspec = array(
0 => array("pipe", "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
$process = proc_open("/bin/bash", $descriptorspec, $pipes);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// 2 => readable handle connected to child stderr

echo rand()," cat...<br>\n";
fwrite($pipes[0], "cat\n");

echo rand()," hallo...<br>\n";
fwrite($pipes[0], "hallo<br>\n");
fflush($pipes[0]); # fflush() doesn't help

# ********* I don't want to fclose stdin here

echo rand()," reading stdout...<br>\n";
while(!feof($pipes[1])) {
echo fgets($pipes[1], 1024);

echo rand()," reading stderr...<br>\n";
while(!feof($pipes[2])) {
echo fgets($pipes[2], 1024);

# ********* I want to fclose stdin here, but reading from stdin
above will hang then
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);

echo "command returned $return_value\n";

Webspace; Low end Serverhousing ab 15 e, etc.: http://www.bksys.at
Linux Admin/Programmierer: http://bksys.at/bernhard/services.html

Jul 17 '05 #1
0 5436

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by lickspittle | last post: by
3 posts views Thread by Harayasu | last post: by
11 posts views Thread by Darklight | last post: by
6 posts views Thread by Christian Convey | last post: by
5 posts views Thread by Cameron Laird | last post: by
8 posts views Thread by rdabane | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.