> For the complete documentation index, see [llms.txt](https://lfool.gitbook.io/operating-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lfool.gitbook.io/operating-system/di-si-zhang-wen-jian-guan-li/5.-kong-xian-fen-qu-guan-li.md).

# 5. 空闲分区管理

## 1. 存储空间的划分 & 初始化

**划分：**&#x5C06;物理磁盘划分为一个个的**文件卷**（**逻辑卷、逻辑盘**），如 C 盘、D 盘等

**初始化：**&#x628A;每个文件卷划分为**目录区、文件区**

* **目录区：**&#x5B58;放文件目录信息（FCB）、用于磁盘存储空间管理的信息
* **文件区：**&#x7528;于存放文件数据

## 2. 空闲表法

适用于**连续分配方式**

![](/files/-MFWB43ugg3vJIH-4_DG)

### 2.1 分配磁盘块

与内存管理中动态分区分配类似，为一个文件**分配连续的存储空间**。同样可采用**首次适用**、**最佳适应**、**最坏适应**等算法来决定要为文件分配哪个区间

### 2.2 回收磁盘块

与内存管理中动态分区分配类似，当回收某个存储区时需要有四种情况：

* 回收区前后都没有空闲区
* 回收区前面有空闲区
* 回收区后面有空闲区
* 回收区前后都有空闲区

**注意：合并**

## 3. 空闲链表法

### 3.1 空闲盘块链

以**盘块**为单位组成一条链

![](/files/-MFWCjS9eVMMWzfXyDH-)

操作系统保存着**链头**、**链尾**指针

**分配方法：**&#x82E5;某文件申请 K 个盘块，则从链头依次摘下 K 个盘块分配，并修改空闲链的链头指针

**回收方法：**&#x56DE;收的盘块依次挂到链尾，并修改空闲链的链尾指针（适用于离散分配的物理结构）

### 3.2 空闲盘区链

以**盘区**为单位组成一条链

![](/files/-MFWCofVXCApqjZPgsWp)

操作系统保存着**链头**、**链尾**指针

**分配方法：**&#x82E5;某文件申请K 个盘块，则可以采用首次适应、最佳适应等算法，从链头开始检索，按照算法规则找到一个大小符合要求的空闲盘区，分配给文件。若没有合适的连续空闲块，也可以将不同盘区的盘块同时分配给一个文件，注意分配后可能要修改相应的链指针、盘区大小等数据

**回收方法：**&#x82E5;回收区和某个空闲盘区相邻，则需要将回收区合并到空闲盘区中。若回收区没有和任何空闲区相邻，将回收区作为单独的一个空闲盘区挂到链尾（离散分配、连续分配都适用）

## 4. 位示图法

每个二进制位对应一个盘块。在下图中，**“0” 代表盘块空闲， “1” 代表盘块已分配**。位示图一般用连续的“字”来表示，如本例中一个字的字长是 16 位，字中的每一位对应一个盘块。因此**可以用（字号，位号）对应一个盘块号**

![](/files/-MFWEtCytcEgCb7opTad)

**（字号，位号）= （i，j）的二进制对应的 盘块号 b = n \* i + j**

**分配方法：**&#x82E5;文件需要 K 个文件

1. 顺序扫描位示图，找到 K 个相邻或不相邻的 “0”
2. 根据字号、位号算出对应的盘块号，将相应盘块分配给文件
3. 将相应位设置为 “1”

**回收方法：**

1. 根据回收的盘块号计算出对应的字号、位号
2. 将相应二进制位设为 “0”

## 5. 成组链接法


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://lfool.gitbook.io/operating-system/di-si-zhang-wen-jian-guan-li/5.-kong-xian-fen-qu-guan-li.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.
