操作系统接口
引入| English | 释义 | 拼音 |
|---|---|---|
| process/ˈprəʊses/ | 进程 | jìn chéng |
| thread | 线程 | xiàn chéng |
| file/faɪl/ | 文件 | wén jiàn |
| context switch/ˈkɒntekst swɪtʃ/ | 上下文切换 | shàng xià wén qiè huàn |
| virtual memory/ˈvɜːtʃuːəl ˈmeməri/ | 虚拟内存 | xū nǐ nèi cún |
| page table/peɪdʒ ˈteɪbl/ | 页表 | yè biǎo |
| TLB | 快表 | kuài biǎo |
| page fault/peɪdʒ fɒlt/ | 缺页 | quē yè |
| system call | 系统调用 | xì tǒng diào yòng |
Many programs, one machine
- A laptop runs a browser, music and an editor at once on a few processor cores. None of them can read another's memory, and none talks to the disk directly.
- The operating system makes this possible. It shares the machine safely, and programs use it through a small set of services.
Processes and threads
- A process 进程 is a program plus its execution state: its own address space, registers, PC and open files.
- A thread 线程 runs inside a process and shares that process's memory. Two processes do not share memory unless they ask to.
Two threads of the same process share that process's memory.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Threads live inside one address space. Separate processes each get their own.
The context switch
- To share one core, the OS does a context switch 上下文切换: it saves the running process's registers and PC, switches the memory mapping, and loads the next process's state.
- The CPU caches are not saved. They are shared hardware, so after a switch they are simply cold for the new process.

A context switch does NOT save —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Caches are shared hardware and are just cold after a switch. The saved state is the registers, the PC and the mapping.
Virtual memory
- With virtual memory 虚拟内存, each process sees its own private addresses. Page tables 页表 map virtual pages to physical frames, and the TLB 快表 caches recent translations.
- It serves three needs: isolation (no process reads another's pages), the illusion of more memory than is installed, and fixed addresses for every program.
Virtual memory gives —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Three needs, one abstraction; page tables and the TLB do the mapping.
Page faults are normal
- A page fault 缺页 means the page is not in physical memory right now. The OS loads it from disk, updates the page table and runs the instruction again.
- This is demand paging, a normal part of operation. A crash comes from touching an address that is not mapped at all.
A page fault is usually —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
In demand paging the fault is the mechanism that brings data in.
Put the steps of a page fault in order.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The program never notices, apart from the delay.
System calls
- A system call 系统调用 is how a program asks the kernel for help:
open,read,write,fork,execve. Each one traps into the kernel, runs with kernel privileges, then returns. - Files 文件 are named streams of bytes.
fd = open(path, O_RDONLY)thenread(fd, buf, 4096)reads the first 4 KiB, and Python'sopenwraps the same calls.
Match each system call to its job.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Each call crosses into the kernel and comes back.
Worked example: translating an address
- Pages are 4096 bytes. Virtual address 9000: page $= \lfloor 9000 / 4096 \rfloor = 2$, offset $= 9000 - 8192 = 808$.
- The page table maps page 2 to frame 5, so the physical address is $5 \times 4096 + 808 = 21288$.
Pages are 4096 bytes. Which page number holds virtual address 13000?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
13000 ÷ 4096 = 3.17, so page 3 (it starts at 12288).
Pages are 4096 bytes and page 3 maps to frame 2. What physical address does virtual address 13000 become?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Offset = 13000 − 12288 = 712. Physical = 2 × 4096 + 712 = 8904.
Three common slips
- Mixing up processes and threads. Processes have their own memory; threads share it.
- Thinking
malloctouches the disk. It hands out memory, not file space. - Reading every page fault as a crash. Most are the OS fetching a page, as designed.
Recap
- Process = program + state; a context switch saves registers and PC and switches the mapping.
- Virtual memory: page tables and the TLB map virtual to physical, for isolation, illusion and convenience.
- A page fault is a fetch, not a failure; system calls are the doorway into the kernel.