0

As we know, cgroups can limit cpu usage of processes. Here is an example:

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND          
30142 root      20   0  104m 2520 1024 R 99.7  0.1  14:38.97 sh

I have a process, whose pid is 30142. I can limit it as below:

mkdir -p /sys/fs/cgroup/memory/foo
echo 1048576 >  /cgroup/memory/foo/memory.limit_in_bytes
echo 30142 > /cgroup/memory/foo/tasks

As we see, if I want to limit a process, I have to first execute it and then I could limit it according to its pid. Is it possible to limit a process according to its name? Is it possible to limit a process before executing it?

0

Control groups are pid-based, and there is no direct way of limiting processes by name. (Since control groups are hierarchical, this makes sense: a group also contains its member processes’ future children, by default, and having them re-attach to another group based on their name would be surprising.)

The typical way to use control groups is to attach a parent process to them, and then rely on the fact that children inherit their parent’s group. However there is a tool which will allow you to start a process in a given group, cgexec:

cgexec -g memory:foo yourcommand

On Debian you’ll find this in cgroup-tools.

  • Actually, the bigger reason that the PID is used is that process names are not guaranteed unique for the life of the process (the process can change it, and other processes can have the same name). This is the same reason that /proc is PID-based, and that the kernel uses PID's internally for tracking individual processes. – Austin Hemmelgarn Sep 19 '18 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.