操作系统实验:连续内存分配管理模拟

采用链表进行空闲区和已分配空间的管理

初始的内存大小为100000字节

采用最先适应分配算法分配内存

功能键

0 退出

1 为作业分配内存

输入:作业名 要求的内存大小

执行逻辑:若无满足要求大小的空闲区,则显示无法分配的消息并退出,否则依据最先适应分配算法找到空闲区,分配给作业(有可能需要对空闲区进行切割),并在已分配链表中进行登记

2 回收内存

输入:作业名

执行逻辑:在已分配链表中找到给定的作业名对应的项,将对应的内存回收,修改已分配链表和空闲区链表(有可能需要在空闲区链表中进行合并)

3 显示内存分配情况

已分配内存空间情况(作业名,起始地址, 长度)和空闲区情况(起始地址,长度)

#include<iostream>
using namespace std;
struct AllocatedSpace 
{ 
	int start_address;
	int length;
	char job;
	struct AllocatedSpace* next;
};
struct FreeSpace 
{
	int start_address;
	int length;
	struct FreeSpace* next;
};
struct AllocatedSpace* allocated_header;
struct FreeSpace* free_header;

void allocate() 
{
	char job;
	cin >> job;
	int mem;
	cin >> mem;
	FreeSpace* fs;
	for (fs = free_header; fs != NULL; fs = fs->next) 
	{
		if (fs->length >= mem)
			break;
	}
	if (fs == NULL) 
	{
		cout << "空间不足,无法分配"<<endl;
		return;
	}
	AllocatedSpace* as = new AllocatedSpace;
	as->start_address = fs->start_address;
	as->length = mem;
	as->job = job;
	as->next = NULL;
	if (allocated_header == NULL)
		allocated_header = as;
	else
	{
		AllocatedSpace* tail=allocated_header;
		while (1) 
		{
			if (tail->next == NULL)
			{
				tail->next = as;
				break;
			}
			tail = tail->next;
		}
	}
	fs->length = fs->length - mem;
	fs->start_address = fs->start_address + mem;
}

void recycle()
{
	char job;
	cin >> job;
	AllocatedSpace* as = allocated_header;
	AllocatedSpace* prve = allocated_header;
	for (; as != NULL; prve=as,as = as->next) {
		if (as->job == job)
		{
			if (as == allocated_header) 
				allocated_header = as->next;
			else
				prve->next = as->next;
			break;
		}
	}
	if (as == NULL)
	{
		cout << "该进程不存在";
		return;
	}
	FreeSpace* fs = free_header;
	FreeSpace* prve2 = free_header;
	for (; fs != NULL; prve2 = fs,fs = fs->next) {
		if (fs->start_address > as->start_address)
			break;
	}
	if (as->start_address + as->length == fs->start_address&& prve2->start_address + prve2->length != as->start_address)
	{
		fs->start_address = as->start_address;
		fs->length = as->length + fs->length;
		return;
	}
	if (as->start_address + as->length != fs->start_address && prve2->start_address + prve2->length == as->start_address)
	{
		prve2->length = prve2->length + as->length;
		return;
	}
	if (as->start_address + as->length == fs->start_address && prve2->start_address + prve2->length == as->start_address)
	{
		prve2->length = prve2->length + as->length+fs->length;
		prve2->next = fs->next;
		return;
	}
	if (as->start_address + as->length != fs->start_address && prve2->start_address + prve2->length != as->start_address)
	{
		FreeSpace* newNode = new FreeSpace;
		newNode->start_address = as->start_address;
		newNode->length = as->length;
		newNode->next = fs;
		if (fs == free_header)
			free_header = newNode;
		else
			prve2->next = newNode;
		return;
	}
}
void output() {
	cout << "分配表" << endl;
	for (AllocatedSpace* as = allocated_header; as != NULL; as = as->next)
		cout << as->job << "  " << as->start_address <<"  " << as->length << endl;
	cout << "空闲表" << endl;
	for (FreeSpace* fs = free_header; fs != NULL; fs = fs->next)
		cout<< fs->start_address << "  " << fs->length << endl;
}
int main()
{
	free_header = new FreeSpace;
	free_header->start_address = 0;
	free_header->length = 100000;
	free_header->next = NULL;
	allocated_header = new AllocatedSpace;
	allocated_header = NULL;
	int size = 100000;
	while (1)
	{
		cout << "0.退出" << endl;
		cout << "1.分配" << endl;
		cout << "2.回收" << endl;
		cout << "3.情况" << endl;
		int choice;
		int flag = 0;
		cin >> choice;
		switch (choice) 
		{
		case 0:
			flag = 1;
			break;
		case 1:
			allocate();
			break;
		case 2:
			recycle();
			break;
		case 3:
			output();
			break;
		default:
			cout << "请输入正确的指令";
			break;
		}
		if (flag == 1)
			break;
	}
	cout << "退出管理"<<endl;
	return 0;
}

测试案例:

结果如下:

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值