操作系统存储器管理源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

动态分区分配模拟项目源代码

#include

#include

#define Free 0

#define Busy 1

#define OK 1

#define ERROR 0

#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;

}

}

相关文档
最新文档