内存管理 —— 最坏适应算法

目录

实验三  内存管理

一、实验目的

二、实验内容

三、代码段

四、效果展示


实验三  内存管理

备注:大二(下)操作系统实验三

一、实验目的

加深对内存管理的认识,进一步掌握内存的分配、回收算法的思想

二、实验内容

实验环境:DEV C++

编写程序模拟实现内存的动态分区法存储管理

内存空闲区使用空闲分区链管理,采用最坏适应算法从空闲分区链中寻找空闲区进行分配

内存回收时假定不做与相邻空闲区的合并

假定系统的内存共640K,初始状态为操作系统本身占用64K

三、代码段

#include
#include
using namespace std;

unordered_set S;

struct FreeNode
{
	int address;
	int len;
	FreeNode * next;
};

struct BusyNode 
{
	string name;
	int address;
	int len;
	BusyNode * next;
};

FreeNode * free_head = NULL;
BusyNode * busy_head = NULL;
BusyNode * busy_tail = NULL;

void start()
{
	int ALL = 640, SYSTEM_BLOCK = 64, REMAIN = ALL-SYSTEM_BLOCK;
	cout << "系统内存共" << ALL << "KB,初始状态为操作系统占用" << SYSTEM_BLOCK << "KB,内存剩余" << REMAIN << "KB" << endl << endl;
	
	BusyNode * p = new BusyNode;
	p -> name = "系统占用块SYSTEM_BLOCK";
	p -> address = 0;
	p -> len = SYSTEM_BLOCK;
	p -> next = NULL;
	S.insert("系统占用块SYSTEM_BLOCK");
	busy_head = p;
	busy_tail = p;
	
	FreeNode * ptr = new FreeNode;
	ptr -> address = SYSTEM_BLOCK;
	ptr -> len = REMAIN;
	ptr -> next = NULL;
	free_head = ptr;
}

pair createJob()
{
	string name;   
	int require;
	cout << "请输入作业名:" << endl;    
	cin >> name;
	while(S.count(name))
	{
		cout << "您输入的作业名已被使用,请重新输入!" << endl;
		cin >> name;
	}
	S.insert(name);
	cout << "请输入作业大小(单位:KB)" << endl;
	cin >> require;
	
	pair p = make_pair(name,require);
	return p;
}

void insert_into_suitable_position(FreeNode * ptr)
{
	if(free_head == NULL)
	{
		free_head = ptr;
		ptr->next = NULL;
		return ;
	}
	
	FreeNode * u = free_head;
 	FreeNode * front_u = NULL;
 	
 	while(u!=NULL  &&  u->len > ptr->len)
	  {
	  	 front_u = u;
	  	 u = u->next;
	  }
	if(front_u)
	  {
	  	 front_u->next = ptr;
	  	 ptr->next = u;
	  }
	if(u == free_head) 
	{
		ptr->next = free_head;
		free_head = ptr;
	}
}

void insert_into_BusyLink(BusyNode * p)
{
	p->next = NULL;
	busy_tail->next = p;
	busy_tail = p;
}

void requireMemo(string name, int require)
{
	if((free_head && free_head->len < require)  ||  ! free_head)   cout << "内存不足,内存分配失败!" << endl;
	
	else
	  {
	  	 int address = free_head->address;
	  	 
	  	 if(free_head->len == require)
	  	 {
	  	 	auto p = free_head;
	  	 	free_head = free_head -> next;
	  	 	delete p;
		 }
		 
		 if(free_head->len > require)
		 {
		 	auto ptr = free_head;
		 	
		 	free_head = free_head -> next;
		 	
		 	ptr->address += require;
		 	ptr->len -= require;
		 	insert_into_suitable_position(ptr);
		 }
		 
		 BusyNode * p = new BusyNode;
		 p->name = name;
		 p->address = address;
		 p->len = require;
		 insert_into_BusyLink(p);
		 
		 cout << "作业创建成功!" << endl;
	  }
}

void freeMome()
{
	string name;
	cout << "请输入您需要回收的作业名" << endl;
	cin >> name;
	
	if(name == "系统占用块SYSTEM_BLOCK") cout << "无法回收系统占用块!" << endl;
	
	else if(! S.count(name)) cout << "作业名不存在,作业回收失败!" << endl;
	
	else
	  {
	  	 int address, len;
	  	 for(BusyNode * i=busy_head, * front_i=NULL;   i!=NULL;   front_i=i, i=i->next)
	  	   if(i->name == name)
	  	     {
	  	     	address = i->address;
	  	     	len = i->len;
	  	     	
	  	     	if(front_i) front_i->next  =  front_i->next->next;
	  	     	else busy_head = busy_head->next;
	  	     	
	  	     	S.erase(i->name);
	  	     	delete i;
	  	     	
	  	     	break;
			 }
			 
		  FreeNode * ptr = new FreeNode;
		  ptr->address = address;
		  ptr->len = len;
		  insert_into_suitable_position(ptr);
		  cout << "作业回收成功!" << endl;
	  }
}

void showFreeLink()
{
	int cnt = 0;
	cout << "空闲分区详情:" << endl;
	if(free_head == NULL) cout << "空闲分区为空!" << endl << endl;
	else
	{
		for(FreeNode * i=free_head; i!=NULL; i=i->next)
		  cout << cnt++ << "号分区,起始地址:" << i->address << ",大小:" << i->len << endl;
		  
		cout << endl;
	}
}

void showBusyLink()
{
	cout << "内存分配详情:" << endl;
	for(BusyNode * i=busy_head; i!=NULL; i=i->next)
	  cout << "作业名:" << i->name << ",起始地址:" << i->address << ",大小:" << i->len << endl;
		  
	cout << endl;
}

int main()
{
	start();
	
	while(true)
	{
		int choice; 
		cout << "当前系统采用最坏适应算法,请选择模拟选项:" << endl;
		cout << "1、创建作业" << endl;
		cout << "2、回收作业" << endl;
		cout << "3、内存详情" << endl;
		cout << "4、退出系统" << endl << endl;
		cin >> choice;
		
		switch(choice)
		{
			case 1: 
			  {
			  	 auto p = createJob();
			  	 requireMemo(p.first, p.second);
			  	 showBusyLink();
			  	 break;
			  }
			case 2: freeMome(); showFreeLink(); break; 
			case 3: showFreeLink(); showBusyLink(); break; 
			case 4: exit(0);
			default: cout << "您的选择有误,请重新选择!" << endl;
		}
	}
	
	return 0;
} 

四、效果展示

 内存管理 —— 最坏适应算法_第1张图片

你可能感兴趣的:(课程设计与实验,操作系统,最坏适应算法)