Professional Documents
Culture Documents
空间链以地址递增的次序链接。进行内存分配时,从链表头部开始依次检索,找到第一个不小于请求空
间大小的空闲空间进行分配。分配时需考虑碎片问题,若分配会导致碎片产生则将整块分区分配。内存
的回收需要考虑四种情况:⑴回收分区前后两个分区都空闲,则需要和前后两个分区合并;(2)回收分
区只有前一分区空闲,则与前一分区合并;( 3)回收分区只有后一分区空闲,则和后一分区合并;
(4)回收分区独立,不考虑合并
。
2、主要数据结构:
struct FreeArea{ 链结点包含的数据:分区号、大小、起址、标记
int ID;
int size;
long address;
int sign;
};
struct Node { 双链表结点结构体:数据区、前向指针、后继指针
FreeArea data;
struct Node *prior;
struct Node *next;
}*DLinkList;
3、源程序代码:
#include<iostream>
using namespace std;
#define Free 0 //空闲状态
#define Busy 1 //已用状态
#define PBusy 2 //碎片已用状态
#define FINISH 1 //完成
#define FINISH2 1 //完成
#define ERROR 0 //出错
#define memory 512 //最大内存空间为(单位:KB)
#define min 10 //碎片最小值(单位:KB)
};
1/6
typedef struct Node //空闲连结构
{
FreeArea data;
struct Node *prior;
struct Node *next;
}*DLinkList;
int Create()//初始化
{
head=(DLinkList)malloc(sizeof(Node));//分配内存
tail=(DLinkList)malloc(sizeof(Node));
head->prior=NULL;
head->next=tail;
tail->prior=head;
tail->next=NULL;
tail->data.address=0;
tail->data.size=memory;
tail->data.ID=0;
tail->data.sign=Free;
return FINISH;
}
2/6
temp->next=p;
temp->data.address=p->data.address;
p->prior->next=temp;
p->prior=temp;
p->data.address=temp->data.address+temp->data.size;
p->data.size=p->data.size-request;
return FINISH;
break;
}
else if(p->data.sign==Free && p->data.size>request && p->data.size-request<=min)// 产
生碎片时
{
p->data.sign=PBusy;
p->data.ID=ID;
return FINISH;
break;
}
p=p->next;//若前面的分区都已分配,P指针后移
}
return ERROR;
}
int Allocate()//主存分配
{
int ID,request;
cout<<"请输入作业所在分区号:";
cin>>ID;
cout<<"请输入分配的主存(单位:KB):";
cin>>request;
if(request<0 ||request==0)
{
cout<<"分配的主存必须是正整数!"<<endl;
return ERROR;
}
if(FirstFit(ID,request)==FINISH)
cout<<"分配成功!"<<endl;
else
cout<<"内存不足,分配失败!"<<endl;
}
3/6
{
if(p->data.ID==ID)
{
p->data.sign=Free;
//p->data.ID=Free;
if((p->prior->data.sign==Free)&&(p->next->data.sign==Free))//与前后的空闲块相
连
{
p->prior->data.size=p->prior->data.size+p->data.size+p->next->data.size;
p->prior->next=p->next->next;
if(p->next->next==NULL)//若p->next是最后一个结点
{p->prior->data.ID=Free;p->next=NULL;}
else{p->next->next->prior=p->prior;}
break;
}
if(p->prior->data.sign==Free)//与前面的空闲块相连
{
p->prior->data.size+=p->data.size;
p->prior->next=p->next;
p->next->prior=p->prior;
break;
}
if(p->next->data.sign==Free)//与后面的空闲块相连
{
p->data.size+=p->next->data.size;
if(p->next->next==NULL)//若p->next是最后一个结点
p->next=NULL;
else{
p->next->next->prior=p;
p->next=p->next->next;}
break;
}
break;
}
p=p->next;
}
cout<<"分区:"<<ID<<"回收成功"<<endl;
return FINISH;
}
void show()//显示
{
4/6
cout<<" 主存分配情况 \n";
Node *p=head->next;
while(p)
{
cout<<"分区号:";
if(p->data.ID==Free)
cout<<"Free"<<endl;
else
cout<<p->data.ID<<endl;
cout<<"起始地址:"<<p->data.address;
cout<<" 分区大小:"<<p->data.size<<" KB";
cout<<" 状 态:";
if(p->data.sign==Free)
cout<<"空 闲"<<endl;
else if(p->data.sign==PBusy)
cout<<"碎片已分配"<<endl;
else
cout<<"已分配"<<endl;
p=p->next;
}
cout<<endl;
}
void main()
{
Create();
int choice;
int i;
for(i=0;;i++)
{
cout<<"请输入操作:";
cout<<"1.分配内存 2.回收内存 3.显示主存 0.退出";
cout<<endl;
cin>>choice;
if(choice==1)// 分配内存
Allocate();
else if(choice==2)// 内存回收
{ int ID;
cout<<"请输入要释放的分区号:";
cin>>ID;
Recycle(ID);
}
else if(choice==3)//显示主存
show();
5/6
else if(choice==0)//退出
break;
else //非法输入
{
cout<<"输入有误!"<<endl;
continue;
}
}
}
6/6