操作系统内存块分配算法

最先适应算法

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
相关推荐
一水鉴天1 分钟前
问题分类的三元组映射体系与求解方法论 20260901(千问)
人工智能·算法·机器学习
breeze jiang3 分钟前
LangChain Memory 实战:用 InMemoryChatMessageHistory 管理多轮对话
前端·算法·langchain
地平线开发者15 小时前
【模型轻量化专题】衡量模型轻量性的指标
算法
学习星球18 小时前
OFDM技术精讲:正交性推导、循环前缀原理与80行Python链路仿真(附实测数据)
算法·面试·前端框架
alphaTao1 天前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
Interview Aid1121 天前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
顶点多余1 天前
那些在算法中适合巩固的知识点---1
java·前端·算法
罗西的思考1 天前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
hahaha60161 天前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
多弗朗皮卡丘2 天前
算法详解4:买卖股票的最佳时机系列(上)
算法