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

Hi!

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.
<?php
$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";
flush();
fwrite($pipes[0], "cat\n");
#
sleep(1);

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

# ********* I don't want to fclose stdin here
fclose($pipes[0]);

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

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

# ********* I want to fclose stdin here, but reading from stdin
above will hang then
#
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
// 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.