What does cgroups provider over ulimit

cgroupsulimit

Why were cgroups created when the setrlimit and getrlimit system calls already existed?

I thought it might be that rlimit only applies to a single process, but the man page states:

Limits on the consumption of system resources by the current process and
each process it creates may be obtained with the getrlimit() call, and
set with the setrlimit() call.

It seems to me that if we wanted to control the resource usage of a group of processes, we could just set the limits in a parent process (possibly a shell) and those limits would be enforced in all child processes.

Clearly I'm missing some crucial difference between the two mechanisms, but I couldn't find the answer I was looking for.

Best Answer

This specific wording seems to be mostly used in the *BSD version of setrlimit.

Other versions of setrlimit (2) state

NOTES

A child process created via fork(2) inherits its parent's resource limits. Resource limits are preserved across execve(2).

Resource limits are per-process attributes that are shared by all of the threads in a process.

I think this more clearly shows, that a limit of 2 GiB main memory applies to a single process (and its threads). And a child process of this process inherits also a limit of 2 GiB main memory, but this is 2 GiB for its own usage.

In other words, each process would have a limit of 2 GiB, and together they could consume up to 4 GiB of main memory.


On the other side, the man page for cgroups - Linux control groups says

Various subsystems have been implemented, making it possible to do things such as limiting the amount of CPU time and memory available to a cgroup, accounting for the CPU time used by a cgroup, and freezing and resuming execution of the processes in a cgroup.

So, control groups allow to limit resources over a group of processes.

Limiting the main memory to 2 GiB for a group containing 3 processes, means the main memory used by all 3 processes together may not exceed 2 GiB.

Related Question