timeout for fread
<p>I am calling TCL script from PHP. I am sending a unique string from TCL process  to PHP to make sure that script has ended . </p>

<p>If I don't send that string then my fread in PHP is blocked forever . </p>

<p>// PHP code </p>

<pre><code>&lt;?php

$id = 'done'; //Unique string 

$app = 'c:/wamp/www/tcl/bin/tclsh84.exe';
$descriptorspec = array(
  0 =&gt; array("pipe","r"),
  1 =&gt; array("pipe","w"),
  2 =&gt; array("pipe","w")
) ;
$process = proc_open($app, $descriptorspec, $pipes);
if (is_resource($process)) 
{

  for($i=0;$i&lt;2;$i++) 
  {
    $output = '';
    $continue = true;
    $cTimeout = 0;
    echo 'loop ', $i, "\n";
    fwrite($pipes[0], "source c:/wamp/www/tcl/bin/helloworld.tcl\n");
    echo "waiting for idle\n";
    $timeout = time();  
    do {
      $read=array($pipes[1]);
      $write=array();
      $except=array($pipes[1]);
      $ready = stream_select($read, $write, $except, 1, 0);
      $dif = time()- $timeout;
      if ( $ready &amp;&amp; $read ) 
      {
        $output .= fread($pipes[1], 2048);  // is blocked indefinitely 
        // if the delimiter id shows up in $output
        if ( false!==strpos($output, $id) ) {
            // the script is done
          $continue = false;
        }
      }
      if($dif &gt; 5)  //timeout value not working
      {
      $continue = false;
      }

    } while($continue);
    echo 'loop ', $i, "$output finished\n";
  }
  proc_close($process);
}
?&gt;
</code></pre>

<p>//TCL code</p>

<p>puts "hello"</p>

<p>If i sends "done" from TCL, then my PHP script ends . 
But I don't want to send just done, instead I need to do with the help of a timeout . 
i.e I want to wait for a certain period of time for the unique string , else I should exit . But I can't seem to implement the timeout in this case. </p>

<p>Please can anyone guide me .</p>
