Course outline · 0% complete

0/28 lessons0%

Course overview →

What "running a program" really means

lesson 3-1 · ~10 min · 7/28

Quiz

Warm-up from unit 1: a program file sits on disk. Before the CPU can fetch its instructions (lesson 1-2), what must happen first?

Program vs process

These two words are not synonyms, and the difference is the key idea of this unit.

  • A program is a file on disk: instructions, sitting there, doing nothing. python3 is a program. So is your script.
  • A process is a running instance of a program: the code loaded into RAM, plus its own memory for variables, plus bookkeeping the OS keeps about it (who started it, what files it has open, where its program counter is).

This distinction is the working vocabulary of real operations: ps, kill, "the server runs four workers", "restart the process", Docker containers — all of these are statements about processes, not programs. Get it wrong and commands like kill seem mysterious; get it right and they become obvious.

One program can be many processes. Open three terminal windows and run python3 in each: one program on disk, three separate processes, each with its own memory. A variable set in one is invisible to the others.

programfile on diskprocess, PID 4021code + its own variablesx = 5process, PID 4022code + its own variablesx = 99launchlaunch
One program on disk can run as many processes, each with private memory. The two x variables never see each other.

The process table

The OS tracks every process in a process table: one entry per process with its ID, owner, state, and more. The states matter:

  • running: on the CPU right now
  • ready: could run, waiting for a turn on the CPU
  • waiting (blocked): cannot run until something happens, like a disk read finishing or a key press

On a machine with 4 CPU cores and 300 processes, at most 4 are running at any instant. Almost everything is ready or waiting. The simulation below prints a toy process table.

Code exercise · python

Run this toy process table. Each entry is a dictionary, one per process, like the OS keeps internally.

Quiz

You run the same Python script in two terminals at once. Script A sets total = 10. What does script B see in its total variable?