C++操作系统之循环首次适应(NF)算法

该文章展示了一个使用C++实现的NF算法,用于内存管理。它将内存分为空闲区和申请区,通过双循环查找策略尝试为申请区的节点分配内存。程序首先初始化空闲区和申请区,然后通过FFAlgorithm函数进行分配,最后输出分配后的空闲区和已分配的申请区状态。
摘要由CSDN通过智能技术生成

NF算法:
分两个区,一个空闲区,一个申请区。
逻辑:
申请区的下一块是申请区上一块在空闲区结束的位置开始查找。说白了双循环就是了
代码区:

#include<iostream>
#include<list>
using namespace std;
struct LCapacityNode
{
	int startAddress;
	int size;
	int endAddress;
	string state;
};


list<LCapacityNode>capacityNode;
list<LCapacityNode>needCapacityNode;

void showData(list<LCapacityNode>& l) {
	cout << "起始地址\t" << "容量大小\t" << "尾地址\t" << "\t状态" << endl;
	for (list<LCapacityNode>::iterator it = l.begin(); it != l.end(); it++) {
		cout << it->startAddress << "\t\t" << it->size << "\t\t" << it->endAddress << "\t\t" << it->state << endl;
	}
}

void InitListCapacity() {
	LCapacityNode L1 = { 0,1023,L1.startAddress + L1.size,"空闲区" };
	LCapacityNode L2 = { L1.startAddress + L1.size + 1,2047,L2.startAddress + L2.size,"空闲区" };
	LCapacityNode L3 = { L2.startAddress + L2.size + 1,3071,L3.startAddress + L3.size,"空闲区" };
	LCapacityNode L4 = { L3.startAddress + L3.size + 1,4095,L4.startAddress + L4.size,"空闲区" };
	LCapacityNode L5 = { L4.startAddress + L4.size + 1,5119,L5.startAddress + L5.size,"空闲区" };
	LCapacityNode L6 = { L5.startAddress + L5.size + 1,6143,L6.startAddress + L6.size,"空闲区" };
	capacityNode.push_back(L1);
	capacityNode.push_back(L2);
	capacityNode.push_back(L3);
	capacityNode.push_back(L4);
	capacityNode.push_back(L5);

	cout << "----------------------初始空闲区----------------------" << endl;
	showData(capacityNode);
	cout << "------------------------------------------------------" << endl;
}


void InitNeedCapacityNode() {
	cout << "请输入要申请的节点个数:";
	int sum;
	cin >> sum;
	LCapacityNode L;
	for (int i = 1; i <= sum; i++) {
		//只需要输入申请容量即可
		L.startAddress = 0;
		L.endAddress = 0;
		cout << "请输入第" << i << "个节点的所需容量:";
		cin >> L.size;
		L.state = "未分配";
		needCapacityNode.push_back(L);
	}
	cout << "----------------------初始申请区----------------------" << endl;
	showData(needCapacityNode);
	cout << "------------------------------------------------------" << endl;
}



void FFAlgorithm() {
	for (list<LCapacityNode>::iterator it = capacityNode.begin(); it != capacityNode.end(); it++) {
		for (list<LCapacityNode>::iterator jt = needCapacityNode.begin(); jt != needCapacityNode.end(); jt++) {
			//如果有满足的数据
			if ((it->size >= jt->size) && (jt->state == "未分配")) {
				//把首地址给到需要分配的
				jt->startAddress = it->startAddress;
				//目标地址的尾地址
				jt->endAddress = jt->startAddress + jt->size;
				//更新区域的首地址
				it->startAddress += jt->size + 1;
				//把当前地址的容量减少
				it->size -= jt->size;
				//改变状态
				jt->state = "已分配";
			}
		}
	}
	cout << "----------------------所剩空闲区----------------------" << endl;
	showData(capacityNode);
	cout << "------------------------------------------------------" << endl;

	cout << "---------------------NF算法完成区---------------------" << endl;
	showData(needCapacityNode);
	cout << "------------------------------------------------------" << endl;
}

int main() {
	InitListCapacity();
	InitNeedCapacityNode();
	FFAlgorithm();
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值