动态分区分配模拟项目源代码
#include
#define MAX_length 1280 typedef int Status; typedef struct freearea { int ID;
long size; long address; int state; }ElemType;
typedef struct DuLNode //double linked list { ElemType data;
struct DuLNode *prior; struct DuLNode *next; }DuLNode,*DuLinkList; DuLinkList block_first; DuLinkList block_last; Status alloc(int); Status free(int);
Status First_fit(int,int); Status Best_fit(int,int); void show();
Status Initblock(); Status Initblock() {
block_first=(DuLinkList)malloc(sizeof(DuLNode)); block_last=(DuLinkList)malloc(sizeof(DuLNode)); block_first->prior=NULL; block_first->next=block_last; block_last->prior=block_first; block_last->next=NULL; block_last->data.address=0;
block_last->data.size=MAX_length; block_last->data.ID=0;
block_last->data.state=Free; return OK; }
Status alloc(int ch) {
int ID,request;
cout<<\请输入作业(分区号):\ cin>>ID;
cout<<\请输入需要分配的主存大小(单位:KB):\ cin>>request;
if(request<0 ||request==0) {
cout<<\分配大小不合适,请重试!\ return ERROR; }
if(ch==2) //选择最佳适应算法
{ if(Best_fit(ID,request)==OK) cout<<\分配成功!\ else cout<<\内存不足,分配失败!\ return OK; }
else //默认首次适应算法
{ if(First_fit(ID,request)==OK) cout<<\分配成功!\ else cout<<\内存不足,分配失败!\ return OK; } }
Status First_fit(int ID,int request) {
DuLinkList temp=(DuLinkList)malloc(sizeof(DuLNode)); temp->data.ID=ID; temp->data.size=request; temp->data.state=Busy;
DuLNode *p=block_first->next; while(p) {
if(p->data.state==Free && p->data.size==request) {
p->data.state=Busy; p->data.ID=ID; return OK; break; }
if(p->data.state==Free && p->data.size>request) { temp->prior=p->prior; 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-=request; return OK; break; }
p=p->next; }
return ERROR; }
Status Best_fit(int ID,int request) {
int ch;
DuLinkList temp=(DuLinkList)malloc(sizeof(DuLNode)); temp->data.ID=ID; temp->data.size=request; temp->data.state=Busy;
DuLNode *p=block_first->next; DuLNode *q=NULL; while(p) {
if(p->data.state==Free &&
(p->data.size>request || p->data.size==request) ) { q=p;
ch=p->data.size-request; break; }
p=p->next; }
while(p) {
if(p->data.state==Free && p->data.size==request) {
p->data.ID=ID; p->data.state=Busy; return OK; break; }
if(p->data.state==Free && p->data.size>request) {
if(p->data.size-request ch=p->data.size-request; q=p; } } p=p->next; } if(q==NULL) return ERROR;//没有找到空闲块 else { temp->prior=q->prior; temp->next=q; temp->data.address=q->data.address; q->prior->next=temp; q->prior=temp; q->data.address+=request; q->data.size=ch; return OK; } } Status free(int ID) { DuLNode *p=block_first; while(p) { if(p->data.ID==ID) { p->data.state=Free; p->data.ID=Free; if(p->prior->data.state==Free) { p->prior->data.size+=p->data.size; p->prior->next=p->next; p->next->prior=p->prior; } if(p->next->data.state==Free) { p->data.size+=p->next->data.size; p->next->next->prior=p; p->next=p->next->next; } break; } p=p->next; } return OK; } void show() { cout<<\ cout<<\ 主 存 分 配 情 况 +++\\n\ cout<<\ DuLNode *p=block_first->next; while(p) { cout<<\分 区 号:\ if(p->data.ID==Free) cout<<\ else cout< cout<<\起始地址:\ cout<<\分区大小:\ cout<<\状 态:\ if(p->data.state==Free) cout<<\空 闲\ else cout<<\已分配\ cout<<\——————————————\ p=p->next; } } //----------------------- 主 函 数--------------------------- void main() { int ch; cout<<\ 动态分区分配方式的模拟 \\n\ cout<<\ cout<<\首次适应算法 2)最佳适应算法 **\\n\ cout<<\ cout<<\请选择分配算法:\ cin>>ch; Initblock(); int choice; while(1) { cout<<\ cout<<\ 1: 分配内存 2: 回收内存 **\\n\ cout<<\ 3: 查看分配 0: 退 出 **\\n\ cout<<\ cout<<\请输入您的操作 :\ cin>>choice; if(choice==1) alloc(ch); // 分配内存 else if(choice==2) // 内存回收 { int ID; cout<<\请输入您要释放的分区号:\ cin>>ID; free(ID); } else if(choice==3) show();//显示主存