Virtual memory, paging and segmentation · 虚拟内存、分页与分段
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| pages/ˈpeɪdʒɪz/ | 页 | yè |
| paging/ˈpeɪdʒɪŋ/ | 分页 | fēn yè |
| segmentation/ˌseɡmənˈteɪʃn/ | 分段 | fēn duàn |
| virtual address space/ˈvɜːtʃuːəl əˈdres speɪs/ | 虚拟地址空间 | xū nǐ dì zhǐ kōng jiān |
| frames/freɪmz/ | 页框 | yè kuāng |
| page table/peɪdʒ ˈteɪbl/ | 页表 | yè biǎo |
| page fault/peɪdʒ fɒlt/ | 缺页 | quē yè |
| swap file/swɒp faɪl/ | 交换文件 | jiāo huàn wén jiàn |
| thrashing/ˈθræʃɪŋ/ | 抖动 | dǒu dòng |
The disk light that means the machine has given up
- Open too many applications on a machine short of memory and something distinctive happens. Everything slows almost to a stop, and the disk light stops flickering and stays on.
- The processor is not busy. It is waiting, because the memory each program needs is on the disk, and fetching one page evicts another that is needed a moment later.
- The machine is doing nothing but moving pages. It has enough work and enough memory to do neither.
- This lesson is virtual memory, how paging 分页 and segmentation 分段 provide it, and how the same mechanism that makes memory look bigger can make a machine useless.
硬盘灯长亮意味着机器已经放弃
- 在内存吃紧的机器上打开太多程序,会出现一个很有辨识度的现象。一切几乎慢到停止,而硬盘灯不再闪烁,而是一直亮着。
- 处理器并不忙。它在等,因为每个程序需要的内存都在磁盘上,而取来一页就要赶走另一页——那一页片刻之后又要用。
- 机器除了搬页什么也没做。它的工作量和内存量,哪一样都不够。
- 这一课讲虚拟内存、分页(paging)和分段(segmentation)怎样实现它,以及让内存看起来更大的同一机制怎样能让机器变得毫无用处。
The virtual address space
- Each process gets its own virtual address space 虚拟地址空间: a clean, contiguous range of addresses starting at zero, which the OS maps to wherever the data really is.
- Three things follow. Each process sees a simple private space and need not know what else is running. Processes are protected from each other, since one cannot name an address in another's space. And the total memory in use can exceed the physical RAM, because part of it lives on disk.
- That isolation is a security boundary as much as a convenience.
虚拟地址空间
- 每个进程获得自己的虚拟地址空间(virtual address space):一段从零开始、干净连续的地址范围,由操作系统映射到数据真正所在的地方。
- 由此得出三件事。每个进程看到一个简单的私有空间,不必知道还有什么在运行。各进程彼此受保护,因为一个进程说不出另一个空间里的地址。而且使用中的内存总量可以超过物理 RAM,因为其中一部分住在磁盘上。
- 这种隔离既是便利,也是一道安全边界。
A benefit of virtual memory is that it: · 虚拟内存的一个好处是它:
Virtual memory gives each process a private space and lets the working set exceed installed RAM by using disk. · 虚拟内存给每个进程一个私有空间,并通过使用磁盘让工作集超过安装的 RAM。
What does giving each process a virtual address space achieve? Select all · 所有 that apply. · 给每个进程一个虚拟地址空间达成了什么?选出所有适用的。
Virtual memory buys simplicity, isolation and capacity. It costs speed, since part of the memory is really on disk. · 虚拟内存换来简单、隔离和容量。它以速度为代价,因为一部分内存实际在磁盘上。
Paging
- The virtual space is divided into fixed-size pages 页; physical memory into frames 页框 of the same size. A page table 页表 records which frame currently holds each page.
- When a process uses an address, the hardware looks up the page in the table and finds the frame. If the page is not in RAM, that is a page fault 缺页.
- On a page fault the OS reads the page from the swap file 交换文件 on disk into a free frame. If no frame is free, it must first evict a page, writing it back to disk if it has been changed.
Same-sized pages and frames, joined by a table
分页
- 虚拟空间被分成固定大小的页(pages);物理内存被分成同样大小的页框(frames)。页表(page table)记录当前哪个页框装着哪一页。
- 当进程使用一个地址时,硬件在表中查这一页并找到页框。如果该页不在 RAM 中,那就是缺页(page fault)。
- 发生缺页时,操作系统把该页从磁盘上的交换文件(swap file)读进一个空闲页框。如果没有空闲页框,它必须先换出一页——若该页被修改过,还要写回磁盘。

大小相同的页和页框,由一张表连接
In paging, memory is divided into: · 在分页中,内存被分成:
Paging uses fixed-size pages and frames, linked by a page table. (Variable sizes are segmentation.) · 分页用固定大小的页和帧,由一个页表连接。(可变大小是分段。)
A page fault occurs when: · 一次缺页发生,当:
The accessed page isn't in a frame, so the OS fetches it from the swap file (evicting another page if needed). · 被访问的页不在一个帧中,所以操作系统从交换文件取它(如果需要就驱逐另一个页)。
Worked example: what happens on a page fault
- A process accesses an address whose page is not in RAM. Describe what the OS does. [4]
- The hardware detects that the page is not present and raises a page fault interrupt, and the process is blocked.
- The OS finds a free frame; if there is none, it selects a page to replace, and writes it back to the swap file if it has been modified.
- It reads the required page from the swap file into that frame and updates the page table.
- The process is returned to the ready state and the instruction is retried. Four steps, four marks, and note the process was blocked throughout.
例题:缺页时发生什么
- 一个进程访问的地址所在的页不在 RAM 中。描述操作系统会做什么。[4]
- 硬件检测到该页不在,引发一次缺页中断,进程被置为阻塞。
- 操作系统寻找空闲页框;若没有,就选出一页替换,若它被修改过则写回交换文件。
- 它把所需的页从交换文件读进那个页框,并更新页表。
- 进程被送回就绪状态,该指令被重试。四步,四分,并且注意进程全程处于阻塞。
What happens on a page fault · 缺页时发生什么
Step through a page fault. When the program touches a page that isn't in RAM, the OS quietly fetches it from disk and updates the page table — so the program sees more memory than physically exists. · 逐步走过一次缺页。当程序接触一个不在 RAM 中的页时,操作系统悄悄地从磁盘取它并更新页表——所以程序看到的内存比物理存在的更多。
Put the OS's response to a page fault in order. · 把操作系统对缺页的处理按顺序排列。
Detect and block, make room, load, record, retry. A page fault is the normal mechanism, not an error. · 检测并阻塞、腾地方、载入、记录、重试。缺页是正常机制,不是错误。
Thrashing
- Choosing which page to replace matters, because the wrong choice means it will be needed again immediately.
- When there is too little RAM for the working set of the running processes, almost every access causes a page fault, and every fault evicts a page that is wanted again at once.
- The system then spends nearly all its time swapping pages instead of executing instructions. That is thrashing 抖动, or disk thrashing, and it is the state described at the top of this lesson.
- The cure is more RAM, or fewer processes; a faster processor does not help at all, because the processor is not the thing that is busy.
抖动
- 选择替换哪一页很要紧,因为选错就意味着它马上又要被用到。
- 当 RAM 对运行中进程的工作集来说太少时,几乎每次访问都引发缺页,而每次缺页又换出一个马上又要用的页。
- 系统于是把几乎全部时间花在换页而不是执行指令上。这就是抖动(thrashing),也叫磁盘抖动,正是本课开头描述的状态。
- 解决办法是加内存,或减少进程;换更快的处理器毫无用处,因为忙的根本不是处理器。
A machine slows almost to a stop and the disk runs constantly. What will fix it? · 一台机器几乎慢到停止,硬盘持续工作。什么能解决它?
This is thrashing: nearly every access faults and the processor waits. The processor is not the busy component, so speeding it up changes nothing. · 这是抖动:几乎每次访问都缺页,处理器在等。忙的不是处理器,所以让它更快毫无改变。
Segmentation
- Segmentation divides memory into variable-sized logical segments that match the program's own structure: the code, the stack, the heap, a large data array.
- Because a segment is a logical unit, it can carry its own permissions: the code segment can be read-only and executable, the stack read-write and not executable.
- The contrast the exam wants: pages are fixed-size and have no meaning to the program; segments are variable-sized and correspond to logical parts of it. Many real systems combine the two, paging within segments.
Segments are the program's own parts, not equal slices
分段
- 分段把内存分成大小可变的逻辑段,与程序自身的结构对应:代码段、栈、堆、一个大数据数组。
- 因为段是逻辑单元,它可以带自己的权限:代码段可以是只读且可执行的,栈可以是可读写且不可执行的。
- 考试要的对比是:**页大小固定、对程序没有意义;段大小可变、对应程序的逻辑部分。**许多真实系统把两者结合,在段内分页。

段是程序自己的组成部分,不是等分的切片
Match each memory-management term to its meaning. · 把每个内存管理术语与它的含义配对。
Paging = fixed pages; segmentation = logical variable units; a page fault triggers a swap; too many faults = thrashing. · 分页 = 固定的页;分段 = 逻辑的可变单元;一次缺页触发一次交换;太多缺页 = 抖动。
Segmentation divides memory into variable-sized logical units (code, stack, heap), each with its own permissions, whereas paging uses fixed-size pages. · 分段把内存分成可变大小的逻辑单元(代码、栈、堆),每个有它自己的权限,而分页用固定大小的页。
Segments match the program's logical structure; pages are uniform fixed-size blocks — some systems combine both. · 段匹配程序的逻辑结构;页是统一的固定大小块——一些系统结合两者。
Unlike pages, which are all the same size, segments are of ____ size and match the program's logical parts. · 与大小全都相同的页不同,段的大小是____的,与程序的逻辑部分对应。
A segment is the code, the stack or the heap, so it can carry its own permissions. A page is a physical division with no meaning to the program. · 一个段就是代码、栈或堆,所以它能带自己的权限。页是对程序没有意义的物理划分。
Worked example: paging against segmentation
- State two differences between paging and segmentation. [2]
- Pages are all of a fixed size, decided by the system; segments are of variable size, decided by the logical parts of the program.
- A page is a purely physical division with no meaning to the program; a segment corresponds to a logical unit such as the code or the stack, and can be given its own access permissions.
- A third if needed: paging can leave unused space inside the last page of an allocation, while segmentation leaves gaps of unusable space between segments.
例题:分页与分段的对比
- 说出分页和分段的两点区别。[2]
- 页全都是固定大小,由系统决定;段是可变大小,由程序的逻辑部分决定。
- 页是纯粹的物理划分,对程序没有意义;段对应一个逻辑单元,比如代码或栈,并且可以有自己的访问权限。
- 若还需要第三点:分页会在一次分配的最后一页内部留下未用空间,而分段会在段之间留下无法使用的空隙。
Marks that slip away
- Virtual memory does not create memory. It lets the total in use exceed physical RAM by keeping part of it on disk, at the cost of disk accesses.
- A page fault is not an error. It is the normal mechanism by which a page is brought in.
- Thrashing is spending more time swapping than executing, and the fix is more RAM or fewer processes, never a faster processor.
- Fixed size versus variable size, logically meaningful is the difference between paging and segmentation. Say both halves.
容易丢掉的分
- 虚拟内存不创造内存。它靠把一部分留在磁盘上,让使用中的总量超过物理 RAM,代价是磁盘访问。
- 缺页不是错误。它是把一页调进来的正常机制。
- 抖动是换页的时间多于执行的时间,解决办法是加内存或减少进程,绝不是更快的处理器。
- 固定大小对可变大小、有逻辑意义,这就是分页与分段的区别。两半都要说。
You've got it
- a virtual address space gives each process a private contiguous range, protects processes from each other, and lets memory in use exceed physical RAM
- paging: fixed-size pages mapped to frames by a page table; a page fault blocks the process while the OS evicts a page if necessary, loads from the swap file, updates the table and retries
- thrashing is more time spent swapping than executing; cure it with more RAM or fewer processes
- segmentation uses variable-sized segments matching the program's logical parts, each with its own permissions; many systems page within segments
你掌握了
- 虚拟地址空间给每个进程一段私有的连续范围,让各进程彼此受保护,并让使用中的内存超过物理 RAM
- 分页:固定大小的页由页表映射到页框;缺页让进程阻塞,操作系统必要时换出一页、从交换文件载入、更新表并重试
- 抖动是换页时间多于执行时间;用加内存或减少进程来解决
- 分段使用与程序逻辑部分对应的可变大小段,各有自己的权限;许多系统在段内分页