操作系统内存块分配算法

最先适应算法

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
相关推荐
江畔柳前堤34 分钟前
大语言模型分布式训练:从并行策略到万卡工程的系统梳理
人工智能·分布式·深度学习·算法·目标检测·机器学习·语言模型
Forever Nore2 小时前
LeetCode 4 寻找两个正序数组的中位数 - 二分
算法·leetcode
罗西的思考3 小时前
【OpenClaw具身硬件】MiniClaw 阅读笔记---(1)基础
人工智能·算法·机器学习
蛋先生DX4 小时前
大模型参数存储格式揭秘:BF不是男朋友
深度学习·算法·llm
爱跳舞的烤冷面5 小时前
自学嵌入式第22天(数据结构——哈希)
数据结构·算法·哈希算法
猎嘤一号5 小时前
博弈论(Game Theory)的理论、算法与工程
人工智能·算法·安全·博弈论
月光船幽幽5 小时前
影子模式下保护 logits 不被修改
人工智能·python·算法
马拉AI5 小时前
腾讯开源 Agent 记忆系统,AI“换对话就忘”的问题有了新解法(附安装使用教程)
人工智能·算法·开源·科研
地平线开发者7 小时前
征程6工具链模型X86推理方式说明
算法
地平线开发者7 小时前
【征程6】校准量化中HistogramObserver解析
算法