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

📄 程国磊0507.txt

📁 利用最先适用法模拟实现内存管理; 初始条件: 随机产生内存的大小及每个进程对内存需求大小。 1.要求根据分配算法显示分区的使用情况。 2.要能处理内存回收的时候上下邻接区合并的问题。
💻 TXT
字号:
#include<iostream.h>   
#include<conio.h>   
#include<cstring>   
#include<stdlib.h>   

struct   Memory                           
{   
	int   size;            //分区容量     
	int   estate;          //分区状态     
	char  tenor[10];       //装入的进程名     
	int   FA;              //内存首地址     
	int   EA;              //内存末地址     
	Memory   *front;   
	Memory   *next;           
};   
void   unite(Memory *);   
void   initialize();   
int    check(char *);   
void   FFA();               //最先适应法(FFA)   
void   FFAinsert();         //FFA创建进程          
void   free();              //释放分区内的进程     
void   unite(Memory *hand); //空闲分区回收   
void   consult();           //查看内存状态   
void   list();              //主菜单      
void   cls();               //清屏     
Memory   *head;             //链表头指针     
Memory   *end;              //链表末指针     
int   sumtenor=0;           //装入内存容量之合   

void   initialize()         //初始化分区链表     
{   
	head=new  Memory;   
	end=head;   
	head->next=NULL;   
	head->front=NULL; 
	head->size=100;   
	head->FA=0;  
	head->EA=102399;   
	head->estate=0;   
	sumtenor=0;  
	
}  
//检测同名进程   
int check(char *str)   
{   
	int vable;   
	Memory *handl=head;   
	while(handl!=NULL)   
	{   
		vable=strcmp(handl->tenor,str);   
		if(vable==0)   
			return  0;   
		handl=handl->next;   
	}   
	return  1;   
}   

void FFA()         //最先适应算法     
{   
	int ii=1;
	Memory   *handl=head;   
	Memory   *PS;   
	Memory   *PEnd;   
	PS=new   Memory;   
	//输入进程信息   
	{   
		cout<<"请输入进程名:   ";   
		cin>>PS->tenor;   
		cout<<"请输入进程大小(单位:KB):   ";   
		cin>>PS->size;   
		PS->estate=1;   
	}   
	
	//判断进程   
	int   vc;   
	vc=check(PS->tenor);   
	if(vc==0)   
	{   
		cout<<"\n对不起!内存中已经存在进程"<<PS->tenor<<",进程不允许重名!"<<endl;   
		return ;   
	}     
    
	
	
	//判断容量     
	if(PS->size>(100-sumtenor))   
	{   
		cout<<"\n对不起!内存容量不够,无法分配"<<endl;   
		return ;   
	}   
	//直接分配为100K     
	if(PS->size==100)   
	{   
		PS->front=NULL;   
		PS->next=NULL;   
		PS->FA=0;   
		PS->EA=102399;   
		head=PS;   
		end=PS;   
		sumtenor=100;            
		return ;   
	}   
	while(handl!=NULL)   
	{   
		if(handl->estate==0)   
		{   
			if(handl->size==PS->size) //插入进程容量等于分区容量     
			{   
				if(handl==head)      //插入的是头指针   
				{       
					sumtenor=sumtenor+PS->size; //累加进程容量     
					PS->FA=head->FA;   
					PS->EA=head->EA;   
					PS->front=NULL;   
					PS->next=head->next;   
					head=PS;   
					head->next->front=PS;  
					return ;   
				}   
				else   if(handl->next!=NULL)  //插入指针在链表内     
				{   
					sumtenor=sumtenor+PS->size;   
					PS->FA=handl->FA;   
					PS->EA=handl->EA;   
					PS->front=handl->front;   
					PS->next=handl->next;   
					handl->front->next=PS;   
					handl->front=PS;           
					return ;   
				}   
				else   if(handl->next==NULL)    //插入指针在链尾     
				{   
					sumtenor=sumtenor+PS->size;   
					PS->FA=handl->FA;   
					PS->EA=handl->EA;   
					PS->front=handl->front;   
					PS->next=NULL;   
					end=PS;   
					handl->front->next=PS;     
					return ;                       
				}   
			}   
			if(handl->size>PS->size)     //插入进程容量小于内存分区容量     
			{  
				if(handl==head)           //插在头指针     
				{   
					sumtenor=sumtenor+PS->size;   
					PS->FA=0;   
					PS->EA=(PS->size)*1024-1;   
					PS->front=NULL;   
					PS->next=head;   
					head->front=PS;   
					head=PS;   
					PS->next->FA=(PS->EA)+1;   
					PS->next->size=PS->next->size-PS->size;   
					return ;   
					
				}   
				else   
				{   
					sumtenor=sumtenor+PS->size;   
					PS->front=handl->front;   
					PS->next=handl;   
					handl->front->next=PS;       //改变前结点指针     
					handl->front=PS;   
					PS->FA=PS->front->EA+1;   
					PS->EA=PS->FA+PS->size*1024-1;   
					PS->next->FA=PS->EA+1;   
					PS->next->size=PS->next->size-PS->size;  
					return ;   
				}   
			}   
        }   
		
        handl=handl->next;   
    }   
    
	cout<<"\n\n"   
		<<"对不起!虽然剩余内存容量足够,但是都是内存碎片,无法分配此进程!"<<endl;   
	return ;  
	
  }     
  
  //FFA创建进程   
  void   FFAinsert()   
  {   
      char chose;   
      do   
      {   
		  cout<<"\n";   
		  FFA(); 
		  cout<<"\n是否继续调入进程   Y/N?";
		  
		  cin>>chose;   
		  cout<<endl;   
      }while(chose=='y');  
	  list();  
  }    
  //释放分区内的进程     
  void   free()   
  {   
      Memory   *handl=head;   
      char   frn[10];   
      int   value;   
      cout<<"\n\n";   
      cout<<"请输入要释放的进程名:   ";     
      cin>>frn;   
      while(handl!=NULL)   
      {   
          value=strcmp(frn,handl->tenor);   
          if(value==0)   
          {   
			  handl->estate=0;   
			  *(handl->tenor)='\0';   
			  sumtenor=sumtenor-handl->size;   
			  cout<<"\n\n进程   "<<frn<<"   已经成功释放"<<endl;     
			  unite(handl);   
			  return;   
          }   
          handl=handl->next;   
      }     
      cout<<"对不起,内存中没有此进程"<<endl;   
  }     
  
  //空闲分区回收   
  void  unite(Memory *hand)   
  {   
	  
      if((hand==head)&&(hand==end))   
	  {   
		  cout<<"\n开始回收空闲分区....\n"<<endl;     
		  head->size=100;   
		  head->estate=0;   
		  head->FA=0;   
		  head->EA=102399;   
		  head->front=NULL;   
		  head->next=NULL;   
		  end=head;   
		  sumtenor=0;   
		  cout<<"\n\n回收分区完毕!!!\n"<<endl;   
		  return;       
	  }   
	  //排除头结点的后继以及尾结点的前驱不为空     
      if(((hand==head)&&(hand->next->estate==1))||((hand==end)&&(hand->front->estate==1)))   
          return;       
      
      if((hand==head)&&(head->next->next==NULL)&&(hand->next->estate==0))   
	  {   
		  cout<<"\n开始回收空闲分区....\n"<<endl;     
		  head->size=100;   
		  head->estate=0;   
		  head->FA=0;   
		  head->EA=102399;   
		  head->front=NULL;   
		  head->next=NULL;   
		  end=head;   
		  cout<<"\n回收空闲分区完毕!!!\n"<<endl;     
		  return;   
	  }   
      if((hand==end)&&(end->front->front==NULL)&&(end->front->estate==0))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;     
		  head->size=100;   
		  head->estate=0;   
		  head->FA=0;   
		  head->EA=102399;   
		  head->front=NULL;   
		  head->next=NULL;   
		  end=head;   
		  return;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;     
	  }   
      if((hand==head)&&(hand->next->next!=NULL)&&(hand->next->estate==0))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;     
		  hand->next->FA=0;   
		  hand->next->size=hand->next->size+hand->size;   
		  hand->next->front=NULL;   
		  head=head->next;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;     
		  return;   
	  }   
      if((hand==end)&&(end->front->front!=NULL)&&(hand->front->estate==0))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;     
		  end->front->EA=102399;   
		  end->front->size=end->front->size+end->size;   
		  end->front->next=NULL;   
		  end=end->front;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;     
		  return;           
	  }   
      if((head->next==hand)&&(head->estate==0)&&(end->front==hand)&&(end->estate==0))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;     
		  head->size=100;   
		  head->estate=0;   
		  head->FA=0;   
		  head->EA=102399;   
		  head->front=NULL;   
		  head->next=NULL;   
		  end=head;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;   
		  return;   
	  }   
      if((head->next==hand)&&(hand->front->estate==0)&&(hand->next->estate==0)&&(hand->next->next!=NULL))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;     
		  head->next->next->FA=0;   
		  head->next->next->size=head->size+head->next->size+head->next->next->size;   
		  head->next->next->front=NULL;   
		  head=head->next->next;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;   
		  return;           
	  }   
      if((end->front==hand)&&(hand->front->estate==0)&&(hand->next->estate==0)&&(end->front->front!=NULL))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;   
		  end->front->front->EA=102399;   
		  end->front->front->size=end->front->front->size+end->front->size+end->size;   
		  end->front->front->next=NULL;   
		  end=end->front->front;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;   
		  return;           
	  }   
      if((hand->front->estate==0)&&(hand->next->estate==1))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;   
		  hand->front->EA=hand->EA;   
		  hand->front->size=hand->front->size+hand->size;   
		  hand->front->next=hand->next;   
		  hand->next->front=hand->front;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;   
		  return;   
	  }         
      if((hand->front->estate==1)&&(hand->next->estate==0))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;   
		  hand->next->FA=hand->FA;   
		  hand->next->size=hand->next->size+hand->size;   
		  hand->front->next=hand->next;   
		  hand->next->front=hand->front;   
		  cout<<"\n\n回收空闲分区完毕!!!\n"<<endl;   
		  return;   
	  }   
      if((hand->front->estate==0)&&(hand->next->estate==0))   
	  {   
		  cout<<"\n\n开始回收空闲分区......\n"<<endl;   
		  hand->front->EA=hand->next->EA;   
		  hand->front->size=hand->front->size+hand->size+hand->next->size;   
		  hand->front->next=hand->next->next;   
		  hand->next->next->front=hand->front;   
		  return;   
	  }     
  }   
  //查看内存状态   
  void   consult()   
  {   
      Memory   *handl=head;   
      int   count=0;   
      while(handl!=NULL)   
      {   
          cout<<"\n";   
          cout<<"分区   "<<count++<<"   信息:"<<endl;   
          cout<<"容量:      "<<dec<<handl->size<<"   KB"<<endl;   
          cout<<"起始地址:  "<<hex<<handl->FA<<"H"<<endl;   
          cout<<"尾地址:    "<<hex<<handl->EA<<"H"<<endl;   
          if(handl->estate==1)   
          {   
              cout<<"状态:   占用"<<endl;   
              cout<<"装入进程:   "<<handl->tenor<<endl;   
          }   
          else   
          {   
              cout<<"状态:   空闲"<<endl;   
          }     
          handl=handl->next;   
      }   
	  
	  cout<<"       ********************************************"<<endl;   
	  cout<<"             内存总容量:  100   KB "<<endl;   
	  cout<<"             已分配容量: "<<dec<<sumtenor<<"    KB"<<endl;   
	  cout<<"             未分配容量: "<<dec<<100-sumtenor<<"    KB"<<endl;   
	  cout<<"       ********************************************"<<endl;   
  }   
  
  //选单           
  void   list()   
  {   
      cout<<"\n"<<endl;   
      cout<<"    **********************************************************\n"   
          <<"    *            最先适用法模拟实现内存管理                  *\n"   
          <<"    *  1--------调入进程         2----释放进程               *\n"   
          <<"    *  3----查看内存状态      其它----退出                   *\n"   
          <<"    **********************************************************\n"   
          <<"请选择"   ;   
	  
  }         
  void  main()           
  {  
	  initialize(); 
	  list();
	  cout<<endl;
	  char key;   
	  key=getche(); 
	  
      while(key=='1'||key=='2'||key=='3')   
      {      
          if(key=='1')   
          {   
			  FFAinsert();   
          }   
          if(key=='2')   
          {   
			  free();   
			  cout<<"\n按任意键继续.........."<<endl;   
			  getche();     
          }   
          if(key=='3')   
          {   
			  consult();   
			  cout<<"\n按任意键继续.........."<<endl; 
			  
			  getche();  
			  
          }      
		  key=getche();   
      }     
      return;   
  } 

⌨️ 快捷键说明

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