Processes, Threads and Scheduling

What a process really is, how threads differ, and how the OS runs far more of them than you have CPU cores without you noticing.

· 2 min read · intermediate

Your machine runs hundreds of programs on a handful of cores. The trick is the OS switching between them so fast it looks simultaneous. Here's the model.

What a process is

A process is a running program plus everything it owns: its own virtual address space (code, data, heap, stack), open files, and a unique PID. Crucially, processes are isolated — one process can't read another's memory, which is what keeps a crash in one program from taking down the rest.

bash
ps -ef --forest     # processes as a tree: who launched whom

Every process (except the first) has a parent that created it. That parent/child tree is why a shell can spawn commands, and why an unexpected parent (a document viewer spawning a shell) is a security red flag.

Threads: sharing within a process

A thread is a unit of execution inside a process. A process starts with one thread but can create more, and — unlike separate processes — threads share the same memory.

ProcessThread
Memoryisolated, its ownshared with siblings
Creation costhigherlower
A crash affectsjust itselfthe whole process
Communicationpipes, sockets, shared memshared variables (needs locks)

Sharing memory makes threads fast to coordinate but dangerous: two threads touching the same data at once cause race conditions, which is why locks and other synchronisation exist.

Scheduling: more work than cores

You have, say, 8 cores but 400 runnable tasks. The scheduler gives each a brief slice of CPU time, then performs a context switch — saving one task's registers and restoring another's — fast enough that everything appears to run at once. This is concurrency (interleaving) which, with multiple cores, becomes real parallelism too.

A process is usually in one of these states:

text
        ┌──────────┐  scheduled   ┌─────────┐
        │  READY   │ ───────────▶ │ RUNNING │
        └──────────┘ ◀─────────── └─────────┘
              ▲        preempted        │ waits for I/O
              │                         ▼
              └────────────────── ┌─────────┐
                 I/O completes     │ BLOCKED │
                                   └─────────┘

A task that's waiting on the disk or network is blocked and uses no CPU — so a machine can run thousands of mostly-idle processes comfortably. Watch it live:

bash
htop        # per-core load, memory, and per-process state

Takeaways

  • A process is an isolated program with its own memory; a thread runs inside a process and shares its memory.
  • Isolation protects processes from each other; shared memory makes threads fast but race-prone.
  • The scheduler time-slices many tasks across few cores via fast context switches.
  • Blocked (I/O-waiting) tasks use no CPU, which is why hundreds of processes coexist; htop shows it all.

Related

Your First Linux Kernel Module

Build, load and inspect a working kernel module from scratch. Understand kernel space vs user space, the module lifecycle, and why a bug here crashes the whole machine.

· 3 min read