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

📄 分页式存储管理.cpp

📁 分页式存储管理 用位示图表示
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdlib.h"
#include "iostream.h"
#include "stdio.h"
#include "malloc.h"
#include "conio.h"
#include "windows.h"

typedef int datatype;
typedef struct
{
	datatype PhyAddress;           //对应位示图的号
	datatype Sign;                 //标志位
}PageTable;

typedef struct
{
	datatype PageAddress;
}Memory;

PageTable *p;                   //全局变量
PageTable *p2;
Memory *q;                    //全局变量
Memory *q2;                 //OPT控制变量

int *buffer;
int *max;
int length,size,num;
int  Visited;                      //访问次数
int  As;                      //缺页次数 

int bitmap[8][8]={{1,1,1,1,1,1,1,1},         //位示图  //全局变量
	{0,1,0,1,0,1,0,1},
	{0,0,0,1,1,0,1,0},
	{1,1,0,0,1,0,1,0},
	{0,1,0,1,1,1,0,1},
	{0,0,0,1,1,1,0,0},
	{1,1,1,1,1,0,0,0},
	{0,0,0,1,1,1,0,0}};

void Help();
void LRU(int m,int n,int k,int a[8][8]);
void FIFO(int m,int n,int k,int b[8][8]);
void Found();
void Cls();
void OPT();


int main()
{

	Found();
	return 0;
}


void Help()
{
	cout<<"\n\n\n\t\t\t欢迎使用该分页式存储管理系统!";
}


void FIFO(int length,int num,int size,int a[8][8])            //先进先去算法
{
	int w,h,offset;                      //w为输入的值,h为页表页数,offset为偏移量
	/*
	int  Visited=0;           //访问次数
	int  As=0;         //缺页次数 
	*/
	char ch;
	bool fig=false;
	int bbs=0;
	bool fig2=false;
	Visited=0;           
	As=0; 
	cout<<"\n\n欢迎使用FIFO算法----->"<<endl;
    cout<<"\n\t\t\t---所使用的位示图---"<<endl;
	for(int m=0;m<8;m++)
	{
		cout<<"\n\t\t\t   ";
		for(int n=0;n<8;n++)
		{
			cout<<a[m][n]<<" ";
			if(n==7)
				cout<<"\n";
		}
	}
	do{
	cout<<"\n\n(想退出请输入'#',继续请输入'!',完全退出请输入'@',清屏请输入'c'):";
	cin>>ch;
	/*
//-----------------------------------------------测试
	cout<<"此处已被使用!"<<endl;
//-----------------------------------------------
*/
	if(ch=='#')
	{
		if(fig2)
		{
		cout<<"\n\n\t\t----------以下是OPT算法----------"<<endl;
		OPT();
		cout<<"\n\t\t----------OPT输出完毕!----------"<<endl;
		Found();
		}
		else
			Found();
	}
	else if(ch=='@')
	{
		if(fig2)
		{
		cout<<"\n\n\t\t----------以下是OPT算法----------"<<endl;
		OPT();
		cout<<"\n\t\t----------OPT输出完毕!----------"<<endl;
	    exit(0);
		}
		else
			exit(0);
	}
	     else if(ch=='c'||ch=='C')
			  Cls();
		      else
				  break;
	}while(ch);

	while(ch=='!')
	{
//		cout<<"输入成功!"<<endl;
	cout<<"请输入地址:";
	cin>>w;
	h=w/(size*1024);	
	buffer[bbs]=h;
	bbs++;
	offset=w%(size*1024);
	if(h>=length)                 
//------------------------------------------------
		cout<<"地址溢出!"<<endl;             //溢出
//------------------------------------------------
	else
	{
		if(p[h].Sign==0)
		{
			{
				for(int i=0;i<8;i++)
				{
					for(int j=0;j<8;j++)
						if(a[i][j]==0)
						{
							p[h].PhyAddress=(8*i+j);          //将位示图值赋给页
							p[h].Sign=1;                      //赋值标志位则为1
							a[i][j]=1;                    //位示图置1
							fig=true;
							break;
						}
						if(fig==true)
						{
							fig=false;          //此处的fig一定要归位
							break;
						}
				}
				Visited++;
				As++;
			}
			if(q[num-1].PageAddress!=-1)
			{
				p[q[0].PageAddress].PhyAddress=NULL;
				p[q[0].PageAddress].Sign=0;
				for(int i=0;i<num-1;i++)
					q[i].PageAddress=q[i+1].PageAddress;
				q[num-1].PageAddress=h;
			}
			else
			{
				for(int i=0;i<num;i++)
				{
					if(q[i].PageAddress==-1)
					{
						q[i].PageAddress=h;
						break;
					}
				}
			}
		}
		else if(p[h].Sign==1)
			Visited++;

//************************************************************************************
//************************************************************************************输出信息
	cout<<"\n\n\t\t----------系统使用情况----------"<<endl;
	cout<<"\n输入地址:"<<w<<"\t\t页号:"<<h<<"\n偏移地址:"<<offset<<"\t\t物理地址:"<<offset+size*1024*p[h].PhyAddress<<"\n页面大小:"<<size<<"k";
	cout<<"\t\t访问数目:"<<Visited<<"\n缺页数目:"<<As;
	cout<<"\t\t缺页率:"<<(int)(As*100)/Visited<<"%"<<endl;

	cout<<"\n页表信息:"<<endl;                      //输出页表信息
	cout<<"\t------------"<<"-------"<<endl;
	cout<<"\t|          |"<<"      |"<<endl;
	if(p[0].PhyAddress!=NULL)
		cout<<"0"<<"\t|    "<<p[0].PhyAddress<<"\t   |"<<"  "<<p[0].Sign<<"\t  |"<<endl;
	else
		cout<<"0"<<"\t|    "<<"\t   |"<<"  "<<p[0].Sign<<"\t  |"<<endl;
	cout<<"\t|          |"<<"      |"<<endl;
	cout<<"\t------------"<<"-------"<<endl;
	for(int i=1;i<length;i++)
	{
		cout<<"\t|          |"<<"      |"<<endl;
		if(p[i].PhyAddress!=NULL)
			cout<<i<<"\t|    "<<p[i].PhyAddress<<"\t   |"<<"  "<<p[i].Sign<<"\t  |"<<endl;
		else
			cout<<i<<"\t|    "<<"\t   |"<<"  "<<p[i].Sign<<"\t  |"<<endl;
//		cout<<i<<"\t|    "<<p[i].PhyAddress<<"\t   |"<<"  "<<p[i].Sign<<"\t  |"<<endl;
		cout<<"\t|          |"<<"      |"<<endl;
		cout<<"\t------------"<<"-------"<<endl;
	}

	cout<<"\n内存信息:"<<endl;                        //输出内存信息
	for(int j=num-1;j>0;j--)
	{
		cout<<"\t------------"<<endl;
		cout<<"\t|          |"<<endl;
		if(q[j].PageAddress!=-1)
			cout<<j<<"\t|    "<<q[j].PageAddress<<"\t   |"<<endl;
		else
			cout<<j<<"\t|    "<<"\t   |"<<endl;
		cout<<"\t|          |"<<endl;
	}
	    cout<<"\t------------"<<endl;
		cout<<"\t|          |"<<endl;
		if(q[0].PageAddress!=-1)
			cout<<j<<"\t|    "<<q[0].PageAddress<<"\t   |"<<endl;
		else
			cout<<j<<"\t|    "<<"\t   |"<<endl;
		cout<<"\t|          |"<<endl;
		cout<<"\t------------"<<endl;
		cout<<"\n\t\t----------输出完毕!-------------\n"<<endl;

//************************************************************************************输出信息
//************************************************************************************


	}
	do{
		cout<<"\n\n(想退出请输入'#',继续请输入'!',完全退出请输入'@',清屏请输入'c'):";
		cin>>ch;
		if(ch=='#')
		{
			cout<<"\n\n\t\t----------以下是OPT算法----------"<<endl;
	    	OPT();
	    	cout<<"\n\t\t----------OPT输出完毕!----------"<<endl;
	    	Found();
		}
		else if(ch=='@')
		{
			cout<<"\n\n\t\t----------以下是OPT算法----------"<<endl;
	    	OPT();
	    	cout<<"\n\t\t----------OPT输出完毕!----------"<<endl;
	        exit(0);
		}
		     else if(ch=='c'||ch=='C')
				 Cls();
			      else
					  break;
	}while(ch);
}
}



void LRU(int length,int num,int size,int a[8][8])            //最近最久未使用算法
{
	int w,h,offset;                      //w为输入的值,h为页表页数,offset为偏移量
	/*
	int  Visited=0;           //访问次数
	int  As=0;         //缺页次数 
	*/
	char ch;
	bool fig=false;
	int bbs=0;
	bool fig2=false;
	Visited=0;         
	As=0;
	cout<<"\n\n欢迎使用LRU算法----->"<<endl;
    cout<<"\n\t\t\t---所使用的位示图---"<<endl;
	for(int m=0;m<8;m++)
	{
		cout<<"\n\t\t\t   ";
		for(int n=0;n<8;n++)
		{
			cout<<a[m][n]<<" ";
			if(n==7)
				cout<<"\n";
		}
	}
	do{
	cout<<"\n\n(想退出请输入'#',继续请输入'!',完全退出请输入'@',清屏请输入'c'):";
	cin>>ch;
		if(ch=='#')
		{
			if(fig2)
			{
			cout<<"\n\n\t\t----------以下是OPT算法----------"<<endl;
			OPT();
     		cout<<"\n\t\t----------OPT输出完毕!----------"<<endl;
    		Found();
			}
			else
				Found();
		}
		else if(ch=='@')
		{
			if(fig)
			{
			cout<<"\n\n\t\t----------以下是OPT算法----------"<<endl;
    		OPT();
    		cout<<"\n\t\t----------OPT输出完毕!----------"<<endl;
	        exit(0);
			}
			else
				exit(0);
		}
    	     else if(ch=='c'||ch=='C')
				 Cls();
			      else
	     			  break;
	}while(ch);

//	show(a);
	while(ch=='!')
	{
	cout<<"请输入地址:";
	cin>>w;
	h=w/(size*1024);	
	buffer[bbs]=h;
	bbs++;
	offset=w%(size*1024);
	if(h>=length)                 
//------------------------------------------------
		cout<<"地址溢出!"<<endl;             //溢出
//------------------------------------------------
	else
	{
		if(p[h].Sign==0)
		{
			{
				for(int i=0;i<8;i++)
				{
					for(int j=0;j<8;j++)
						if(a[i][j]==0)
						{
							p[h].PhyAddress=(8*i+j);          //将位示图值赋给页
							p[h].Sign=1;                      //赋值标志位则为1
							a[i][j]=1;                    //位示图置1
							fig=true;
							break;
						}
						if(fig==true)
						{
							fig=false;          //此处的fig一定要归位
							break;
						}
				}

⌨️ 快捷键说明

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