# 9.2 地址空间

**地址空间**（addressspace）是一个非负整数地址的有序集合：

$$
{0,1,2,\cdots}
$$

如果地址空间中的整数是连续的，那么我们说它是一个**线性地址空间**（linear address space）。为了简化讨论，我们总是假设使用的是线性地址空间。在一个带虚拟内存的系统中，CPU 从一个有 $$\small N=2^n$$ 个地址的地址空间中生成虚拟地址，这个地址空间称为**虚拟地址空间**（virtual addres sspace）：

$$
{0,1,2,\cdots,N—1}
$$

一个地址空间的大小是由表示最大地址所需要的位数来描述的。例如，一个包含$$\small N=2^n$$个地址的虚拟地址空间就叫做一个 n 位地址空间。现代系统通常支持 32 位或者 64 位虚拟地址空间。

一个系统还有一个**物理地址空间**（physical address space），对应于系统中物理内存的 M 个字节：

$$
{0,1,2,\cdots,M—1}
$$

M 不要求是 2 的幕，但是为了简化讨论，我们假设$$\small M=2^m$$ 。

地址空间的概念是很重要的，因为它清楚地区分了数据对象（字节）和它们的属性（地址）。一旦认识到了这种区别，那么我们就可以将其推广，允许每个数据对象有多个独立的地址，其中每个地址都选自一个不同的地址空间。这就是虚拟内存的基本思想。主存中的每字节都有一个选自虚拟地址空间的虚拟地址和一个选自物理地址空间的物理地址。

### 练习题 9.1

{% tabs %}
{% tab title="练习题 9.1" %}
完成下面的表格，填写缺失的条目，并且用适当的整数取代每个问号。

利用下列单位：$$\small\rm K=2^{10}$$（kilo，千），$$\small\rm M=2^{20}$$（mega，兆，百万），$$\small\rm G=2^{30}$$（giga，千兆，十亿），$$\small\rm T=2^{40}$$（tera，万亿），$$\small\rm P=2^{50}$$（peta，千千兆），或$$\small E = 2^{60}$$（exa，千兆兆）。

|   虚拟地址位数（n）   | 虚拟地址数（N）                   | 最大可能的虚拟地址                     |
| :-----------: | -------------------------- | ----------------------------- |
|       8       |                            |                               |
|               | $$\small 2^? = 64\rm K$$   |                               |
|               |                            | $$\small \rm 2^{32}-1=? G-1$$ |
|               | $$\small \rm 2^? = 256 T$$ |                               |
|       64      |                            |                               |
|  {% endtab %} |                            |                               |
| {% endtabs %} |                            |                               |

{% tabs %}
{% tab title="答案" %}
这道题让你对不同地址空间的大小有了些了解。曾几何时，一个 32 位地址空间看上去似乎是无法想象的大。但是，现在有些数据库和科学应用需要更大的地址空间，而且你会发现这种趋势会继续。在有生之年，你可能会抱怨个人电脑上那狭促的 64 位地址空间！

|   虚拟地址位数（n）   | 虚拟地址数（N）                             | 最大可能的虚拟地址                                 |
| :-----------: | ------------------------------------ | ----------------------------------------- |
|       8       | $$\small 2^8 = 256$$                 | $$\small 2^8 -1= 255$$                    |
|       16      | $$\small 2^{16} = 64\~\rm K$$        | $$\small 2^{16} -1= 64\~\rm K -1$$        |
|       32      | $$\small 2^{32} = 4\~\rm G$$         | $$\small 2^{32} -1 = 4\~\rm G -1$$        |
|       48      | $$\small \rm 2^{48} = 256\~\rm T$$   | $$\small \rm 2^{48} -1 = 256\~\rm T -1$$  |
|       64      | $$\small \rm 2^{64} = 16384\~\rm P$$ | $$\small \rm 2^{64} -1 = 16384\~\rm P-1$$ |
|  {% endtab %} |                                      |                                           |
| {% endtabs %} |                                      |                                           |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hansimov.gitbook.io/csapp/part2/ch09-virtual-memory/9.2-address-spaces.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
