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

📄 内存管理.cpp

📁 用指针对简要内存进行简单管理
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////

//计算机操作系统实验   内存管理
//版本号 3。0
//日期   2003-11-6

#include "stdio.h"
#include "conio.h"
#include "iostream.h"
#define MAX 10
#define MAXSIZE 300
#define INVALID -1
typedef struct process
{  int no;
   int size;
   int startAddress;
}pr;
typedef struct LA
{
//剩余空间
  int size;
  int startAddress;
}La;
La la[MAX];
pr p[MAX];
int pN, lN;
void Create()
{
	int i,j,k,num,tem;
    pr tempProcess;
    printf("请输入要创建的进程数目:");
    scanf("%d", &num);
    tem = pN+num;
    for (i=pN; i<tem; i++)
    {
       if (i >= MAX)
       {
	       printf("额度数已满,不能创建额外的进程\n");//can't create extra processes.
    	  return;
	   }
       while(1)
       {
	      printf("ID号:");
    	  scanf("%d", &tempProcess.no);
	      for (j=0; j<pN; j++)
		  {
	          if (p[j].no == tempProcess.no)
			  {
		        printf("这个ID号已经存在,不能创建,请输入别的ID号.\n");
	           	break;
			  }
		  }
	      if (j == pN)
     	    break;
	   }
	   cout<<"空间大小:";
       cin>>tempProcess.size;
       printf("\n");
       for (j=0; j<lN; j++)
       {
           if (la[j].size < tempProcess.size)
			   continue;
		   p[i].no = tempProcess.no;
           p[i].size = tempProcess.size;
	       p[i].startAddress = la[j].startAddress;
           pN+=1;
		   if (la[j].size==tempProcess.size)
           {
               for(k=j; k<=lN-2; k++)
				   la[k]=la[k+1];
                   lN-=1;
           }
		   else
		   {
			   la[j].startAddress+=tempProcess.size;
	           la[j].size=la[j].size-tempProcess.size;
		   }
		   break;
       }
       if (j==lN)
		   cout<<"进程"<< tempProcess.no <<"不能被创建."<<endl;
	}
}
void Output()
{
    int i;
    cout<<"进程菜单:"<<endl;
    cout<<"      ID号 \t 开始地址    \t 大小"<<endl;
    cout<<"   --------------------------------------"<<endl;
    for (i=0; i<pN; i++)
	{
        printf("\t%d\t %d \t\t %d\n",p[i].no,p[i].startAddress,p[i].size);
    }
    cout<<"   --------------------------------------"<<endl;
    cout<<"\n\n\n";
    cout<<"剩余地址空间列表:"<<endl;//Vacant Area List
    cout<<"\n\t索引 \t 开始地址  \t 大小size"<<endl;
    cout<<"    --------------------------------------"<<endl;
    for (i=0; i<lN; i++)
    {//"\t%d \t %d \t\t %d\n",
        cout<<"        "<<i<<"         "<<la[i].startAddress<<"              "<<la[i].size<<endl;
        cout<<"    --------------------------------------"<<endl;
    }
}
void Inte(int ind)
{
    int i;
    if (la[ind+1].startAddress==la[ind].startAddress
     +la[ind].size)
	{
		la[ind].size += la[ind+1].size;
        for (i=ind+1; i<=lN-2; i++)
			la[i] =la[i+1];
		lN -= 1;
	}
}
void Del(int tempID)
{
    int i, j;
	int ind = INVALID;
    for (i=0; i<pN; i++)
	{
		if (p[i].no == tempID)
		{
			ind = i;
			break;
		}
	}
	if (ind == INVALID)
    {
		printf("invalid ID.\n");
        return;
    }
    for (i=0; i<lN; i++)
    {
		if (la[i].startAddress > p[ind].startAddress)
		{
			for (j=lN; j>i; j--)
	        la[j] = la[j-1];
	        break;
		}
   }
	la[i].startAddress = p[ind].startAddress;
    la[i].size = p[ind].size;
    lN+= 1;
	if (i <= lN-2)
		Inte(i);
	if (i > 0)
		Inte(i-1);
	for (i=ind; i<=pN-2; i++)
		p[i] = p[i+1];
	pN-= 1;
}
void main()
{
    int i,ind,tempID;
//  clrscr();
    la[0].size=MAXSIZE,lN=1,pN=0;
    while(1)
	{

       ind= 0;
       cout<<"1.创建  2.销毁  3.显示  4.离开 "<<endl;
       cout<<"请输入你的选择(1--4):"<<endl;
       cin>>ind;
       if (ind<1 || ind>4)
	   {
		   cout<<"错误输入.press any key to exit."<<endl;
           getch();
	       for (i=0; i<pN; i++)
	       Del(p[i].no);
		}
	    switch(ind)
		{
            case 1: if (lN> 0)
					{
	                    Create();
                        Output();
					}
	                else
	                     cout<<"there are no vacant areas."<<endl;
    	                 break;

           case 2: if (pN> 0)
				   {
                       cout<<"ID of process to destory:"<<endl;
                       cin>>tempID;
                       Del(tempID);
	                   Output();
				   }
                   else
	                   cout<<"there are no processes."<<endl;
    	                 break;

           case 3: Output();
	              break;
           case 4: for (i=0; i<pN; i++)
                      Del(p[i].no);
    	                return;
		}
	}
}

⌨️ 快捷键说明

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