note 10337 modified in usleep by ronabop
| From: | ronabop@php.net | Date: | Wed, 03 Jan 2001 07:37:46 +0000 |
| Subject: | note 10337 modified in usleep by ronabop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-9316@lists.php.net to get a copy of this message | ||
To monitor a scripts CPU ussage and avoid any nasty CPU gobbling loops you can use this function
(will not work with windows or safe mode) I know it works on FreeBSD:
function phpmon($max)
{
$cmd =
ps -Unobody -r -o%cpu;
$lines = explode("\n", $cmd);
$usage = substr($lines[1], 0, strpos($lines[1], "."));
$sleeprate = 500;
while ($usage >= $max)
{
$cmd = ps -Unobody -r -o%cpu;
$lines = explode("\n", $cmd);
$usage = substr($lines[1], 0, strpos($lines[1], "."));
usleep($sleeprate);
}
}
phpmon($MAX);
where $MAX is the maximum CPU you want the process to consume. e-mail me with any
improvements/suggestions.
I have noticed that this consumes a lot of system CPU (at least in my limited testing) possibly from
all of the system calls or the huge mathematical functions I used to test the effectiveness of the
script.
http://www.php.net/manual/function.usleep.php