A second ago, WhatsApp was just an icon on your home screen. You tapped it, and now your chats are loading, a voice note is playing and a photo is coming in. The app didn't change, but something on your phone came alive.

What exactly came alive when you tapped?

See it#

Tap an app and follow it. It doesn't run the moment you tap: first it gets set up, then it waits its turn.

Here's what stays and what moves. The app on your storage never changed: it's still the same file. What moved was a live copy of it. That copy got set up, waited in line, ran on the CPU, stopped to wait for a photo to load, went back in line, and ran again.

Name it#

The app sitting on your storage is a program, a file that does nothing by itself. A process is a program while it runs: a copy loaded into memory, with its own ID, that the operating system (the OS: the software that shares your phone's hardware among its apps) gives turns on the CPU.

So one program can become many processes. On a laptop, open two terminal windows (the windows where you type commands) and start the same calculator program in each: there's still one program on the disk, but two processes are running, each with its own memory and its own ID. Close one and the other carries on.

Every app you have open right now is at least one process. Android, iOS, Windows, macOS and Linux all work this way.

How it works#

A process is more than its code. The OS gives it three things: its own memory, a place in a life of five states, and a card that remembers where it stopped.

1. It gets its own memory, in parts#

When a program becomes a process, the OS gives it its own space in memory (What is an OS showed why no other app can read it). That space is split into parts, each with one job:

  • Code: the program's instructions. They don't change while it runs.
  • Data: values the whole program shares, like a game's high score.
  • Stack: the notes for each function call.
  • Heap: memory the program asks for while it runs.

The stack is where function calls live. Each time a function is called, it gets a frame on top of the stack holding its local values. When the function returns, its frame comes off. The last call in is the first one out, and nobody has to free anything: it happens by itself.

The heap is for memory whose size isn't known until the program is running. WhatsApp can't know in advance how many messages a chat has, so it asks for the room once it knows. That memory comes from the heap, and it stays until the program gives it back. (In C, asking is a call to malloc. In Java and Python, creating a new object does it for you.)

2. It moves through five states#

From the tap to the moment you close it, a process is always in one of five states:

StateWhat it meansWhatsApp right now
NewThe OS is setting it upYou just tapped the icon
ReadyIt could run, and is waiting for its turn on the CPULoaded, waiting in line
RunningIt's on the CPU this momentDrawing your chats
WaitingIt can't go on until something happens, so the CPU would be no use to itWaiting for a photo to arrive from storage
Done (also called terminated)It has finished, and the OS cleans upYou swiped it away

The moves between them follow from what each state means:

  • Running to waiting: the process asks for something slow, like a read from storage, and stops until it arrives. Giving it the CPU now would be wasted, so it isn't ready, it's waiting.
  • Waiting to ready: the storage finishes and tells the CPU so. The process can run again, but another process may be on the CPU, so it joins the ready line. It never jumps straight back to running.
  • Ready to running: the OS picks it for its turn.
  • Running to ready: its turn ends while it still has work to do. (How turns end is the Multitasking lesson.)

3. The OS keeps a card for it#

For every process, the OS keeps one card, called the process control block (PCB). It holds:

  • the process's ID, its number;
  • its state, from the table above;
  • the program counter: which instruction it runs next;
  • the CPU's registers: the few values the CPU was holding for it, in tiny slots inside the chip;
  • where its memory is, and the files it has open.

The card matters most when the OS takes the CPU away. Before the next process gets the CPU, the OS writes the program counter and registers of the one leaving into its PCB. When that process gets the CPU back, the OS copies them back in, and the process carries on from the exact instruction where it stopped, as if nothing had happened. (Doing that swap fast, many times a second, is the Multitasking lesson.)

Why it matters#

You can see processes yourself. Task Manager on Windows and Activity Monitor on a Mac list every process, each with its ID, its memory and its share of the CPU. When your phone runs low on memory, Android ends background processes, which is why an app you left open sometimes starts again from the top. And Chrome runs each tab as its own process, so one tab that crashes doesn't take the others down. (Why that's safer than threads is the Threads lesson.)

In interviews, this topic comes up as "What is the difference between a program and a process?", "What are the states of a process?" and "What is a PCB?". The words they use match what you just saw:

Interviews call itWhat you saw
Program vs processthe file on storage vs the live copy in memory
Process address spacecode, data, stack and heap
Process statesnew, ready, running, waiting, done
Process control block (PCB)the OS's card: ID, state, program counter, registers

What to remember#

  • A program is a file; a process is that program running. One program run twice is two processes.
  • A process's memory has code, data, a stack for function calls and a heap for memory asked for at run time.
  • Its states are new, ready, running, waiting and done. Waiting means it can't use the CPU until something arrives, and from waiting it goes back to ready.
  • The OS keeps one card per process, the PCB, and saves the program counter and registers there when it takes the CPU away.