⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 内存分配.cpp

📁 装入作业:采用最先适应算法。输入要装入的作业的大小
💻 CPP
字号:
/*
 程序说明:
 装入作业:采用最先适应算法。输入要装入的作业的大小,程序查找合适位置插入;若空间不足,给出"内存不足"的提示;若总的剩余空间足够,但分配不连续,则提示整理内存;若作业总数已达最大值,在插入时给出提示,要求撤除不需要的作业。
 撤除作业:需要输入要撤除的作业的起始地址,程序找到目标表目后,将其状态置空,并检查其相邻表目,若为空,则合并之;若输入的首地址没有在表中找到,则提示错误;空闲表目不能再次被撤除;起始地址为零的表目为操作系统,不能被撤除。
 整理内存:将所有的已分配表目向低地址端移动,将其连成一片,所有空闲表目将被合并到表尾的"空表目"。
 print() 函数:用于输出空闲分区表。

 部分变量说明:
 wlon 作业长度 ;whadr   起始地址 ;
 maxl 数组长度 ;nowlong 当前作业的总长度 ;
 i、j 、k 循环控制变量
 状态表示:  1:已分配   0:空闲   -1:空表目
 */
///////////////////////////////////////////////////////////
#include<iostream.h>
#include<iomanip.h>

struct works //表目结构
{
	int hadr;		//起始地址
	int lon;		//长度
	int state;		//状态
};
// 函数定义 //
bool load_works(works [],int &,int &);
bool remove_works(works [],int &,int &);
void print(works [],int,int);
void tidy_memory(works [],int &);
// *****   ***** //
const int MAX=2000;	//最大总长度(内存容量)
const int MAXWORKS=20;	//最多支持20个作业
void main()
{
	works w[MAXWORKS];	//最多支持20个作业
	int choice,maxl,nowlong;
	// ***装入操作系统*** //
	cout<<"装入操作系统.................."<<endl;
	w[0].hadr=0; 
	w[0].lon=1; 
	w[0].state=1;
	// *****   ***** //
	maxl=0;		//数组长度标志
	nowlong=1;	//当前总长度
	print(w,maxl,nowlong);	//输出空闲分区表
	
	while(true)	//开始循环
	{
		cout<<" 1.装入作业;2.撤除作业;3.整理内存;0.退出程序"<<endl;
		cout<<"CHOICE # > ";
		cin>>choice;
		if(choice==0)break;	//退出程序
		if(choice==1)		//装入作业
		{
			if(maxl==MAXWORKS-1) //作业数已达最大值
			{
				cout<<"\n警告!作业数已达最大值,请撤除多余的作业以释放空间!\n"<<endl;
				continue;
			}
			if(load_works(w,maxl,nowlong))
				print(w,maxl,nowlong);	//输出空闲分区表
		}
		else
		if(choice==2)		//撤除作业
		{
			if(remove_works(w,maxl,nowlong))
				print(w,maxl,nowlong);	//输出空闲分区表
		}
		else
		if(choice==3)		//整理内存
		{
			tidy_memory(w,maxl);
			print(w,maxl,nowlong);	//输出空闲分区表
		}
		else continue;
	}//while(true)
}//main()
////////////////////////////////////////////////////////////
//装入作业
bool load_works(works w[],int & maxl,int & nowlong)
{
	int wlon,i,j;
	cout<<"作业长度 > ";
	cin>>wlon;			//作业长度

	if(wlon>MAX-nowlong)
	{
		cout<<"\t***>>>>> 警告! 内存不足! <<<<<***"<<endl;
		return false;
	}

	for(i=0;i<=maxl;i++)
	{
		if(w[i].state==1)continue;//表目非空
		if(w[i].lon<wlon)continue;//表目长度太小

		if(w[i].lon==wlon)	//表长等于作业长度
		{
			w[i].state=1;	//修改表目状态
			nowlong=nowlong+wlon;//修改当前内存容量
			return true;
		}

		for(j=maxl;j>i;j--)			//表长大于作业长度
			w[j+1]=w[j];			//w[i]以后表目后移一格
		//拆分w[i]并装入作业
		w[i+1].hadr=w[i].hadr+wlon;	//修改w[i+1]首地址
		w[i+1].lon=w[i].lon-wlon;	//修改w[i+1]长度
		w[i+1].state=0;				//修改w[i+1]状态
		w[i].lon=wlon;	//修改w[i]长度
		w[i].state=1;	//修改w[i]状态
		maxl++;			//数组长度加1
		nowlong=nowlong+wlon;//修改当前内存容量
		return true;
	}//for(i=0;i<=maxl;i++)

	//原空闲表目不能满足要求或无空闲表目
	if(i>maxl)
	{
		if((MAX-(w[i-1].hadr+w[i-1].lon))<wlon)//表尾空闲长度不足
		{
			cout<<"\t***>>>>> 提示...内存需要整理... <<<<<***"<<endl;
			return false;
		}
		//新建表目
		w[i].hadr=w[i-1].hadr+w[i-1].lon;//起始地址
		w[i].lon=wlon;	//长度
		w[i].state=1;	//状态
		maxl++;			//数组长度标志加1
		nowlong=nowlong+wlon;//修改当前内存容量
	}
	return true;
}
///////////////////////////////////////////////////////////
//撤除作业
bool remove_works(works w[],int & maxl,int & nowlong)
{
	int whadr,i,j;
	cout<<"要撤除的作业的起始地址 & > ";
	cin>>whadr;
	if(whadr==0)
	{
		cout<<"\t***>>>>> 警告!操作系统不能被撤除! <<<<<***"<<endl;
		return false;
	}
	for(i=0;i<=maxl;i++)//查找目标表目
	{
		if(w[i].hadr==whadr)	//w[i]为符合的表目
		{
			if(w[i].state==0)
			{
				cout<<"\t***>>>>> 提示...表目已经为空... <<<<<***"<<endl;
				return false;
			}
			w[i].state=0;		//状态置空
			nowlong=nowlong-w[i].lon;//修改当前内存容量

			if(w[i+1].state==0)	//如果w[i+1]为空,合并w[i]、w[i+1]
			{
				w[i].lon=w[i].lon+w[i+1].lon;
				for(j=i+1;j<maxl;j++)//w[i]以后表目前移一格
				{
					w[j].hadr=w[j+1].hadr;
					w[j].lon=w[j+1].lon;
					w[j].state=w[j+1].state;
				}
				maxl--;		//数组长度标志减1
			}//if(w[i+1].state==0)

			if(w[i-1].state==0)	//如果w[i-1]为空,合并w[i]、w[i-1]
			{
				w[i-1].lon=w[i-1].lon+w[i].lon;
				for(j=i;j<maxl;j++)//w[i-1]以后表目前移一格
				{
					w[j].hadr=w[j+1].hadr;
					w[j].lon=w[j+1].lon;
					w[j].state=w[j+1].state;
				}
				maxl--;		//数组长度标志减1
			}//if(w[i-1].state==0)
			return true;
		}//if(w[i].hadr==whadr)
	}//for(i=0;i<=maxl;i++)
	if(i>maxl)
	{
		cout<<"\t***>>>>> 警告!地址输入错误! <<<<<***"<<endl;
		return false;
	}
	return true;
}
///////////////////////////////////////////////////////////
//输出空闲分区表
void print(works w[],int maxl,int nowlong)	
{
	int k;
	cout<<setw(10)<<"起始地址"<<setw(10)<<"长度"<<setw(10)<<"状态"<<endl;
	for(k=0;k<=maxl;k++)
		cout<<setw(10)<<w[k].hadr<<setw(10)<<w[k].lon<<setw(10)<<w[k].state<<endl;

	if((w[k-1].hadr+w[k-1].lon)<MAX)//输出空表目
	cout<<setw(10)<<w[k-1].hadr+w[k-1].lon
		<<setw(10)<<MAX-(w[k-1].hadr+w[k-1].lon)
		<<setw(10)<<"-1"<<endl;

	cout<<"当前作业总长度:"<<nowlong<<"\n当前空闲总长度:"<<MAX-nowlong
		<<"\n当前内存使用率:"<<(double(nowlong)/MAX)*100<<" %"<<endl;
}
///////////////////////////////////////////////////////////
//整理内存
void tidy_memory(works w[],int & maxl)
{
	int i,j;
	for(i=0;i<=maxl;i++)
	if(w[i].state==0)
	{
		for(j=i;j<maxl;j++)
		{
			w[j].hadr=w[j-1].hadr+w[j-1].lon;
			w[j].lon=w[j+1].lon;
			w[j].state=w[j+1].state;
		}
		maxl--;	//数组长度标志减1
	}
	cout<<" \t***... 内存整理完成 ...***"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -