php script only being server to two clients at a time
<p>I wrote the following script as a test and it appears it's only being server to two clients at a time:</p>

<pre><code>&lt;?php
$time = time();
$timediff = 0;

do
{
    $timediff = time() - $time;
    usleep(1);
}while($timediff &lt;= 1);

echo date('G:i:s', $time)." - ".date('G:i:s', time()); 
?&gt;
</code></pre>

<p>Result for a LAMP server:</p>

<p><img src="http://i.stack.imgur.com/wwy7e.png" alt="lamp_results"></p>

<p>Each thread seems to be correctly <code>started</code> at the same time, confirmed by the <code>result</code> (what php echo'd back) and every 2 new threads to indeed take an extra 2 seconds (<code>time</code>) to finish.</p>

<p>Each script seems to be taking 2 seconds to finish, after which the following 2 are served. This is just stupid, because now I have a quad core 3.2 GHz server with 12GB RAM queueing people for no good reason. It may be worth to mention that according to <code>htop</code> (a system monitor) only two CPU cores are being used by apache2.</p>

<p>I tried the script again on a debian-squeeze server running lighttpd and php5-fpm, which is a completely different setup, but it gave the same results. I'm not sure if it's bad coding or something else.. </p>

<p>I already tried commenting out the <code>usleep()</code> but that nets the exact same results, except for the fact that <code>htop</code> then shows CPU usage at 100% on all (both) cores. With usleep on <code>1</code> it shows a usage of 15% on each core, and with usleep on <code>500</code> it's barely reaching 6% each, confirming that the server should be able to serve way more pages than 2 at a time.</p>

<p>Can anyone tell me why this happens?</p>
