title: always blocked on reading from pipe opened through php's proc_open when used with stream_select
body: <p>I'm talking to a process requiring user interaction using the following (PHP 5.3/ Ubuntu 12.04),</p>
  
  <pre><code>$pdes = array(
    0 =&gt; array('pipe', 'r'), //child's stdin
    1 =&gt; array('pipe', 'w'), //child's stdout
  );
  $process = proc_open($cmd, $pdes, $pipes);
  sleep(1);
  if(is_resource($process)){
    while($iter--&gt;0){
      $r=array($pipes[1]);
      $w=array($pipes[0]);
      $e=array();
      if(0&lt;($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";
  }
  </code></pre>
  
  <p>And this is my test program in C,</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(){
    char buf[512];
    printf("before input\n");
    scanf("%s",buf);
    printf("after input\n");
    return 0;
  }
  </code></pre>
  
  <p>Now, the problem is <code>$r</code> is always empty after <code>stream_select</code> even if <code>$pipes[1]</code> is set to non-blocking where as write to <code>$pipes[0]</code> never blocks. However, things work fine without <code>stream_select</code> i.e. if I match reads and writes to the test program,</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I couldn't figure out what's happening here. I'm trying to achieve something sort of web based terminal emulator here. Any suggestions on how to do this are welcome :)</p>