ncvncvn
ncvncvn

Reputation: 5903

possible to force, a script to use certain amount of CPU and memory?

is it possible to force a ruby script to use up to certain amount of CPU and memory.

i dont want the script to be killed when it exceeds this specified amount. i just want it to run within the given constraints.

EDIT:

yes its an endless recursive loop that seems to use lot of CPU.

i noticed that doing return at the end of each recursion is causing this. after i remove it, this high cpu usage is gone. what else can i use to terminate the loop ? exit ?

Upvotes: 0

Views: 493

Answers (4)

Kieron
Kieron

Reputation: 11834

You should almost never need to do this. Why would you waste that time doing nothing? There's nothing wrong with the CPU being at high utilisation; it doesn't need to rest. If there are multiple processes running then the operating system deals with dividing the CPU time between them.

Upvotes: 0

stimms
stimms

Reputation: 44094

sleep will sleep the current thread for some period of time. Your cpu load goes down because your programme isn't doing anything for that time. The kernel should handle ensuring that your CPU has sufficient time for all the programmes running.

Upvotes: 1

Terry Felkrow
Terry Felkrow

Reputation: 653

It really depends what you do in your script. If it's some sort of a endless loop, you are just "hibernating" your script, and allow less processing time to be spent on it.

In short, "sleeping" is not a particularly clean or proper solution. It would help if you posted details on exactly what your script does, typically there would a much more sensible solution available.

Upvotes: 0

ckknight
ckknight

Reputation: 6173

Yes, calling a sleep function in most programming systems (ruby included) will cause the program to wait for that amount of time, using little to no CPU power.

Alternatively, you could run your program at a lower priority (in *nix systems, this is done with nice or renice).

Upvotes: 2

Related Questions