🚧Linux Control Groups (cgroups) Labs

🚧 Under Construction!!

🎯 Objective

Understand how Linux cgroups isolate and limit process resources. These labs use native Linux tools (no Docker) to demonstrate:

  • CPU and memory limits

  • Monitoring cgroup behavior

  • Cgroups v1 and systemd-based cgroups (v2)


🧰 Prerequisites

  • Linux system (Ubuntu/Debian/CentOS)

  • Root or sudo access

  • Tools: cgroup-tools, stress

sudo apt update
sudo apt install cgroup-tools stress

Check the cgroups version:

mount | grep cgroup

This should either print cgroup1 or cgroup2:


πŸ”Ή Lab 1: CPU Limit with cgroups

Step 1: Create a CPU cgroup

You can check the current settings of this new cgroup with:

This should return this information:

Step 2: Limit CPU usage to 25%

Step 3: Run a CPU-intensive task

Step 4: Monitor with top or htop

βœ… Expected: CPU usage is throttled.


πŸ”Ή Lab 2: Memory Limit with cgroups (v1)

Step 1: Create a memory cgroup

Step 2: Set the memory limit to 100 MB

Step 3: Run a memory-hungry task

βœ… Expected: Process is killed once it exceeds memory limit.


πŸ”Ή Lab 3: Monitor cgroup Stats

Check CPU stats:

Check memory usage:


πŸ”Ή Lab 4: systemd-based cgroups (v2-style)

Step 1: Start a transient systemd unit with CPU limit

Step 2: Check status

βœ… Expected: The stress task runs with CPU limited by systemd.


πŸ”Ή Bonus: Inspect Process Membership

Get PID of a process:

Check its cgroup:


βœ… Wrap-Up

  • You applied CPU & memory limits to processes using cgroups.

  • Explored both cgroups v1 (manually) and systemd-based cgroups (v2).

  • Observed how system resources can be controlled at the kernel level.


Last updated