In one minute
- A look inside Linux: how programs ask the kernel for help (system calls), how processes are born, how memory really works, and how namespaces and cgroups make containers.
- At expert level you stop treating Linux as a black box. You can explain puzzles that confuse everyone else: why a container is killed for memory when the server has plenty free, why a process ignores
kill -9, or why an app is slow at only 40% CPU. - Apps never touch hardware directly. They ask the kernel through system calls. The kernel gives each process its own private view of memory. Containers are just normal processes that the kernel shows a smaller view of the system (namespaces) and puts limits on (cgroups).
- 👉 Hard production puzzles, container memory and CPU problems, and senior interviews.
Think of it like: a restaurant. Customers (apps) never walk into the kitchen (hardware). They give orders to the waiter (system calls), and the kitchen staff (kernel) do the work.
User space and kernel space
Linux has two zones. Your apps live in user space. The kernel lives in kernel space and is the only part allowed to touch the hardware.
User space: your apps, shells, libraries (glibc) │ system calls (read, write, open, fork, mmap, ...)Kernel space: scheduler, memory manager, filesystems, network stack, drivers │Hardware: CPU, RAM, disks, network cardsIt keeps the system safe. A buggy app cannot break the hardware or other apps, because it has to ask the kernel for everything.
Every file read, network packet and new process goes through a system call (a request to the kernel).
Watching system calls: strace
strace shows every system call a program makes.
👉 "What is this program actually doing?" or "Why is it stuck?"
strace -c lsstrace -f -e trace=openat,connect -p 2211 # what files and sockets is it opening?strace -c -p 2211 # which syscalls dominate (Ctrl+C for summary)strace -T -e trace=read,write ./app # time spent in each callA stuck program sitting on connect() to an IP tells you exactly which service it is waiting for. strace slows the program down a lot, so use it only briefly in production.
How processes are born
Three steps:
- fork(): the parent makes a copy of itself. Linux is clever here: the copy shares the parent's memory until one of them changes something (copy-on-write), so it is fast.
- exec(): the copy replaces itself with a new program.
- wait(): the parent collects the child's exit result when it finishes. If the parent never does, the child stays a zombie.
Think of it like: photocopying a form (fork), then writing new details over it (exec). When the job is done, the manager must sign it off (wait), or it sits in the in-tray forever (zombie).
Why this matters for containers: inside a container, your app is often PID 1. PID 1 has special jobs: it must clean up zombie children and pass signals (like SIGTERM) on. Many apps are not written to do this. That is why many images use a tiny helper called tini (or docker run --init).
Process states, again
| State | Meaning | Can it be killed? |
|---|---|---|
R | Running, or ready to run | Yes |
S | Sleeping, waiting for something (can be woken) | Yes |
D | Stuck waiting for the disk or network storage (cannot be woken) | Not even with kill -9, until the storage answers |
T | Stopped | Yes |
Z | Zombie (finished, not cleaned up) | It is already dead: fix or kill its parent |
Clue: many processes in D, high load, but low CPU use → a storage problem: a dying disk, an overloaded EBS volume, or a stuck NFS mount.
Remember it: D = Disk-stuck. You cannot kill it until the disk answers.
Everything is a file: /proc
The kernel shows live information about every process as files under /proc.
👉 Digging into one process, or when a minimal container has no tools installed.
cat /proc/loadavgcat /proc/meminfols /proc/2211/cat /proc/2211/status # state, memory, threads, UIDcat /proc/2211/cmdline # full command linels -l /proc/2211/fd | wc -l # number of open file descriptorscat /proc/2211/limits # its ulimitscat /proc/2211/environ | tr '\0' '\n' # its environment variablesTools like ps, top and free simply read these files. Even in a tiny container with no tools, /proc is still there.
File descriptors
Every open file, network connection and pipe gets a number called a file descriptor. Three are always there: 0 = input (stdin), 1 = output (stdout), 2 = errors (stderr).
That is why 2>&1 means "send errors (2) to the same place as output (1)".
When it bites: "Too many open files" means a process reached its limit. Often it is a leak: connections that are opened and never closed. Count them with ls /proc/PID/fd | wc -l and compare with /proc/PID/limits.
Virtual memory
Every process thinks it has its own big, private memory. The kernel quietly maps that onto real RAM, in small chunks called pages (usually 4 KB).
It keeps processes apart, and lets Linux use RAM cleverly.
| Word | Meaning |
|---|---|
| VSZ / VIRT | Memory reserved. Can be huge and harmless |
| RSS / RES | Memory really in RAM right now |
| PSS | Like RSS, but shared memory is split fairly between processes |
| Page cache | File data kept in RAM to speed things up; given back when apps need it |
| Page fault | Touching a page that is not in RAM. A "major" one means reading from disk (slow) |
| Swap | Pages moved out to disk when RAM is full |
| Overcommit | Linux lets programs reserve more than exists, betting they will not use it all |
Think of it like: a hotel that takes more bookings than it has rooms (overcommit), because not everyone turns up. Usually fine. If everyone does turn up, someone gets turned away (the OOM killer).
When memory truly runs out, the OOM killer picks a process to stop, mostly the one using the most memory. You can protect an important process by lowering its /proc/PID/oom_score_adj (in systemd: OOMScoreAdjust=-500).
free -mcat /proc/meminfoNamespaces: how containers are kept apart
A container is just a normal process. The kernel uses namespaces to show it a smaller, private view of the system.
| Namespace | What the container gets its own copy of |
|---|---|
| PID | Process numbers: the app inside is PID 1 |
| NET | Network cards, IPs, routes, ports |
| MNT | The filesystem it sees |
| UTS | Its hostname |
| IPC | Shared memory between processes |
| USER | User IDs: root inside can be a normal user outside |
| CGROUP | Its view of the limits |
Think of it like: blinkers on a horse. The horse (process) is still in the same field (server), but it can only see its own little patch.
lsns # list namespaces on the hostsudo nsenter -t 2211 -n ip addr # run 'ip addr' inside PID 2211's network namespaceExpert trick: nsenter lets you run the host's tools inside a container's network view, even when the container image has no tools at all.
cgroups: how containers are limited
Control groups (cgroups) cap and measure how much CPU, memory, disk and processes a group may use. Docker's --memory and Kubernetes resources.limits are cgroup settings.
Think of it like: a prepaid phone plan. Namespaces decide what you can see. cgroups decide how much you can use.
cat /sys/fs/cgroup/system.slice/orders.service/memory.maxcat /sys/fs/cgroup/system.slice/orders.service/memory.currentcat /sys/fs/cgroup/system.slice/orders.service/cpu.stat| cgroup v2 file | Meaning |
|---|---|
memory.max | Hard limit. Going over it triggers the OOM killer for that group only |
memory.current | Current use, including page cache |
cpu.max | CPU allowance, e.g. 100000 100000 = 1 CPU |
cpu.stat | Includes nr_throttled: how often the group was held back |
pids.max | Most processes allowed (stops runaway process creation) |
This explains two famous container puzzles:
- "OOMKilled, but the server has free memory!" The container hit its own
memory.max. The server's free memory does not matter. - "The app is slow, but CPU is only 40%!" The container is being throttled (held back) by
cpu.max. Checknr_throttledincpu.stat. This is why many teams set CPU requests but not CPU limits in Kubernetes.
The scheduler, briefly
The scheduler decides which task gets the CPU, and for how long. (Recent kernels use one called EEVDF; older ones used CFS.)
nicevalues (−20 to 19) change a process's priority. A higher number = "nicer" = lower priority.- cgroup CPU weights do the same for whole groups.
- A very high context-switch count (
csinvmstat) means too many threads fighting for the CPU.
nice -n 10 tar czf backup.tgz /etcThis runs a backup at lower priority, so it does not slow down the main app.
Real-world scenario: a container keeps getting OOMKilled
The node has 10 GB free, yet the pod keeps restarting with OOMKilled.
free -mkubectl describe pod orders-7d9f | grep -A3 "Last State" # Reason: OOMKilled, exit 137cat /sys/fs/cgroup/.../memory.max # 536870912 (512Mi)Java was set to use up to 1 GB, inside a container limited to 512 Mi. Set Java's memory to fit the limit (or raise the limit) and the restarts stop.
The lesson: the node's free memory did not matter. The cgroup limit is what counts.
Common mistakes
- Reading VSZ as real memory use.
- Trying to
kill -9a process inDstate instead of fixing the stuck storage. - Running apps as PID 1 without an init, which leaks zombies and ignores SIGTERM.
- Setting tight CPU limits, then chasing "mysterious" slowness caused by throttling.
Practice
- Run
strace -con a simple command and name the most common system call. - Explain why a process in
Dstate ignoreskill -9. - List four namespaces and what each one keeps separate.
- A container uses 300 Mi but is OOMKilled at a 512 Mi limit during traffic spikes. What would you check?
Cheat sheet
| Command | What it shows |
|---|---|
strace -c -p PID | Summary of system calls |
cat /proc/PID/status | State, memory, threads |
ls /proc/PID/fd | wc -l | Open file descriptors |
lsns | Namespaces |
nsenter -t PID -n cmd | Run a command in a process's network view |
cat /sys/fs/cgroup/.../memory.max | cgroup memory limit |
cpu.stat nr_throttled | How often CPU was held back |
Key takeaways
- Apps ask the kernel for everything through system calls.
straceshows them. - A process stuck in
Dstate is waiting for storage, and evenkill -9cannot stop it. /procholds live details about every process.- A container is a normal process: namespaces limit what it sees, cgroups limit what it uses.
- Containers are killed or slowed by their own limits, not by how busy the server is.