Preventing massive CPU suckage

Jul 18, 2009 08:16

While trying to find a way to simulate getrlimit and setrlimit on MS Windows I realized you could do this on most Unix platforms as a way to prevent your process from sucking massive CPU cycles:

trap('XCPU'){
puts "CPU time exceeded!"
# Insert diagnostic code here
exit!
}

Process.setrlimit(Process::RLIMIT_CPU, 1) # Or whatever max value you want

while true
puts "This should exit in a few seconds..."
end

What I'm not sure of is what a good value is for the maximum CPU time. Then again, maybe limiting RLIMIT_VMEM would be better. Or perhaps both?

I've never done this in practice, btw, but it might be a good way to deal with spiking processes. Just a thought.
Previous post Next post
Up