How to limit the number of processes?

In my PHP web application, I am trying to limit the number of CPU / memory intensive processes that are running (e.g. ImageMagick 'convert' command). I have several krn jobs that execute various scripts that could potentially run too many instances of these processes with intensive processor / memory.

In an attempt to limit such processes, I first check to see if a certain number of processes are running on my system. 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 control the resources of the OS (using the command "from above"), I see a lot of processes than I expect to work. Any ideas why?

+4
source share
2 answers

I think that an approach to the approach would be a task manager or task ... However, if this is not possible for you because your material is so fragmented, your approach does not look so bad. but what can I say from here, your ps command should always just return the "php" maby to better check the executable or user or something else?

What LOT other processes are you talking about? php processes? how do they look, perform? you can also check this link http://www.php.net/manual/en/function.sem-get.php

+2
source

For this usecase php has the extension Semaphore

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 can fully satisfy your needs, but the semaphore is not a daily extension, so I could set that you need to install it using the --enable-sysvsem in php

0
source

All Articles