serversim
LearnSimulatorTemplatesPricing
…
…
Open menu
Learn↗Simulator↗Templates↗Pricing↗
My DocumentsDocs
Contents/Chapter 55 / 16

Processes — the first way to share a CPU

Dozens of programs are open but the machine has only a few cores — so how do they all appear to run at once? Let many customers take very fast turns at one counter and, from outside, it looks simultaneous.

See this first in FoundationsCan a computer do something else while it waits?→

Dozens of programs are open on your machine right now. With one core, how can they all appear to run at once? By switching very quickly. This chapter covers what a process is and why the OS goes to such lengths to isolate them.

① A program is not a process

A program is a file on disk — dead. Run it and the OS loads it into memory, sets up a stack and heap, and points the PC at the first instruction. That living thing is a process. Run the same program twice and you get two processes with separate memory.

② Address space — each gets its own map

Every process gets its own stack and heap, and the OS shows each one a private map (virtual memory) in which "all of memory is yours". Address 100 in A and address 100 in B are different physical memory, so A cannot corrupt B. That isolation is the point of processes.

③ Time-sharing — switch fast, look simultaneous

Inside the OS a scheduler decides who runs next. When a process waits on IO another goes on immediately; if nobody yields, the OS forces a switch every few milliseconds. Watch the CPU0 chip hop between rows below — not simultaneous, just fast.

④ Isolation has a price

Creating a process means building a whole memory space. Sharing data requires OS-mediated IPC. Switching swaps memory maps. Those costs are exactly why threads appear in chapter 8.

⑤ What creating a process actually costs

To create a process the OS must build an address space, set up page tables, and prepare kernel structures such as the file descriptor table. That is why process creation is far more expensive than the thread creation we will see next. And yet that same preparation is why one broken process leaves the others untouched. The reason it is expensive and the reason it is safe are the same reason.

⑥ Why processes are still used

Being heavy did not make processes obsolete. Browsers isolate tabs into processes and containers take the process boundary as their unit for exactly this reason. When a crash on one side must not take everything down, isolation is worth paying for. Heavy is not the same as wrong — what matters is what you are buying with the price.

SpeedPrograms

Try it in the simulator

  1. 1.In the 'three-tier basics' preset, NGINX, Spring, and MySQL are each their own process (node). At the default 1,000 RPS the inspector shows Spring at 63% CPU and MySQL at 11% — fully separate resources.
  2. 2.Drag the client node's RPS slider — the two CPU gauges move independently, at different rates. That is what it means for them to be separate processes.
Try it in the simulator →

Terms in this chapter

process
A running program with its own memory space and flow of execution.
address space
The memory map one process sees. Independent per process.
virtual memory
The OS trick that shows each process a private memory map — the basis of isolation.
scheduler
The OS component that decides who gets the CPU next.
time-sharing
Slicing CPU time so several processes take turns.
IPC
Inter-process communication — needed because processes cannot share variables directly.
page table
The per-process map from virtual addresses to physical memory.
file descriptor
A per-process number referring to an open file or socket.

Why it evolved this way

Early machines ran one program at a time, and the expensive CPU sat idle whenever that program waited for tape or disk. Loading several programs at once and interleaving them removed the waste, and isolation grew up alongside it so they could not wreck each other.

A process = one isolated memory space + one flow of execution. Not simultaneous — taking turns.

The remaining problem

But what does "switching" actually mean? How does a stopped program resume later, and when does the CPU actually rest? →

← Previous

Chapter 4. Why memory fills up — and GC

Next →

Chapter 6. When does the CPU rest — interrupts and context switches