操作系统内存块分配算法

最先适应算法

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
相关推荐
科学实验家10 小时前
二叉树的几道题
算法
abcy07121311 小时前
flink state实例
大数据·算法·flink
qizayaoshuap11 小时前
# [特殊字符] 密码生成器 — 鸿蒙ArkTS安全算法与密码强度评估系统
java·算法·安全·华为·harmonyos
良木林11 小时前
滑动窗口 - LeetCode hot 100
javascript·算法·leetcode·双指针·滑动窗口
2401_8414956411 小时前
【数据结构】B*树
数据结构·c++·b树·算法·删除·插入·三分分裂
QXWZ_IA13 小时前
**电力登高作业高空失保实时监管:千寻智能安全带高挂低用监测方案**
人工智能·科技·算法·能源·智能硬件
爱刷碗的苏泓舒13 小时前
FFRT:固定失败率比率检验、宽巷模糊度固定及 Ratio Test
算法·相位偏差·模糊度固定·ppp-ar·ffrt·ratio test·wlnl
小园子的小菜13 小时前
深入理解 JVM 垃圾回收:从对象判定、回收算法到经典收集器全解析
jvm·算法
2601_9545267513 小时前
【硬核长文】从卡门涡街物理方程到边缘网关温压补偿算法:工业蒸汽测控实战,深度解密靠谱的涡街流量计厂家有哪些
算法
吞下星星的少年·-·13 小时前
牛客技能树:区间翻转
算法·滑动窗口