Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: min(zero, four)?
(Example: nine)

The Note You're Voting On

betchern0t
18 years ago
Care needs to be taken in the case of long running child processes. Say you want to run tail -f /var/log/messages or in my case burn dvds. If you have a busy wait, Apache2 can sit towards 100%cpu and steadily grow memory. In my case I crashed the server after about an hour and 90% of the dvd burned. During that time apache had consumed a gig of swap.

Offending code - don't copy:

<?php
$ThisCommand
= sprintf("%s %s",COMMAND,$ThisFile);
$fp=popen($ThisCommand,"r");
while (!
feof($fp)) {
set_time_limit (20);
$results = fgets($fp, 4096);
if (
strlen($results) == 0) {
// stop the browser timing out
echo " ";
flush();
} else {
$tok = strtok($results, "\n");
while (
$tok !== false) {
echo
htmlentities(sprintf("%s\n",$tok))."<br/>";
flush();
$tok = strtok("\n");
}
}
}
pclose($fp);
?>

to go from zero memory and 100% cpu to negligible memory and negligible cpu add a sleep.

<?php
while (!feof($fp)) {
set_time_limit (20);
$results = fgets($fp, 256);
if (
strlen($results) == 0) {
// stop the browser timing out
echo " ";
flush();
} else {
$tok = strtok($results, "\n");
while (
$tok !== false) {
echo
htmlentities(sprintf("%s\n",$tok))."<br/>";
flush();
$tok = strtok("\n");
}
}
// avoid a busy wait
sleep(1);

}
?>

I think the continued banging of the space to keep the browser awake triggered some issues in apache.

<< Back to user notes page

To Top