TL;DR
A process is a program while it is running. It has the program's instructions, the memory the program uses, and a record the operating system keeps about it, such as which instruction comes next. A processor core runs one process at a time, so a computer with many programs open switches quickly between their processes, giving each a short turn, and each turn feels like the program never stopped.
Builds on: what a processor and its memory are (see Memory Hierarchy) · Leads to: Operating System, Virtual Memory, Thread
Where it's used
Every browser tab. Modern browsers such as Chrome run many tabs as separate processes. If one page crashes or misbehaves, the others keep working, because their memory is separate.
Your laptop's task list. On macOS, Activity Monitor lists every running process with its name, the share of the processor it uses and the memory it holds. On Windows, Task Manager shows the same thing. Each row is one process, even when two rows have the same name, such as two open terminal windows.
Servers handling requests. Older web servers, such as Apache in its classic "prefork" mode, kept a pool of processes ready and gave each incoming connection to one of them. A crash in one process ended only that request, and the pool started a replacement.
How it works
We will follow two tiny programs on one processor. Each program is a list of instructions, and one instruction takes one tick of the clock.
- Program A:
a1setsx = 2 + 3,a2printsx,a3exits. - Program B:
b1counts "1",b2counts "2",b3exits.
The operating system gives each process one tick in turn. Three words need defining first. The program counter is the number that says which instruction is next. The registers are the processor's small scratch cells, the fastest level of the memory, which hold values such as x while a program works. The saved state is the record the operating system keeps for each process: its program counter, its register values and where its memory lives.
- Create the processes. The operating system loads program A into memory and creates a record for it: program counter at
a1, registers empty. It does the same for program B, with program counter atb1. Check: two processes exist, and neither has run yet. - Give A a tick. The operating system loads A's saved state into the processor. The processor runs
a1, soxbecomes 5 in A's registers. The program counter moves toa2. The operating system saves A's new state. Check: A's saved state saysx = 5and the next instruction isa2. - Give B a tick. A's state is already saved from step 2, so the operating system loads B's state, and the processor runs
b1. B's count is 1. B's program counter moves tob2, and the state is saved. Check: B's saved state says the next instruction isb2. - Give A a tick. The operating system loads A's saved state, so
xis 5 again and the next instruction isa2. The processor prints 5. The program counter moves toa3, and the state is saved. Check: the output shows 5, and A has one instruction left. - Give B a tick. The processor runs
b2, and B's count is 2. The program counter moves tob3. Check: B has one instruction left. - Give A a tick. The processor runs
a3, which exits. The operating system frees A's memory and removes its record. - Give B a tick. The processor runs
b3, which exits, and B's record is removed too.
Check the whole run: each program had 3 instructions, so 6 instructions ran, and the processor did them in 6 ticks. Neither program knew the other existed. Each one saw only its own registers, because the operating system saved and restored them at every switch. The switch is the key idea of this lesson.
Real systems use slices of milliseconds, which hold millions of instructions, but the logic is the same: save, load, run, save.
stateDiagram-v2 [*] --> Ready: created Ready --> Running: scheduler gives a turn Running --> Ready: turn ends, state saved Running --> Waiting: asks for data (for example, a disk read) Waiting --> Ready: data arrives Running --> [*]: exits
The math
Suppose a machine runs processes that are all ready to run, and the operating system gives each one a turn of length , in a fixed rotation called round robin. The ready processes wait in a Queue, and each turn goes to the one at the front. We ignore the time spent switching.
Each process gets one turn of length in every full rotation, and a rotation takes time. So each process gets a fraction of the processor. The longest wait before a ready process gets a turn is the time of the other turns, which is .
Step 1 in symbols: a rotation
One rotation runs process 1, then 2, up to . Each turn takes , so the rotation takes .
Step 2 in symbols: the share
Over one rotation, process runs for out of . Its share is , which does not depend on .
Step 3 in symbols: the worst wait
A process that has just finished its turn must wait for the other turns, each of length . Its wait is . Then it gets its own turn of .
Worked examples
Problem: how many ticks does program A need alone, and how many do A and B need together, from the walkthrough above?
- Alone: A has 3 instructions, and with no other process each takes one tick. Total: 3 ticks.
- Together: A runs 3 instructions and B runs 3 instructions, so 6 instructions in all. The processor does one per tick. Total: 6 ticks.
- Each program takes 3 ticks of its own work, so the switching adds no extra ticks in this idealised model.
Interpretation: sharing does not make a program's work smaller. It makes the finish time of each program depend on the others. In this example, A finishes at tick 5 and B at tick 6.
Problem: three processes each need 20 ms of processor time. The operating system uses round robin with a turn of ms. When does each process finish, ignoring switch time?
- Each process needs turns.
- One rotation has 3 turns of 5 ms, so it takes ms.
- After rotation 1 (15 ms), each process has had 1 turn. After rotation 2 (30 ms), 2 turns. After rotation 3 (45 ms), 3 turns.
- Rotation 4 starts at 45 ms and gives each process its fourth and last turn, in order. Process 1 runs from 45 to 50 ms and finishes at 50 ms. Process 2 runs from 50 to 55 ms and finishes at 55 ms. Process 3 runs from 55 to 60 ms and finishes at 60 ms.
- Check with the formula: the share is of the processor, so the last process needs ms of wall-clock time. Process 3 finishes at 60 ms, which matches.
Interpretation: the three need the same 20 ms of work, but they finish at 50, 55 and 60 ms, because round robin serves them one after another within each rotation. The last one served finishes last. A process that needed less work would finish sooner.
Problem: a heavy calculation (process A) is always ready. A text editor (process B) wakes up when you type a key, needs 1 ms of processor time to show the letter, then sleeps again. There are two processes, and the turn length is ms. What is the longest delay between a keypress and the letter appearing?
- The keypress wakes B, which becomes ready.
- Worst case: A has just started a turn. A keeps the processor for its full turn of 10 ms before the scheduler can switch.
- B then gets its turn and runs for its 1 ms of work.
- Total worst case: ms. Check with the formula: the wait is ms, then B's 1 ms.
Interpretation: 11 ms is below the threshold where a person notices lag, so the editor feels instant even while the calculation runs. Shorter turns cut the wait but cost more switching.
Try it: four processes are ready, B and three others, the turn length is 5 ms, and B needs 1 ms of processor time after it wakes. What is the longest delay before B shows its letter? Answer: the wait is the other three turns, ms, then B's 1 ms, so ms.
Common mistakes and tradeoffs
It is tempting to think that a program and a process are the same thing. In fact a program is a file on disk, and a process is one running copy of it. Two terminal windows running the same program are two processes, each with its own memory and its own program counter.
It is tempting to think that several processes run at the same moment. In fact a single processor core runs one instruction at a time. The rapid switching makes processes look simultaneous. Several cores can run several processes truly at once.
It is tempting to think that a slow process is slow because the processor is busy. In fact a process often waits for something else, such as a disk read or a network reply. While it waits it is in the waiting state, not running, and the processor serves other processes.
The tradeoff is isolation against cost. Separate processes protect each other: a crash or a bug in one cannot overwrite another's memory. But creating a process and switching between processes costs more than creating a Thread, which shares memory with its siblings. Sharing data between processes needs explicit channels, such as pipes or sockets. Threads share memory directly, which is faster but makes race conditions possible. Use separate processes when safety matters most, and threads when processes must cooperate closely on shared data.
History
Early computers ran one program at a time, and a person waited while it ran. In the early 1960s, time-sharing systems, such as the Compatible Time-Sharing System at MIT, let many users share one computer by switching among their programs, so each user had the feeling of a machine of their own.
In 1968 Edsger Dijkstra described a multiprogramming system built from cooperating sequential processes, a clear statement of the idea that a running program is a unit the system can pause and resume. The Unix systems of the early 1970s made the process the central object of the operating system, with a simple call that creates a new process from an existing one.
Processes caught on because they solved two problems at once: they let a machine serve many users, and they let one program fail without taking down the rest.
Recap
- A process is a running program: its instructions, its memory and the operating system's saved record of it.
- The processor runs one process at a time. The Operating System switches between processes by saving one's state and loading another's.
- Each process has its own program counter and registers, so each one runs as if it had the machine to itself.
- Round robin gives each of ready processes a fraction of the processor, and the longest wait is .
- What to read next: Operating System for how the switching is organised, Thread for the cheaper way to share work, and Virtual Memory for how each process gets its own memory.
Check yourself
QuestionWhat is the difference between a program and a process?Show answer
A program is the file of instructions stored on disk. A process is one running instance of a program, with its own memory, its own program counter and its own record in the operating system. Two windows running the same program are two processes.
QuestionTwo processes are ready. The turn length is 4 ms. Each process needs 12 ms of processor time. Ignoring switch time, when does the first process finish?Show answer
Each process needs turns. Round robin alternates, so the first process has turns at 0 to 4 ms, 8 to 12 ms and 16 to 20 ms. It finishes at 20 ms. The second process finishes at 24 ms.
QuestionWhy does the operating system have to save a process's registers and program counter before it gives the processor to another process?Show answer
Without the saved values, the next time the first process ran it would start from the wrong place with the wrong scratch values, and its results would be wrong. Saving them lets the process resume exactly where it stopped, so it cannot tell that it was paused.
QuestionA process is waiting for a disk read. Five other processes are ready, and the turn length is 10 ms. Why does the waiting process not slow down the others, and how long can it wait once its data arrives?Show answer
A waiting process is not ready, so the scheduler skips it and gives the processor to the ready processes. Once the data arrives it becomes ready, and in the worst case it must wait for the other ready processes' turns. With five other ready processes, that is up to 5 turns of 10 ms, which is 50 ms, before its own turn begins.
Sources
- Process (computing) (Wikipedia). https://en.wikipedia.org/wiki/Process_(computing)
- Time-sharing (Wikipedia). https://en.wikipedia.org/wiki/Time-sharing
- Operating system (Wikipedia). https://en.wikipedia.org/wiki/Operating_system