Reputation: 40653
In my PHP web application, I am trying to limit the number of CPU/memory intensive processes that run (for example, ImageMagick's 'convert' command). I have a number of crons jobs that execute various scripts that could potentially execute too many instances of these CPU/memory intensive processes.
In my attempt to limit such processes, I first check to see if my system is already running a certain number of processes. The function:
function has_reached_process_limit($process, $limit)
{
$command = 'ps -eo comm | grep ' . $process;
exec($command, $output, $return);
if (count($output) > $limit)
{
return TRUE;
}
else
{
return FALSE;
}
}
So, I run something like this:
while (has_reached_process_limit('convert', 5) === TRUE)
{
// loop over
}
The problem is that when I monitor my OS' resources (via the 'top' command), I see a LOT more processes than what I expect to be running. Any ideas why?
Upvotes: 4
Views: 2569
Reputation: 29874
I think abetter approach would be a job controller or a jobqueue... However if thats not possible for you because your stuff is so seperated your approach deoesnt look so bad. but what i can tell from here your ps command should always just return "php" maby its better to check for the executed file or the user or something?
what LOT other processes are you talking about? php processes? what do they look like, execute? you can also check out this link http://www.php.net/manual/en/function.sem-get.php
Upvotes: 2
Reputation: 435
For this usecase php has the Semaphore Extension
resource sem_get ( int $key [, int $max_acquire = 1 [, int $perm = 0666 [, int $auto_release = 1 ]]] )
http://www.php.net/manual/en/function.sem-get.php
i think this could fullfill your needs, but semaphore is not a everyday extension so i could be that you need to install this via --enable-sysvsem
switch in php
Upvotes: 0