操作系统内存块分配算法

最先适应算法

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 小时前
【力扣100题】57.合并区间
算法·leetcode
玉树临风ives1 小时前
atcoder ABC 458 题解
数据结构·c++·算法
如竟没有火炬1 小时前
有序矩阵中第K小的元素
数据结构·线性代数·算法·leetcode·矩阵·深度优先
叁散1 小时前
ESP32智能闹钟系统实验报告
单片机·嵌入式硬件·算法
Realdagongzai1 小时前
Linux 6.19.10 内核调度器算法详解
linux·学习·算法·spring·kernel
洛水水2 小时前
【力扣100题】63.最小覆盖子串
算法·leetcode
AllData公司负责人2 小时前
亲测丝滑,体验跃迁|AllData通过集成开源项目RustFS,多模态数据存储新范式
java·大数据·数据库·算法·数据分析·rustfs
磊 子2 小时前
AVL树的讲解
数据结构·算法
Trouvaille ~2 小时前
【Redis篇】Hash 哈希:字段级操作与对象存储的最佳实践
数据库·redis·后端·算法·缓存·哈希算法·键值对