tags:

views:

368

answers:

5

I'm running on a shared *NIX server (run by Site5). I have a php script that runs in the background occasionally doing some offline calculations. It uses around 100% CPU while it runs. I've tried nice-ing it, like this:

nice -n 19 php script.php

but that doesn't seem to make any difference.

Can anyone help?

Thanks,

Ben

+4  A: 

Even niced, it'll use 100% CPU if available. However, the kernel will give priority to any other (non-niced) processes that come along.

Brian Knoblauch
+6  A: 

You could scatter usleep ( int $micro_seconds ) through your code. This will force your script to stop for tiny amounts of time leaving the CPU free for other things.

Will that be necessary though? If you script has a low priority, does it matter that it's using 100% of the CPU... If other processes with a higher priority needed the CPU wouldn't they get the time they needed and your script getting the rest (up to 100%)?

Stacey Richards
Ah. Now I realise that all that was happening here was that I was forgetting exactly what nice does. Thanks for the useful reminder! :-)
Ben
+4  A: 

As long as the system is responsive, and you are able to get other work done while it's running, I wouldn't worry about it. I run a distributed computing client on my systems, and it soaks up any available CPU cycles. Since it runs with the lowest priority, any and all other processes will preempt it as needed.

Nighthawk
+1  A: 

As your process is background, and uses 100% CPU it seem that the process is cpu bound. It is background so user bound would not be expected, so the only alternative would be IO bound. If your process should not really do interesting IO, the script itself would be expected to be CPU bound, and not just buggy.

Processes will always try to go as fast as possible. If they are IO bound, they will use 100% IO, if they are CPU bound, they will try to use 100% CPU. Properly written process schedulers automatically aim to provide a sense of fairness to all processes, meaning that the bigger processes get lower priority. You can further lower the priority with nice. The fact that the cpu usage is still about 100% means that there are no other processes which are currently CPU bound, but are most likely waiting for input from the network.

Paul de Vrieze
+1  A: 

Nice changes how often your php results in 100% CPU, if there are considerable processes to context-switch, not how much.

The moment a program executes a system call, the kernel can yield the CPU to another process before doing the real job. Your php doesn't seem to make any system call when it's running full speed. That is, you're not making any blocking I/Os (socket, file, etc.) See if any code block is stuck in busy-waiting.

yogman