Introduction to cgroups in Linux

In Linux, cgroups (control groups) is a kernel feature that limits, accounts for, and isolates the CPU, memory, disk I/O, and network usage of one or more processes. This tutorial will guide you through the process of using cgroups to throttle resources in Linux. Let’s dive in!

Installing cgroups

Before we start, ensure that cgroups is installed on your Linux system. If not, you can install it using the following command:

sudo apt-get install cgroup-tools

Creating a cgroup

Once cgroups is installed, the next step is to create a cgroup. Here’s how:

sudo cgcreate -g cpu:/mycgroup

This command creates a new cgroup named “mycgroup” under the “cpu” subsystem.

Setting Resource Limits

Now that we have a cgroup, we can set resource limits. For example, to limit CPU usage to 20% for all processes in the “mycgroup” cgroup, we can use the following command:

echo 200000 > /sys/fs/cgroup/cpu/mycgroup/cpu.cfs_quota_us

The value is in microseconds, so 200000 microseconds is 20% of one CPU.

Adding Processes to the cgroup

To add a process to the cgroup, we need to know the PID (Process ID) of the process. We can then use the following command to add the process to our cgroup:

echo PID > /sys/fs/cgroup/cpu/mycgroup/tasks

Verifying the cgroup Configuration

To verify that the process has been added to the cgroup and the CPU limit is in effect, you can use the following command:

cat /sys/fs/cgroup/cpu/mycgroup/cpu.stat

This will display the CPU statistics for the cgroup, including the usage in user and system mode.


That’s it! You now know how to throttle resources using cgroups in Linux. Remember, cgroups is a powerful tool, but with great power comes great responsibility. Use it wisely to ensure optimal system performance. For more information, check out the official cgroups documentation.