In this lesson, you will learn how systemd utilizes the Linux kernel's Control Group (cgroup) feature to transform a collection of scattered processes into a single, manageable service entity.
The Unit as a Container
Before systemd, tracking a service was difficult because a main process might spawn children, grandchildren, or background daemons that "orphan" themselves. Systemd solves this by placing every unit (like a service) into its own dedicated cgroup.
Think of a cgroup as a virtual "fenced-in area" created by the kernel. When systemd starts a service, it doesn't just execute a command; it creates a new cgroup directory—usually located at /sys/fs/cgroup/system.slice/your-service.service—and assigns the PID of your service to it. Because cgroups are hierarchical, any process that your service forks is automatically inherited into that same cgroup. This allows systemd to "see" every single process related to a service, even if they aren't direct children of the main process.
Resource Governance
Beyond tracking, this hierarchy allows you to impose strict limits on what a service can consume. By defining directives in your service file, systemd instructs the kernel to enforce these boundaries at the cgroup level.
For example, if you want to prevent a memory-hungry service from crashing your entire system, you can add these lines to your unit file:
[Service]
MemoryMax=500M
CPUWeight=50
When you reload and restart the service, systemd updates the cgroup configuration for that specific path. The kernel then monitors the total resource usage of all processes within that specific group. If the group attempts to exceed the MemoryMax, the kernel will intervene—either by slowing down the processes or triggering an OOM (Out of Memory) kill specifically for that cgroup, sparing the rest of your system.
By organizing the system into this tree-like hierarchy, systemd ensures that every service is isolated, trackable, and bounded by resource constraints that the kernel enforces directly.
Key Takeaway: Systemd uses the cgroup hierarchy to turn an unpredictable collection of processes into a unified, resource-constrained unit that the kernel can track and throttle as a single entity.