note 10337 modified in usleep by ronabop

From: Date: Wed, 03 Jan 2001 07:36:40 +0000
Subject: note 10337 modified in usleep by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-9315@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); } } 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

« previous php.notes (#9315) next »