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
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.