👉 Overview
👀 What ?
Linux CGroups, also known as control groups, is a Linux kernel feature designed to limit, police, and account the resource usage for processes. These resources include CPU, memory, disk I/O, network, etc. It is one of the fundamental concepts that underpin the working of Linux containers and orchestration tools like Docker and Kubernetes.
🧐 Why ?
In a system where multiple processes are running, it becomes crucial to have a mechanism for resource allocation and management. Without such a mechanism, a single process could potentially consume all the available resources, causing other processes to starve or the entire system to crash. This is where CGroups comes into play. It provides a way to control the distribution of system resources among processes, making the system more stable and efficient.
⛏️ How ?
To make use of CGroups, you would need to mount the cgroup filesystem and create control groups. Each control group has a set of parameters that you can modify to limit resources. For example, you can set 'cpu.shares' to determine how much CPU time a group should get, or 'memory.limit_in_bytes' to set a limit on memory usage. Once you've configured your cgroups, you can add processes to them by writing the process's PID to the 'cgroup.procs' file.
⏳ When ?
The concept of CGroups was introduced in Linux kernel version 2.6.24, which was released in 2008. Since then, it has been a vital part of the Linux ecosystem, especially with the rise of containerization and cloud computing.
⚙️ Technical Explanations
At the core of CGroups is the idea of grouping processes and applying limits and constraints to those groups. The Linux kernel does this by attaching a cgroup to a set of processes, so that the same limits are applied to all processes in the group. The kernel tracks the resource usage of each cgroup and enforces the limits as set by the user. CGroups also provides a mechanism called 'cgroup hierarchy', which allows for the creation of nested cgroups. This means you can have a parent cgroup with child cgroups, each having its own set of limits, providing greater flexibility in resource management.