This being Linux, of course the defaults are relatively basic, but there are many competing choices of tools to change those. But not all of them are available on every distro, so it is not obvious which one to use.
On cgroups systems, CPU resource supports a weighted distribution model, where each group is assigned a fraction of available resource in a ratio according to weight.
On systemd systems, the slice concept directly corresponds to using cgroups. Read man systemd.resource-control
and consider enabling DefaultCPUAccounting=
. Apply the same CPU ratio to each unit in question. For example, to do this to each interactive user, put this in something like /etc/systemd/system/user-.slice.d/resource.conf
[Unit]
Description:Default CPU ratio
Documentation:man:systemd.resource-control
[Slice]
CPUWeight=100
The number's precise value is not important, as long as each unit in question gets something of the correct proportion. Further, its not that they are dividing up total system CPU, but the CPU assigned to the parent user.slice. If tweaking a service unit instead, those are out of system.slice.
On systems using pam_limit, POSIX resource limits can be set on logged in users. See man limits.conf
, and the config file might be a drop-in like /etc/security/limits.d/resource.conf
. A practical thing to might be to set nproc
to the maximum number of tasks a user gets. Exceeding that, things fail to fork. Or, cpu
is a tempting limit, however exceeding the hard limit might start killing processes, which is not a great user experience.
And for the task version of the children's game red light green light, there is cpulimit. This sends SIGSTOP and SIGCONT signals to manage CPU use, simple. Notably it is a bit ad-hoc in that there is no config file, it needs to be invoked to start a process, or be given a PID.