4.2.41 · D3Operating Systems

Worked examples — Containers — namespaces, cgroups, difference from VMs

2,891 words13 min readBack to topic

This page is the drill floor for Containers — namespaces, cgroups, difference from VMs. We take the two ideas from the parent — namespaces (what a process can SEE) and cgroups (what a process can USE) — and push them through every case that a real machine or an exam can throw at you.

Before we start, one reminder so nothing is used before it's built:


The scenario matrix

Every question about containers lands in one of these cells. The worked examples below are each tagged with the cell they cover, and together they hit all of them.

# Case class The tricky part Example
A CPU quota < period (fraction of a core) share below 1 Ex 1
B CPU quota = period (exactly one core) boundary value Ex 2
C CPU quota > period (multiple cores) share above 1 Ex 3
D Degenerate quota = max (unlimited) division by "infinity" Ex 4
E Memory limit + OOM decision byte arithmetic, over/under Ex 5
F PID namespace numbering the "PID 1" surprise Ex 6
G User namespace UID mapping root-inside ≠ root-outside Ex 7
H Container vs VM word problem which one, and why Ex 8
I Exam twist: cgroup without namespace isolation vs limits confusion Ex 9

CPU share: cases A, B, C, D

The next figure is a timeline of one CPU. Time runs left to right; each period is a box; the shaded part of each box is the quota the group is allowed to run. Look at how the fraction filled changes across the four cases — that fraction is the answer. (Case C needs more than one core, so it also needs more than one such timeline — we handle that in words in Ex 3.)

Figure — Containers — namespaces, cgroups, difference from VMs

Memory: case E


Namespaces: cases F and G

The next figure shows two process tables side by side: the host's table (many PIDs) and the container's private PID-namespace table (renumbered from 1). Follow the red arrow — it's the same running process wearing two different ID badges.

Figure — Containers — namespaces, cgroups, difference from VMs

Container vs VM: cases H and I


Recall Self-test across the whole matrix

cpu.max = "50000 200000" gives how many cores? ::: cores memory.max = 134217728 is how many MiB? ::: MiB New PID namespace: first process gets which PID? ::: 1 User-ns maps container-0 to base 100000 → host UID? ::: 100000 (unprivileged, not root) What must /etc/subuid contain for that mapping to be allowed? ::: a delegated range for the user, e.g. alice:100000:65536 Cgroup alone, no namespace: can it hide host processes? ::: No — only namespaces isolate view memory.current charges RSS plus what? ::: reclaimable page cache (so charged memory > RSS)


Back to the parent topic · prerequisite: Processes and the clone/fork syscall · related: Scheduling — CFS and CPU shares, Networking — virtual interfaces and bridges, Filesystems — mount and chroot.