The case
One browser. Twelve entries. And the order system, just one.
Isaac opened the task manager and saw the browser listed twelve times. The store's order system showed up only once, yet it served dozens of customers at the same time.
Two programs, two ways of organizing the work. What does the operating system mean by "a running program"?
This post builds on the Kernel: it is the kernel that creates and tracks everything described here.
The problem
A program on disk does nothing
A program is a file, stored on disk. For it to run, the system has to load it into memory, give it a slice of the processor and keep track of what it does. Two needs appear at the same time:
- Isolate: two programs can't wreck each other's memory.
- Do many things: a single program, like the order system, has to serve many customers without making one wait for another.
The solution
Process: a running program
A process is a running program. The program is the recipe on paper, and the process is the kitchen at work. For each process, the kernel keeps a record of everything it needs to know:
- the PID (process ID), a number that identifies the process;
- the state (running, waiting…);
- the memory it can use, separate from the others';
- the open files and the values of the processor registers.
That record is called the process control block (PCB).
How a process is born
On Unix systems, like Linux and macOS, a new process is born in two steps:
forkcreates a child process, a copy of the process that called it (the parent). To avoid copying everything at once, Linux uses copy-on-write: parent and child share memory until one of them writes, and only then is the changed part copied.execswaps the child's program for another one. That is how the terminal opensls: it does afork, and the child runsls.
What the process is doing
At any moment, a process is in one of these states:
| State | What it means |
|---|---|
| Ready | It has everything it needs to run and waits for its turn on the processor |
| Running | It is using the processor right now |
| Blocked (or waiting) | It waits for something to continue, like a reply from the disk or the network |
The kernel moves processes between these states so fast that everything seems to run at once. Each switch requires saving the state of the one leaving and restoring the state of the one entering: that is the context switch, and it costs time.
Inside the process
Thread: several lines of execution
A thread is a line of execution inside a process. Every process has at least one, and it can have several. The threads of the same process:
- share the process's memory and open files;
- each have their own stack and registers, because each one is at a different point in the code.
The process is Isaac's store, with its counter, its stockroom and its cash register. The threads are the employees: all work in the same store and use the same stockroom, and each one is doing a different task. Another store is another process: it has its own stockroom, and nobody from one gets into the other's stockroom.
Because threads share memory, creating one and switching between them is cheaper than with processes. The price is risk: two threads touching the same data at the same time can get in each other's way, a problem that will come up in the posts about concurrency.
Don't mix them up
Process × thread
Process
“Do I need strong isolation between the parts?”
- Example
- Each browser tab, so that one crashing doesn't take down the others.
- Focus
- Its own memory, more expensive to create, and one's failure doesn't affect the others.
Thread
“Do I need several tasks working on the same data?”
- Example
- The order system, serving many customers with one thread per order.
- Focus
- Shares memory, is lighter, and one's failure can take down the whole process.
In practice
Where this shows up
Browser (a process per tab)
Modern browsers use separate processes to isolate tabs and extensions.
On Isaac's computer: the twelve browser entries.
Server with threads (thread pool)
A server keeps a group of threads ready, and each request uses one of them.
At the store: the order system serving dozens of customers.
Watching processes and threads (ps, top)
On Linux, ps -eLf lists the processes and their threads, and top shows what is using the processor.
| Situation | Choice |
|---|---|
| Programs that can't get in each other's way, or third-party code | Processes |
| Many tasks that read and write the same data | Threads |
| A failure can't bring down the rest | Processes |
| Creation has to be fast and cheap | Threads |
Key terms
Six terms to remember
Tap a card to see the definition.
Quick test
Three questions, exam style
01
What do the threads of the same process share?
02
On a Unix system, what does the fork call do?
03
A thread accesses an invalid region of memory and the program is ended. What happens to the other threads of the process?
Process and thread answer two different needs: the process isolates a program from the others, and the thread lets a program do many things with the same data. Choosing between them is choosing between isolation and economy, and that is what explains why the browser uses processes and Isaac's server uses threads.