Skip to content
$ lynuxpro
← Linux

LESSON 23 OF 26 · EXPERT · 23 MIN

Linux internals: processes, memory and the kernel

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 cards
OUTPUTterminalutf-8 · 5L

It 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 ls
SHELLbashutf-8 · 1L
strace -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 call
OUTPUTterminalutf-8 · 3L

A 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:

  1. 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.
  2. exec(): the copy replaces itself with a new program.
  3. 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

StateMeaningCan it be killed?
RRunning, or ready to runYes
SSleeping, waiting for something (can be woken)Yes
DStuck waiting for the disk or network storage (cannot be woken)Not even with kill -9, until the storage answers
TStoppedYes
ZZombie (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/meminfo
SHELLbashutf-8 · 2L
ls /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 variables
OUTPUTterminalutf-8 · 6L

Tools 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.

WordMeaning
VSZ / VIRTMemory reserved. Can be huge and harmless
RSS / RESMemory really in RAM right now
PSSLike RSS, but shared memory is split fairly between processes
Page cacheFile data kept in RAM to speed things up; given back when apps need it
Page faultTouching a page that is not in RAM. A "major" one means reading from disk (slow)
SwapPages moved out to disk when RAM is full
OvercommitLinux 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/meminfo
SHELLbashutf-8 · 2L

Namespaces: 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.

NamespaceWhat the container gets its own copy of
PIDProcess numbers: the app inside is PID 1
NETNetwork cards, IPs, routes, ports
MNTThe filesystem it sees
UTSIts hostname
IPCShared memory between processes
USERUser IDs: root inside can be a normal user outside
CGROUPIts 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 namespace
OUTPUTterminalutf-8 · 2L

Expert 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
OUTPUTterminalutf-8 · 3L
cgroup v2 fileMeaning
memory.maxHard limit. Going over it triggers the OOM killer for that group only
memory.currentCurrent use, including page cache
cpu.maxCPU allowance, e.g. 100000 100000 = 1 CPU
cpu.statIncludes nr_throttled: how often the group was held back
pids.maxMost 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. Check nr_throttled in cpu.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.)

  • nice values (−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 (cs in vmstat) means too many threads fighting for the CPU.
nice -n 10 tar czf backup.tgz /etc
SHELLbashutf-8 · 1L

This 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 -m
SHELLbashutf-8 · 1L
kubectl describe pod orders-7d9f | grep -A3 "Last State"   # Reason: OOMKilled, exit 137cat /sys/fs/cgroup/.../memory.max                          # 536870912 (512Mi)
OUTPUTterminalutf-8 · 2L

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 -9 a process in D state 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

  1. Run strace -c on a simple command and name the most common system call.
  2. Explain why a process in D state ignores kill -9.
  3. List four namespaces and what each one keeps separate.
  4. A container uses 300 Mi but is OOMKilled at a 512 Mi limit during traffic spikes. What would you check?

Cheat sheet

CommandWhat it shows
strace -c -p PIDSummary of system calls
cat /proc/PID/statusState, memory, threads
ls /proc/PID/fd | wc -lOpen file descriptors
lsnsNamespaces
nsenter -t PID -n cmdRun a command in a process's network view
cat /sys/fs/cgroup/.../memory.maxcgroup memory limit
cpu.stat nr_throttledHow often CPU was held back

Key takeaways

  • Apps ask the kernel for everything through system calls. strace shows them.
  • A process stuck in D state is waiting for storage, and even kill -9 cannot stop it.
  • /proc holds 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.