Containers · Course contents
Machine not started
30 min·Intermediate

Give a process a resource budget

Use cgroup v2 to enforce CPU, memory, and process-count limits.

Jump to code and terminal ↓

Namespaces do not allocate resources

A process can have private names and still consume every CPU cycle or page. Cgroups account for work and enforce budgets. The unified cgroup v2 tree lives at /sys/fs/cgroup.

Policy first, membership second

Use cpu.max = 20000 100000, memory.max = 16777216, and pids.max = 8. These mean 20 ms of CPU per 100 ms period, 16 MiB charged memory, and at most eight tasks. Only move the command child into the supplied group.

Your change

Write the three controller files, then have a child shell write its own PID to cgroup.procs before exec. Tests request too many children, touch excess memory, and burn CPU; the kernel must block, kill, and throttle those workloads.

Your goal

Apply a 20% CPU quota, a 16 MiB memory ceiling, and an eight-process limit to a child without limiting the course shell.

What the tests observe

  • Actual fork rejection
  • Cgroup OOM accounting
  • CPU throttling counter
  • Parent outside the budget

Need a nudge?

Hint 1

Controller settings are ordinary writes to kernel pseudo-files. Configure them before launching the workload.

Hint 2

A shell can move itself with echo $$ > GROUP/cgroup.procs; then exec replaces it without changing membership.

Hint 3

Do not write the parent shell PID to cgroup.procs: the grader must stay outside the student resource budget.

What happens inside Linux

The pids controller rejected forks, memcg charged actual touched pages and invoked a local OOM kill, and the CPU controller stopped scheduling the child until another quota period. These are independent policies on the same process subtree.

Optional account sync. Exercises work without an account.

sh/work/limits.shLoading local work…
Restoring your workspace…
⌘ / Ctrl ↵
TerminalRISC-V 64 · root · /work

Your Linux machine is ready to start.

Boot a disposable environment in this browser. You’ll get a real root shell, with no remote server or account required.

Isolation checks

Observed inside Linux

Run tests to inspect namespace identities, process visibility, and the hostname. Each check reports the behavior your program produced.