C++操作系统之最坏适应(WF)算法

WF算法
分两个区一个空闲区一个申请区
WF逻辑:
空闲区容量从大到小排序,每次有申请区的某一块成功申请到内存,就重新排序一次
代码区:

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


list<LCapacityNode>capacityNode;
list<LCapacityNode>needCapacityNode;
//每次按容量从大到小排序
bool Compare_Size(LCapacityNode L1, LCapacityNode L2) {
	return L1.size > L2.size;
}

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);
	capacityNode.sort(Compare_Size);
	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() {
	list<LCapacityNode>endlist;
	int size = needCapacityNode.size();
	while (size != 0) {
		int count = 0;
		for (list<LCapacityNode>::iterator it = capacityNode.begin(); it != capacityNode.end(); it++) {
			//如果有满足的数据
			if (it->size >= needCapacityNode.front().size) {
				//把首地址给到需要分配的
				needCapacityNode.front().startAddress = it->startAddress;
				//目标地址的尾地址
				needCapacityNode.front().endAddress = needCapacityNode.front().startAddress + needCapacityNode.front().size;
				//更新空闲区域的首地址
				it->startAddress += needCapacityNode.front().size + 1;
				//把当前地址的容量减少
				it->size -= needCapacityNode.front().size;
				//改变状态
				needCapacityNode.front().state = "已分配";
				endlist.push_back(needCapacityNode.front());
				needCapacityNode.pop_front();
				capacityNode.sort(Compare_Size);
				break;
			}
			count++;
		}
		//如果记录的次数跟空闲容量的数量一样则代表第一个数据没有空闲区域给他
		if (count == capacityNode.size()) {
			endlist.push_back(needCapacityNode.front());
			needCapacityNode.pop_front();//把这个不能分配的弹出,让下一个去查找能不能分配
		}
		size--;
	}
	cout << "----------------------所剩空闲区----------------------" << endl;
	showData(capacityNode);
	cout << "------------------------------------------------------" << endl;

	cout << "---------------------WF算法完成区---------------------" << endl;
	showData(endlist);;
	cout << "------------------------------------------------------" << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值