操作系统内存块分配算法

最先适应算法

cpp 复制代码
//最先适应算法 
#include <iostream>
#include <vector>

using namespace std;

class FreeBlock{  //块节点 
public:
	int id;
	int size;
	FreeBlock* pre;
	FreeBlock* next;
	FreeBlock(int _id = 0, int _size = 0) {
		id = _id;
		size = _size;
		pre = next = nullptr;	
	}
};

class BlockList{  //块链 
public:
	FreeBlock* head;  //头节点不存储信息 
	BlockList(){
		head = new FreeBlock();
		head->next = head;
		head->pre = head; 
	}
};

//void clearList(BlockList& list) {
//	FreeBlock* p = list.head->next;
//	while (p != list.head) {
//		FreeBlock* tmp = p;
//		p = p->next;
//		delete tmp;
//	}
//	list.head->next = list.head;
//	list.head->pre = list.head;
//}

void initList(BlockList& list, vector<int> v) {
//	clearList(list);
	FreeBlock* head = list.head;
	for (int i = 0; i < v.size(); ++i ) {
		FreeBlock* node = new FreeBlock(i + 1, v[i]);  //分配第 i + 1 个块的大小为 v[i]
		//循环链表构建 
		node->pre = head->pre;
		node->next = head;
		head->pre->next = node;
		head->pre = node;
	}
	list.head = head;
}

void allocSize(FreeBlock* block, int request){
	block->size -= request;
	cout << "当前请求为 " << request << "k, 分配到了 " << block->id << " 区" << endl; 
}

void first_fit(BlockList& list, int request) {
	FreeBlock* p = list.head->next;
	
	while (p != list.head) {
		if (p->size >= request) {
			allocSize(p, request);
			return ;
		}
		p = p->next;
	}
	
	cout << "当前请求为 " << request << "k, 空闲空间大小不够,无法分配" << endl;
}


int main (void) {
	vector<int> freeSizes = {10, 4, 20, 18, 7, 9, 12, 15};
//    vector<int> request1 = {12, 10, 9};
//    vector<int> request2 = {12, 10, 15, 18};
	cout << "输出需要分配的空间个数: ";
	int n; cin >> n;
	vector<int> requestList(n);
	for (int i = 0; i < n; ++i ) {
		cout << "需要分配的大小为: ";
		cin >> requestList[i];
		cout << endl; 
	}
	cout << endl;
    BlockList list;
    initList(list, freeSizes);
    for (auto x : requestList) {
		first_fit(list, x);
	}
	cout << endl; 
	cout << "分配后的空闲区序列为: " << endl;
	cout << "块号      空闲空间" << endl;
	FreeBlock* p = list.head->next;
	int i = 0;
	while (p != list.head) {
		cout << p->id << "          " << freeSizes[i++] << '(' << p->size << ')' << endl;
		p = p->next; 
	}
	cout << endl; 
} 

//free     10, 4, 20, 18, 7, 9, 12, 15 
//       3 12 10 9
//       4 12 10 15 18

最佳适应算法

cpp 复制代码
//最佳适应算法 
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

class FreeBlock{  //块节点 
public:
	int id;
	int size;
	FreeBlock* pre;
	FreeBlock* next;
	FreeBlock(int _id = 0, int _size = 0) {
		id = _id;
		size = _size;
		pre = next = nullptr;	
	}
};

class BlockList{  //块链 
public:
	FreeBlock* head;  //头节点不存储信息 
	BlockList(){
		head = new FreeBlock();
		head->next = head;
		head->pre = head; 
	}
};

//void clearList(BlockList& list) {
//	FreeBlock* p = list.head->next;
//	while (p != list.head) {
//		FreeBlock* tmp = p;
//		p = p->next;
//		delete tmp;
//	}
//	list.head->next = list.head;
//	list.head->pre = list.head;
//}

void initList(BlockList& list, vector<int> v) {
//	clearList(list);
	FreeBlock* head = list.head;
	for (int i = 0; i < v.size(); ++i ) {
		FreeBlock* node = new FreeBlock(i + 1, v[i]);  //分配第 i + 1 个块的大小为 v[i]
		//循环链表构建 
		node->pre = head->pre;
		node->next = head;
		head->pre->next = node;
		head->pre = node;
	}
	list.head = head;
}

void allocSize(FreeBlock* block, int request){
	block->size -= request;
	cout << "当前请求为 " << request << "k, 分配到了 " << block->id << " 区" << endl; 
}

void best_fit(BlockList& list, int request) {
	FreeBlock* p = list.head->next;
	FreeBlock* best = nullptr;
	int bestSize = INT_MAX;
	
	while (p != list.head) {
		if (p->size >= request && p->size < bestSize) {
			best = p;
			bestSize = p->size;
		}
		p = p->next;
	}
	
	if (best != nullptr) {
		allocSize(best, request);	
		return ; 
	}
	
	cout << "当前请求为 " << request << "k, 空闲空间大小不够,无法分配" << endl;
}


int main (void) {
	vector<int> freeSizes = {10, 4, 20, 18, 7, 9, 12, 15};
//    vector<int> request1 = {12, 10, 9};
//    vector<int> request2 = {12, 10, 15, 18};
	cout << "输出需要分配的空间个数: ";
	int n; cin >> n;
	vector<int> requestList(n);
	for (int i = 0; i < n; ++i ) {
		cout << "需要分配的大小为: ";
		cin >> requestList[i];
		cout << endl; 
	}
	cout << endl;
    BlockList list;
    initList(list, freeSizes);
    for (auto x : requestList) {
		best_fit(list, x);
	}
	cout << endl; 
	cout << "分配后的空闲区序列为: " << endl;
	cout << "块号      空闲空间" << endl;
	FreeBlock* p = list.head->next;
	int i = 0;
	while (p != list.head) {
		cout << p->id << "          " << freeSizes[i++] << '(' << p->size << ')' << endl;
		p = p->next; 
	}
	cout << endl; 

} 

//free     10, 4, 20, 18, 7, 9, 12, 15 
//       3 12 10 9
//       4 12 10 15 18

最坏适应算法

cpp 复制代码
//最坏适应算法 
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

class FreeBlock{  //块节点 
public:
	int id;
	int size;
	FreeBlock* pre;
	FreeBlock* next;
	FreeBlock(int _id = 0, int _size = 0) {
		id = _id;
		size = _size;
		pre = next = nullptr;	
	}
};

class BlockList{  //块链 
public:
	FreeBlock* head;  //头节点不存储信息 
	BlockList(){
		head = new FreeBlock();
		head->next = head;
		head->pre = head; 
	}
};

//void clearList(BlockList& list) {
//	FreeBlock* p = list.head->next;
//	while (p != list.head) {
//		FreeBlock* tmp = p;
//		p = p->next;
//		delete tmp;
//	}
//	list.head->next = list.head;
//	list.head->pre = list.head;
//}

void initList(BlockList& list, vector<int> v) {
//	clearList(list);
	FreeBlock* head = list.head;
	for (int i = 0; i < v.size(); ++i ) {
		FreeBlock* node = new FreeBlock(i + 1, v[i]);  //分配第 i + 1 个块的大小为 v[i]
		//循环链表构建 
		node->pre = head->pre;
		node->next = head;
		head->pre->next = node;
		head->pre = node;
	}
	list.head = head;
}

void allocSize(FreeBlock* block, int request){
	block->size -= request;
	cout << "当前请求为 " << request << "k, 分配到了 " << block->id << " 区" << endl; 
}

void best_fit(BlockList& list, int request) {
	FreeBlock* p = list.head->next;
	FreeBlock* best = nullptr;
	int bestSize = INT_MIN;
	
	while (p != list.head) {
		if (p->size >= request && p->size > bestSize) {
			best = p;
			bestSize = p->size;
		}
		p = p->next;
	}
	
	if (best != nullptr) {
		allocSize(best, request);	
		return ; 
	}
	
	cout << "当前请求为 " << request << "k, 空闲空间大小不够,无法分配" << endl;
}


int main (void) {
	vector<int> freeSizes = {10, 4, 20, 18, 7, 9, 12, 15};
//    vector<int> request1 = {12, 10, 9};
//    vector<int> request2 = {12, 10, 15, 18};
	cout << "输出需要分配的空间个数: ";
	int n; cin >> n;
	vector<int> requestList(n);
	for (int i = 0; i < n; ++i ) {
		cout << "需要分配的大小为: ";
		cin >> requestList[i];
		cout << endl; 
	}
	cout << endl;
    BlockList list;
    initList(list, freeSizes);
    for (auto x : requestList) {
		best_fit(list, x);
	}
	cout << endl; 
	cout << "分配后的空闲区序列为: " << endl;
	cout << "块号      空闲空间" << endl;
	FreeBlock* p = list.head->next;
	int i = 0;
	while (p != list.head) {
		cout << p->id << "          " << freeSizes[i++] << '(' << p->size << ')' << endl;
		p = p->next; 
	}
	cout << endl; 

} 

//free     10, 4, 20, 18, 7, 9, 12, 15 
//       3 12 10 9
//       4 12 10 15 18
相关推荐
先吃饱再说3 小时前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰5 小时前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术7 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六10 小时前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术10 小时前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize11 小时前
初识DFS 与 BFS:递归、队列与图遍历
算法
罗西的思考1 天前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
美团技术团队1 天前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法
To_OC2 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode