Skip to content
Cloudadvanced

What are cgroups?

Updated Nov 30, 20263 min readBased on official documentation
What are cgroups?

In short

cgroups, short for control groups, are a Linux kernel feature that groups processes into a tree and controls how much of the system's resources each group can use. Each process belongs to exactly one cgroup, and limits set on a group apply to every process inside it and its children.

The core idea

Linux runs many processes at once, and they all compete for CPU time, memory, disk I/O and other resources. Control groups, usually called cgroups, give the kernel a way to sort processes into named groups and apply rules to each group. The rules can cap how much a group may use, or divide resources fairly between groups.

The documentation describes cgroups as two parts. The core organizes processes into a hierarchy. Controllers, the second part, do the actual resource work. A controller usually manages one type of resource, such as CPU or memory. A few controllers serve other purposes and do not distribute resources at all.

How the hierarchy works

The groups form a tree. Every process in the system belongs to one and only one cgroup, and all threads of a process share that cgroup. When a process creates a child process, the child starts in the same cgroup as its parent. Moving a process to another cgroup does not move the processes it has already spawned.

Controllers work hierarchically too. If a controller is enabled on a group, it affects every process in that group and in all groups below it. A rule set near the root cannot be loosened by a child group. A nested group can only restrict resources further. This makes the tree behave like a set of nested budgets.

Version 2 and the single hierarchy

The page describes cgroup v2. The main change from version 1 is that v2 uses a single unified tree. In v1, each controller could have its own separate tree. In v2, you mount one filesystem of type cgroup2, and the controllers that support v2 appear at its root. The documentation advises against moving controllers between hierarchies on a running production system. It recommends deciding the layout early, right after boot.

You mount the v2 hierarchy with a command like this:

mount -t cgroup2 none /sys/fs/cgroup

On most modern distributions, systemd already does this at boot, so you often find the hierarchy in place.

Working with groups

Creating a group is as simple as making a directory inside the mounted hierarchy. Each group exposes a file named cgroup.procs. Writing a PID to that file moves the process into the group. The documentation notes that one write moves one process, and writing the PID of any thread moves the whole process.

To see which group a process is in, read /proc/PID/cgroup. For v2 the entry always has the form 0::PATH. A group can be removed with rmdir once it has no children and no live processes. A zombie process does not count as live, but it still keeps its group in the record until it is reaped.

Threads and thread mode

By default, cgroup v2 treats a whole process as one unit. Some controllers also support a thread mode, which lets the threads of one process be spread across a subtree while still sharing a common resource domain. Controllers that support this are called threaded controllers. The rest are domain controllers. This detail matters mainly when you tune a single large application, so beginners can skip it at first.

Common mistakes

A frequent mistake is assuming that a limit on a parent group applies to its children only partly. In fact, the parent's limit caps the whole subtree. Another mistake is editing controllers on a live system and expecting a smooth result. The documentation warns that controllers can linger after being unmounted, and that disabling one may require disabling others first.

Another trap is forgetting that cgroup membership is per process and per thread group. If you move one thread by hand, you may not get the effect you wanted, because the default rule keeps threads of a process together.

Where cgroups fit

cgroups are the foundation of many container tools. Container runtimes place each container in its own group so that its CPU and memory limits stay separate from other workloads on the same host. If you are new to containers, the Docker overview is a good next step to see how these ideas are used in practice.

The official documentation remains the reference for exact file names and behavior, and it is worth reading the controller sections that match your workload.

FAQ

Do I need to mount cgroup v2 myself?

Usually no. Most modern Linux distributions mount the v2 hierarchy during boot, often through systemd. You only need the mount command when you are testing or when the hierarchy is missing.

Can a process belong to more than one cgroup?

In cgroup v2 a process belongs to exactly one cgroup in the single hierarchy. Its /proc/PID/cgroup entry shows a single 0::PATH line.

Does moving a process also move its children?

No. The documentation says that migrating a process does not affect its existing descendant processes. Only new children follow the cgroup their parent belongs to at the time of creation.

Why can't I remove a cgroup directory?

A group can only be removed when it has no child groups and no live processes. A zombie process alone does not block removal, but any running process in the group does.

Sources

This page is based on the official documentation below. Read it for full detail.

cgroups — official documentation ↗

cgroups in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.