It happened to me multiple times now that a program which entered an infinite loop made the entire machine completely unresponsive, one time even foreign JavaScript, and I had to power off. This is unacceptable so I want to limit the CPU usage of processes. Using limits.conf
seems obvious, but it says that the unit is max CPU time (MIN)
. But as far as I know, CPU time is the time the CPU spends doing things, and in this case the complete timeframe is unknown. A limit of 1 CPU minute would mean what? (On a sidenote, ulimit
claims that the unit is seconds, for even more confusion...) So, how can I set stuff like "Don't ever allow any process to exceed to use more than half of the CPU at any given point in time"
Asked
Active
Viewed 2,246 times
1

ctrl-alt-delor
- 27,993
1 Answers
1
You can not do with ulimit
, there is a tool that can do it, by suspending and un-suspending the process (cpulimit
). It may be part of your OS, it is part of Debian, but not installed by default. There is also kernel namespaces. However you should not need to do this. Unix does not behave badly in this situation. You can also use nice however I have rarely seen a situation when it was needed. Your problem may have another cause, such as swapping.

ctrl-alt-delor
- 27,993
nice
it to an acceptable level, but aside from that any process simply consumes a slice of time based on its scheduling priority. If you're determined that you need to limit CPU consumption, there is a tool called cpulimit available on github. – Fubar May 26 '20 at 17:08for(;;){}
can make my computer so unresponsive that I have to cut the power, I think there is a reason to limit CPU. @ctrl-alt-delor I don't know because at that point opening a Linux tty took 2 minutes, and I didn't bother to wait for bash starting + htop starting. – May 28 '20 at 08:04nice
on the process running the loop would keep it from being so disruptive, as normal processing would be able to take precedence for CPU timeslices. You could also try tuning the length of a timeslice to have them available more frequently. – Fubar May 28 '20 at 12:47