Chapter 1
What happens when you run a program
You type a word. You press Enter. Something happens. This chapter explains what, step by step, from the metal upwards.
A computer only follows instructions
Inside your machine is a chip called the CPU. It does all the thinking. Its job is simple, and it never changes:
- read one instruction
- do what it says
- read the next one
That is all. The instructions are tiny. "Add these two numbers." "Put this number here." "If that number is zero, jump to a different instruction."
A CPU does this billions of times a second. That speed is the only thing that makes it feel clever. Each single step is very small.
It does exactly what it is told. Not what you meant. When a program does the wrong thing, the computer did not make a mistake — it followed your instructions correctly, and your instructions were wrong.
This is worth remembering. It is the whole reason bugs exist.
A program is a file of instructions
A program is a file. Inside it is a long list of those tiny instructions, saved on the disk, waiting.
The ls command you have been typing is a program. It is a real
file, and you can look at it like any other file:
$ which ls
/usr/bin/ls
$ ls -l /usr/bin/ls
-rwxr-xr-x 1 root root 199464 Jan 23 2026 /usr/bin/ls
That number, 199464, is its size in bytes. About two hundred
thousand bytes of instructions, sitting on your disk, so that you can list a
folder.
Nothing about ls is special. It is a file, in a folder, like
the files you made in step 1.5.
Disk keeps, memory works
Your machine has two places to put things, and they do different jobs.
| Disk | Memory | |
|---|---|---|
| Also called | storage, SSD, hard drive | RAM |
| Speed | slow | very fast |
| When power goes off | keeps everything | loses everything |
| Holds | your files, and programs waiting | programs that are running now |
Running a program means copying it from the disk into memory, and pointing the CPU at the first instruction. The CPU then works through the list.
A program sitting on the disk is just a file. The same program, copied into memory and being worked through, is called a process. One program can be running twice, as two processes, at the same time.
The CPU only understands numbers
Those instructions are numbers. The CPU knows nothing else. It cannot read
printf or if or your variable names.
But people cannot write numbers all day. So we write words, and something turns our words into numbers. There are two ways to do that:
-
Translate the whole file once, ahead of time, and save the result as a
program. This is what
clangdoes in step 3. -
Translate it line by line, while it runs. This is what
python3does in step 4.
That single difference explains most of what feels different about C and Python. Chapter 7 comes back to it properly.
The operating system shares the machine
You have one CPU, but many programs want to run. Something must decide who gets a turn. That something is the operating system.
Its job is to:
- give each program a turn on the CPU, switching very fast between them
- give each program its own piece of memory, and keep the pieces apart
- own the disk, the screen and the network, and hand them out
Keeping programs apart matters. It is why one program crashing does not take the whole machine with it.
Windows is an operating system. Linux is another one. Ubuntu is a way of getting Linux, which chapter 3 explains.
The shell is a program that starts programs
Now the last piece. The shell is a program too. Its whole job is to start other programs for you.
When you type ls and press Enter, the shell:
- reads what you typed
- looks for a program with that name, and finds
/usr/bin/ls - asks the operating system to run it
- waits while it runs
- shows you the prompt again
That is the loop, over and over. Everything you do in a terminal is some version of it.
You typed ls, not /usr/bin/ls. The shell has a
list of folders it searches, in order. That list is called
PATH, and chapter 5 explains it.
It also explains the most common message a beginner sees: command not found. It rarely means the program is missing. It usually means the shell looked in its list and the program was not in any of those folders.
What the prompt is telling you
That $ is the shell saying: I am not busy. Type
something.
While a program is running, there is no prompt. The shell is waiting, so it has nothing to say. When the program finishes, the prompt comes back.
A student opens less or htop, and the prompt
disappears. Nothing is broken and nothing is frozen. A program is running,
and it will not give the terminal back until it ends.
Press q to ask it to finish, or Ctrl+C to ask the operating system to stop it. Then the prompt returns.
Try it
Find out where a command lives:
$ which ls
See that it really is a file, with a size:
$ ls -l /usr/bin/ls
Now watch the prompt go away and come back. This program does nothing for five seconds:
$ sleep 5
Run it again and press Ctrl+C before the five seconds are up. The prompt comes straight back, because you asked the operating system to stop the program early.
$ sleep 30
You can now explain
- What a CPU does, and why it never guesses
- That a program is a file of instructions, and
lsis one - The difference between disk and memory
- What the word process means
- Why our words have to be turned into numbers before a CPU can use them
- What an operating system is for
- What the shell does when you press Enter
- Why the prompt disappears while a program is running
