The Plain Picture#
Each hotel guest gets their own room: their own bed, wardrobe, and belongings. Guests can't see into each other's rooms. The manager keeps a register at the front desk: room number, guest name, check-in time, what they've ordered.
The Real Name#
A process is a running program with its own private "room":
its own memory (Ch 5),
its own list of open files and connections,
its own ID number, the PID (process ID),
the state of the CPU when it last ran — register values, including the program counter (How Computers Work Ch 5) — so it can continue exactly where it stopped.
The kernel keeps a record for every process — the hotel's register. It's called the process control block (PCB).
Under the Hood#
What's inside a process's memory#
high addresses ┌───────────────────────────┐ │ STACK │ function calls and their local variables (Ch 6) │ ↓ grows down │ │ │ │ (unused space) │ │ │ │ ↑ grows up │ │ HEAP │ data created while running — objects, lists (Ch 6) ├───────────────────────────┤ │ DATA (global values) │ ├───────────────────────────┤ │ CODE (instructions) │ the machine code copied in from disk └───────────────────────────┘ low addresses
Process states#
A process is not always running. It moves between states:
created
│
▼
┌───────▶ READY ──── scheduler picks it ────▶ RUNNING
│ (waiting (on a core)
│ for a core) ◀─── time slice over ──── │
│ │ asks for disk/network data
│ ▼
└──────── data arrived ─────────────────────── BLOCKED / WAITING
│
(when finished) → EXITNotice: a process that's waiting for the disk or network is not using the CPU at all. It's parked. This one fact is the foundation of async programming (Ch 11, and later Backend with Python (coming soon)).
Creating processes#
On Unix-like systems a new process is made in two steps:
fork— the kernel makes a near-exact copy of the current process (the child gets a new PID).exec— the child replaces its own code with a different program.
When you type python3 quiz.py in a terminal, the terminal (itself a process) forks, and the child
execs Python. Every process has a parent; the very first one (PID 1) is started by the kernel at
boot. On Linux it's usually systemd; in a Docker container, PID 1 is whatever command the container
runs — which matters a lot for shutdown (Ch 12).
Try it on your Mac:
ps aux | head # list processes: user, PID, CPU %, memory %, command
ps -o pid,ppid,command -p $$ # your terminal shell's PID and its parent's PIDGotchas
Processes don't share memory. If StudyBuddy runs as two processes, a variable changed in one is not changed in the other. To share data they must use files, a database, network connections or special shared memory. This surprises people with Python multiprocessing and with web servers that run several worker processes (Backend in Production (coming soon)).
Creating a process is relatively expensive (new memory map, new kernel records). That's one reason threads exist (Ch 7).
Check yourself
4 questionsWhat does a process own that makes it separate from other processes?
Show answerHide answer
Its own private memory, its open files/connections, its PID, and its saved CPU state.
Name the four areas of a process's memory.
Show answerHide answer
Code, data (globals), heap, stack.
A process is waiting for a file to load from disk. Is it using the CPU?
Show answerHide answer
No — it's blocked/waiting, and the CPU runs other processes meanwhile.
What do fork and exec do?
Show answerHide answer
fork copies the current process into a new child; exec replaces a process's program with
another one.