Skip to content

Lesson 1 of 9 · 22m

Processes, Threads, and the Anatomy of a Running Program

What a process really owns, what threads share, and how to read ps, /proc, and a crash without guessing.

The first production mental model worth keeping: a process is not 'a program running.' It is the kernel's container for a running program — an address space, open file descriptors, credentials, resource limits, signal disposition, and one or more threads of execution. When something goes weird in production, the answer is often in one of those ownership boundaries.

Processes are ownership boundaries

$ ps -o pid,ppid,user,stat,%cpu,%mem,rss,cmd -p 24117
    PID    PPID USER     STAT %CPU %MEM   RSS CMD
  24117       1 www-data Sl    2.8  1.4 152304 /usr/sbin/nginx -g daemon off;

Here nginx is a process owned by init (PPID 1), running as www-data, sleeping most of the time (S), multithreaded (l), and holding about 152 MiB resident memory. STAT is compressed but useful: R means on CPU or runnable, D means uninterruptible kernel sleep — usually I/O — and Z means zombie.

The kernel gives each process its own virtual address space. One process cannot normally read another's memory, which is why privilege separation works: nginx workers can run as a low-privilege user while the master process manages privileged sockets and configuration reloads.

Threads share almost everything that matters

Threads in one process share memory, file descriptors, signal handlers, and resource limits; they differ in stack, registers, and scheduling state. That sharing is why threads are efficient and why they are dangerous.

/* Two pthreads incrementing the same counter without synchronization. */
counter++;       /* read-modify-write: not atomic */

The failure mode is not deterministic corruption. It is 'works for months, corrupts under a burst, then prints clean logs.' Data races produce timing-dependent bugs. In production, prefer explicit ownership, queues, immutable snapshots, or language runtimes that make the invariant impossible to break.

/proc is the process's public file cabinet

ls -l /proc/24117/fd | head
cat /proc/24117/status
cat /proc/24117/limits

/proc/<pid>/fd shows open sockets, files, and pipes. /proc/<pid>/status exposes threads, memory, capabilities, and signal masks. /proc/<pid>/limits explains mysteries like 'why did the service die at 1024 connections' — often Max open files.

Fork, exec, and why zombies happen

Unix creates processes with fork() and replaces the program with execve(). When a child exits, it remains a zombie until the parent reaps it with wait(). A few zombies are normal churn. Thousands mean the supervisor is not reaping children; restarting the parent usually clears them because zombies are reparented or disappear with their owner.

Field notes

  • D state is not 'using CPU'; it usually means the thread is stuck in kernel I/O. Look at storage, NFS, or block devices before blaming the application.
  • A thread count explosion is a symptom, not a root cause. Find what work items are being created and why they are not draining.
  • File descriptor leaks hide in health checks, metrics clients, and retry loops. ls /proc/<pid>/fd | wc -l over time tells the truth.
  • Process title tricks in ps are cosmetic. Use unit files, cgroups, and /proc/<pid>/exe when attribution matters.

Was this lesson helpful?

Sign in to rate