2012-05-25 31 views
2

我說的是一個過程,需要使用以下(PHP 5.3/Ubuntu的12.04),用戶交互使用時,始終擋在距離管道通過PHP的proc_open打開閱讀與stream_select

$pdes = array(
    0 => array('pipe', 'r'), //child's stdin 
    1 => array('pipe', 'w'), //child's stdout 
); 
$process = proc_open($cmd, $pdes, $pipes); 
sleep(1); 
if(is_resource($process)){ 
    while($iter-->0){ 
    $r=array($pipes[1]); 
    $w=array($pipes[0]); 
    $e=array(); 
    if(0<($streams=stream_select($r,$w,$e,2))){ 
     if($streams){ 
     if($r){ 
      echo "reading\n"; 
      $rbuf.=fread($pipes[1],$rlen); //reading rlen bytes from pipe 
     }else{ 
      echo "writing\n"; 
      fwrite($pipes[0],$wbuf."\n"); //writing to pipe 
      fflush($pipes[0]); 
    }}}} 
    fclose($pipes[0]); 
    fclose($pipes[1]); 
    echo "exitcode: ".proc_close($process)."\n"; 
} 

這是我的測試後stream_select即使$pipes[1]設爲非阻斷的作爲寫$pipes[0]不會阻塞程序C,

#include <stdio.h> 
int main(){ 
    char buf[512]; 
    printf("before input\n"); 
    scanf("%s",buf); 
    printf("after input\n"); 
    return 0; 
} 

現在的問題是$r總是空的。然而,事情沒有stream_select也就是說,如果我符合讀取和寫入測試程序做工精細,

echo fread($pipes[1],$rlen); //matching printf before input 
fwrite($pipes[0],$wbuf."\n"); //matching scanf 
fflush($pipes[0]); 
echo fread($pipes[1],$rlen); //matching printf after input 

我無法弄清楚這裏發生了什麼。我試圖在這裏實現某種基於Web的終端模擬器。如何做到這一點的任何建議,歡迎:)

回答

0

對不起,浪費你的時間的傢伙。我在一段時間後發現了這個問題(對於後期更新感到抱歉)。由於競賽狀況,閱讀被阻止。

我寫入流程並立即檢查流的可用性。寫入永遠不會被阻塞,數據還沒有準備好讀取(不知何故,即使1個字節的數據可用,它也需要600ms)。所以,我能夠通過在寫入塊末尾添加sleep(1)來解決問題。

+0

難道你不能重複選擇超時? –