3.25存储管理动态分区分配及回收算法
一、实验目的
通过构造分区描述器及编制动态分区分配与回收算法的模拟程序,了解分区管理在存储管理中的应用,体会不同分配算法的优缺点,掌握动态存储管理的基本技术,提高解决实际问题的能力。
二、实验要求
1、理解分区管理的基本概念
学生需理解分区管理在存储管理中的作用,包括动态分区分配和回收的基本原理。
2、构造分区描述器
使用C/C++语言定义分区描述器的数据结构,包括分区的首地址、大小和指针等信息,确保其能够准确描述存储分区的状态。
三、实验过程
1.准备
A. 查阅相关资料:
B. 初步编写程序:
C. 准备测试数据:
2.上机调试
3. 主要流程和源代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 定义分区描述器
struct Node {
int adr; // 分区首地址
int size; // 分区大小
Node* next; // 指向下一个分区的指针
Node(int a, int s) : adr(a), size(s), next(nullptr) {}
};
// 全局变量
Node* head1 = nullptr; // 空闲区队列首指针
Node* back1 = nullptr; // 指向释放区的指针
Node* assign = nullptr; // 指向申请的内存分区的指针
int freeSize = 0; // 用户申请存储区的大小
// 函数声明
void check(int, int); // 检查释放块合法性
Node* firstFit(int); // First Fit分配算法
Node* bestFit(int); // Best Fit分配算法
void firstFitAccept(int, int); // First Fit回收算法
void bestFitAccept(int, int); // Best Fit回收算法
void printFreeList(); // 打印空闲区队列
int main() {
// 初始化一个空闲区
head1 = new Node(0, 32767);
int choice;
cout << "请选择分配算法: 1. First Fit 2. Best Fit" << endl;
cin >> choice;
while (true) {
int op;
cout << "请选择操作: 1. 分配 2. 回收 3. 退出" << endl;
cin >> op;
if (op == 1) { // 分配
cout << "请输入申请存储区的大小: ";
cin >> freeSize;
if (choice == 1) {
assign = firstFit(freeSize);
} else if (choice == 2) {
assign = bestFit(freeSize);
}
if (assign) {
cout << "分配成功: 首址 " << assign->adr << " 大小 " << assign->size << endl;
} else {
cout << "分配失败: 空闲区不足" << endl;
}
} else if (op == 2) { // 回收
int adr, size;
cout << "请输入释放区的首址和大小: ";
cin >> adr >> size;
if (choice == 1) {
firstFitAccept(adr, size);
} else if (choice == 2) {
bestFitAccept(adr, size);
}
cout << "回收成功" << endl;
} else if (op == 3) { // 退出
break;
}
printFreeList(); // 每次操作后打印空闲区队列
}
return 0;
}
// First Fit分配算法
Node* firstFit(int size) {
Node* temp = head1;
while (temp) {
if (temp->size >= size) {
assign = temp;
temp->size -= size;
temp->adr += size;
return assign;
}
temp = temp->next;
}
return nullptr; // 分配失败
}
// Best Fit分配算法
Node* bestFit(int size) {
Node* best = nullptr;
Node* temp = head1;
while (temp) {
if (temp->size >= size && (!best || temp->size < best->size)) {
best = temp;
}
temp = temp->next;
}
if (best) {
assign = best;
best->size -= size;
best->adr += size;
}
return best; // 返回最佳匹配的空闲区
}
// First Fit回收算法
void firstFitAccept(int adr, int size) {
Node* temp = head1;
while (temp) {
if (temp->adr + temp->size == adr) { // 合并到当前空闲区
temp->size += size;
return;
}
temp = temp->next;
}
// 如果没有合适的空闲区,插入到空闲区队列
Node* newNode = new Node(adr, size);
newNode->next = head1;
head1 = newNode;
}
// Best Fit回收算法
void bestFitAccept(int adr, int size) {
Node* temp = head1;
while (temp) {
if (temp->adr + temp->size == adr) { // 合并到当前空闲区
temp->size += size;
return;
}
temp = temp->next;
}
// 如果没有合适的空闲区,插入到空闲区队列
Node* newNode = new Node(adr, size);
newNode->next = head1;
head1 = newNode;
}
// 打印空闲区队列
void printFreeList() {
cout << "空闲区队列情况:" << endl;
cout << "编号\t首址\t终址\t大小" << endl;
Node* temp = head1;
int index = 1;
while (temp) {
cout << index++ << "\t" << temp->adr << "\t" << temp->adr + temp->size << "\t" << temp->size << endl;
temp = temp->next;
}
}
4.遇到的主要问题和解决方法
①. **问题**:在动态分区分配中,First Fit和Best Fit算法的逻辑混淆,导致分配结果不正确。
- **解决方法**:明确两种算法的核心逻辑。First Fit从头开始查找第一个足够大的空闲区,Best Fit查找最小的足够大的空闲区。通过独立封装函数并添加调试输出,验证每一步的分配逻辑。
②. **问题**:空闲区回收时,分区合并逻辑复杂,容易出现错误。
- **解决方法**:在回收时,仔细检查释放区的首地址和大小,判断是否可以与相邻的空闲区合并。通过画图或模拟示例,逐步分析合并逻辑,并在代码中添加详细的注释和调试信息,确保回收后空闲区队列的正确性。
四、实验结果

浙公网安备 33010602011771号