By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

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

P: n/a

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.:
Linux Admin/Programmierer:

Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.