2016-05-31 63 views
3

任何人都可以说,为什么下面的代码挂在fwrite($pipes[0], $data);上,但是当我将$bytesCount更改为例如1000时,它不会挂起?php - > fwrite处理管道挂起 - >为什么?

我是不是能够通过谷歌找到答案:(
谢谢。

$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); 
+0

如果你使用带有第三个参数的'fwrite',然后'fflush'这个流,重复这个几次? –

+0

@AurelBílý不,它不) –

回答

3

管道与输入和输出缓冲器。cat开始读取,并复制到所有的输出,当输出缓冲区已满,其写入被阻止。

因为没有什么是阅读cat的输入(如从未达到该行),它将无限期阻塞,阻止您fwrite

+0

是的,你是对的,谢谢。 如果我在每次写入标准输入之后从进程标准输出读取数据 - 按预期工作。 –