Groups of processes, not single processes
A limit on one process is easy to get around: the process forks a child, and the child has its own budget. Control groups solve this by attaching limits to a group. Every process belongs to exactly one cgroup in each hierarchy, and the kernel adds up the usage of the members. A service that starts twenty workers is limited as one unit, not twenty separate times.
Cgroups have two jobs. They account, which means measuring CPU, memory, and I/O for the group, and they control, which means enforcing limits or weights. Monitoring tools read the accounting files, and container runtimes write the control files.
The v2 unified hierarchy
The first version of cgroups had a separate tree for each controller, so a process could sit in one group for memory and another for CPU. Version 2 uses a single unified hierarchy for all controllers. The filesystem is mounted with the type cgroup2, usually at /sys/fs/cgroup. The kernel documentation describes the rules, and the most important one is the no internal processes rule: a group that has child groups does not hold processes itself, except at the root.
You can tell which version a system uses by reading the membership file of your own process. Under v2 there is exactly one line, and it begins with 0:: followed by a path.
cat /proc/self/cgroupThe path after the last colon is the group your shell belongs to, relative to the root of the hierarchy. A v1 system would list several lines, one per controller, each with a name in the middle field.
Control files
Every cgroup directory contains files that you read or write. A few are common to all groups: cgroup.procs lists the PIDs in the group, and writing a PID there moves that process into the group. cgroup.controllers lists the controllers available at that level, and cgroup.subtree_control chooses which of them are passed to child groups.
The controller files have names with a prefix. memory.max sets the hard memory limit, and memory.current shows the usage. cpu.max sets a quota per period, and cpu.stat reports the time used. pids.max limits the number of processes the group can create, which is a simple defense against fork bombs.
ls /sys/fs/cgroupOn a system with cgroup v2 mounted, the listing includes the control files described above. The lab kernel may mount the hierarchy in a different place or with fewer controllers, so the lab only asks you to read what is there.
Limits are enforced by the kernel
When a group reaches its memory limit, the kernel first tries to reclaim memory from that group. If it cannot, the OOM killer chooses a process in the group and kills it. For CPU, a quota means the group is paused for the rest of the period once it uses its share, so a busy process cannot starve others on the same machine. These rules work for any program, including one that was never written with limits in mind.
A memory limit that is too low does not fail politely. The process is killed when it exceeds the limit and the reclaim fails, so set limits with measurements from memory.current, not guesses.
Who can change them
Creating a cgroup or writing a limit normally needs root, or a delegated subtree where the owner has been given write access to the directory. Systemd uses delegation to let user services manage their own groups. The lab user is unprivileged, so the exercises only read the membership and the root listing, and that is enough to see how the tree is laid out.
cgroups and namespaces together
A container uses both mechanisms. A PID namespace hides the other processes, a cgroup limits the container to a memory budget, and both are attached to the same set of processes. The cgroup namespace adds one more trick: it makes the container see its own group as the root, so the paths in /proc/self/cgroup do not reveal the host layout. Reading the membership file before and after entering a container shows that change.
For the full reference, read cgroups(7) on man7.org and the cgroup v2 document in the kernel documentation.
Accounting files you can read
Even without permission to write, a process can read the accounting files of its own group. cpu.stat reports usage_usec, the total CPU time the group has used in microseconds, along with counters that show how often the quota paused the group. memory.current holds the current usage in bytes, and memory.stat breaks that usage down into anonymous memory, file cache, and kernel memory. Monitoring tools that show these numbers in a table are reading exactly these files.
Accounting values only grow while the group runs, so a rate needs the difference between two readings over an interval, just as with the counters in /proc. A single reading is a snapshot, and a snapshot is useful for limits but not for speed.
Hierarchy and inheritance
A child group does not inherit a limit automatically, but the kernel checks every ancestor. A process in a leaf group is limited by its own memory.max and by the limit of each parent on the path. The tightest limit wins, which lets an administrator put one budget on a whole service while each worker group gets a smaller slice of it.
Limits and the reading habit
A good habit is to read a limit before you change it. Read memory.max, cpu.max, and pids.max for the group, then read the usage files for the same group, and only then decide whether the limit is too low. A usage number close to the limit means the group is being throttled or killed, and a usage number far below it means the limit is not the problem. The same two-step reading works on a laptop and on a production host.