Can anyone say, why the following code hangs on fwrite($pipes[0], $data);
, but it does not hang when I change $bytesCount
to, for example, 1000?
I was not able to find answer via google :(
Thank you.
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w")
);
$bytesCount = 1000000;
$process = proc_open('cat', $descriptorspec, $pipes);
$data = str_repeat('a', $bytesCount);
fwrite($pipes[0], $data);
fclose($pipes[0]);
$response = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($process);
Pipes are implemented with input and output buffers. cat
starts to read, and copies everything to the output. When the output buffer is full, its write is blocked.
Since nothing is reading cat
's input (as that line is never reached), it will block indefinitely, blocking your fwrite
.