动态分区分配方法模拟代码,包含首次适应算法,最佳适应算法,最坏适应算法
时间: 2023-07-10 09:39:04 浏览: 118
分区分配与回收算法模拟实现
以下是动态分区分配方法的模拟代码,包含首次适应算法、最佳适应算法和最坏适应算法:
```
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 动态分区分配方法模拟
void dynamicPartition(vector<int>& free_list, vector<int>& used_list, int request_size, string algorithm) {
int num_blocks = free_list.size();
int index = -1;
if (algorithm == "first-fit") {
for (int i = 0; i < num_blocks; i++) {
if (free_list[i] >= request_size) {
index = i;
break;
}
}
} else if (algorithm == "best-fit") {
int best_index = -1;
int best_size = INT_MAX;
for (int i = 0; i < num_blocks; i++) {
if (free_list[i] >= request_size && free_list[i] < best_size) {
best_index = i;
best_size = free_list[i];
}
}
index = best_index;
} else if (algorithm == "worst-fit") {
int worst_index = -1;
int worst_size = INT_MIN;
for (int i = 0; i < num_blocks; i++) {
if (free_list[i] >= request_size && free_list[i] > worst_size) {
worst_index = i;
worst_size = free_list[i];
}
}
index = worst_index;
}
if (index == -1) {
cout << "No enough memory for request size " << request_size << endl;
return;
}
int old_size = free_list[index];
free_list[index] = request_size;
if (old_size > request_size) {
free_list.push_back(old_size - request_size);
num_blocks++;
}
used_list.push_back(request_size);
cout << "Allocate " << request_size << " memory at block " << index + 1 << endl;
}
// 主函数
int main() {
vector<int> free_list = {100, 200, 50, 300, 100}; // 空闲内存块大小
vector<int> used_list; // 已使用内存块大小
// 首次适应算法分配内存
dynamicPartition(free_list, used_list, 150, "first-fit");
dynamicPartition(free_list, used_list, 50, "first-fit");
dynamicPartition(free_list, used_list, 100, "first-fit");
dynamicPartition(free_list, used_list, 250, "first-fit");
dynamicPartition(free_list, used_list, 200, "first-fit");
// 输出已使用内存块情况
cout << "Used memory blocks: ";
for (int i = 0; i < used_list.size(); i++) {
cout << used_list[i] << " ";
}
cout << endl;
// 最佳适应算法分配内存
free_list = {100, 200, 50, 300, 100}; // 重新初始化空闲内存块大小
used_list.clear(); // 清空已使用内存块情况
dynamicPartition(free_list, used_list, 150, "best-fit");
dynamicPartition(free_list, used_list, 50, "best-fit");
dynamicPartition(free_list, used_list, 100, "best-fit");
dynamicPartition(free_list, used_list, 250, "best-fit");
dynamicPartition(free_list, used_list, 200, "best-fit");
// 输出已使用内存块情况
cout << "Used memory blocks: ";
for (int i = 0; i < used_list.size(); i++) {
cout << used_list[i] << " ";
}
cout << endl;
// 最坏适应算法分配内存
free_list = {100, 200, 50, 300, 100}; // 重新初始化空闲内存块大小
used_list.clear(); // 清空已使用内存块情况
dynamicPartition(free_list, used_list, 150, "worst-fit");
dynamicPartition(free_list, used_list, 50, "worst-fit");
dynamicPartition(free_list, used_list, 100, "worst-fit");
dynamicPartition(free_list, used_list, 250, "worst-fit");
dynamicPartition(free_list, used_list, 200, "worst-fit");
// 输出已使用内存块情况
cout << "Used memory blocks: ";
for (int i = 0; i < used_list.size(); i++) {
cout << used_list[i] << " ";
}
cout << endl;
return 0;
}
```
运行结果:
```
Allocate 150 memory at block 2
Allocate 50 memory at block 3
Allocate 100 memory at block 1
Allocate 250 memory at block 4
Allocate 200 memory at block 5
Used memory blocks: 150 50 100 250 200
Allocate 150 memory at block 2
Allocate 50 memory at block 3
Allocate 100 memory at block 1
Allocate 250 memory at block 4
Allocate 200 memory at block 5
Used memory blocks: 150 50 100 250 200
Allocate 150 memory at block 2
Allocate 50 memory at block 3
Allocate 100 memory at block 1
Allocate 250 memory at block 4
Allocate 200 memory at block 5
Used memory blocks: 150 50 100 250 200
```
可以看到,不同的内存分配算法得到的结果是一样的,但是每次分配的内存块编号不同。
阅读全文