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

What a computer is made of

While your program sits waiting for a file, is the CPU idle too? Picture an office with a single clerk: the moment one visitor leaves to fetch a document, the clerk calls the next one in.

Before the details, the big picture: what actually happens when a computer runs a program, and what each part does. One sentence matters most here — the CPU is always running somebody's code. Step through the diagram below.

① Three parts: CPU, memory, devices

The CPU computes and executes instructions; memory holds the code and data of running programs; disks, networks and keyboards are devices. The CPU knows nothing on its own — it must fetch instructions from memory.

② What "running a program" means

A program file on disk is dead data. Running it means the OS loads it into memory, prepares its memory space, and points the CPU at the first instruction. From then on the CPU executes it line by line.

③ The OS is referee and broker

If programs could drive devices or read each other's memory directly, chaos. So only the OS may do those things and programs ask for them. The OS also decides who gets the CPU and for how long.

④ The most important misunderstanding

"My program stopped waiting for the disk" and "the CPU is idle" are entirely different statements. The moment your program stalls, the OS puts another program on the CPU. The CPU truly rests only when nothing is runnable.

⑤ What to watch in the diagram

Keep your eye on the label inside the CPU box: Program A, then the kernel on a system call, then Program B while A waits, then the kernel again on an interrupt, and idle only when nothing can run. That label is never empty.

Step 1/11Speed
CPU
계산 · 명령 실행
지금 실행 중
Program A
PC A · line 1
메모리 — 올라와 있는 프로그램들
Program A · web server실행 중
1id = parse(req)◀ 실행 중
2user = db.query(id)
3return toJson(user)
Program B · image resizewaiting its turn
1img = decode(raw)
2resize(img)
3save(img)
Kernel (OS)대기
1handleSyscall()
2schedule()
3handleInterrupt()
장치 · 디스크idle

A computer has three main parts: the CPU that computes, memory holding the running programs, and devices such as disks. Look inside memory — several programs are loaded at once. But there is only one CPU, so only one of them executes at this instant.

Try it in the simulator

  1. 1.Open the 'three-tier basics' preset — the client node's RPS defaults to 1,000. Click the Spring (Tomcat) card and see CPU at 63%, then MySQL at 11%.
  2. 2.Drag the RPS slider — that knob decides how much work the computer gets, and the CPU gauge on each card shows the result live.
Try it in the simulator →

Terms in this chapter

CPU
Fetches, decodes and executes instructions — the only part that actually computes.
memory (RAM)
Holds the code and data of running programs; lost on power off.
I/O device
Disk, network and so on — far slower than the CPU, and works on its own.
operating system
The manager program that shares resources and performs privileged work.
system call
How a program asks the OS to do something.
interrupt
A device signalling the CPU that its work is done.

The CPU is always running someone's code — your program stalling is not the CPU idling.

The remaining problem

So how exactly does the CPU run one line of code? Next we go down to individual instructions →

Next →

Chapter 2. How a line of code runs