#include<stdio.h>
#include<stdlib.h>
#define OK 1 //完成
#define ERROR 0 //出错
typedef int Status;
typedef struct free_table//定义一个空闲区说明表结构
{
int num; //分区序号
long address; //起始地址
long length;//分区大小
int state; //分区状态
}ElemType;
typedef struct Node//线性表的双向链表存储结构
{
ElemType data;
struct Node*prior;//前趋指针
struct Node *next;//后继指针
}Node,*LinkList;
LinkList first;//头结点
LinkList end;//尾结点
int flag;//记录要删除的分区序号
Status Initblock()//开创带头结点的内存空间链表
{
first=(LinkList)malloc(sizeof(Node));
end=(LinkList)malloc(sizeof(Node));
first->prior=NULL;
first->next=end;
end->prior=first;
end->next=NULL;
end->data.num=1;
end->data.address=40;
end->data.length=600;
end->data.state=0;
return OK;
}
void sort()//分区序号重新排序
{
Node *p=first->next,*q;
q=p->next;
for(;p!=NULL;p=p->next)
{
for(q=p->next;q;q=q->next)
{
if(p->data.num>=q->data.num)
{
q->data.num+=1;
}
}
}
}
void show()//显示主存分配情况
{
int flag=0;//用来记录分区序号
Node *p=first; p->data.num=0;
p->data.address=0;
p->data.length=40;
p->data.state=1;
sort();
printf("\n\t\t》主存空间分配情况《\n");
printf("**********************************************************\n\n");
printf("分区序号\t起始地址\t分区大小\t分区状态\n\n");
while(p)
{
printf("%d\t\t%d\t\t%d",p->data.num,p->data.address,p->data.length);
if(p->data.state==0)
printf("\t\t空闲\n\n");
else
printf("\t\t已分配\n\n"); p=p->next;
}
printf("**********************************************************\n\n");
}
Status First_fit(int request)
{//首次适应算法
Node *p=first->next;//为申请作业开辟新空间且初始化
LinkList temp=(LinkList)malloc(sizeof(Node));
temp->data.length=request; temp->data.state=1;
p->data.num=1;
while(p)
{
if((p->data.state==0)&&(p->data.length==request))
{//有大小恰好合适的空闲块
p->data.state=1;
return OK;
break;
}
else if((p->data.state==0) && (p->data.length>request))
{//有空闲块能满足需求且有剩余
temp->prior=p->prior;
temp->next=p;
temp->data.address=p->data.address;
temp->data.num=p->data.num;
p->prior->next=temp;
p->prior=temp;
p->data.address=temp->data.address+temp->data.length;
p->data.length-=request;
p->data.num+=1;
return OK;
break;
}
p=p->next;
}
return ERROR;
}
Status Best_fit(int request)//最佳适应算法
{
int ch;//记录最小剩余空间
Node *p=first;
Node *q=NULL;//记录最佳插入位置
LinkList temp=(LinkList)malloc(sizeof(Node));
temp->data.length=request;
temp->data.state=1;
p->data.num=1;
while(p)//初始化最小空间和最佳位置
{
if((p->data.state==0) && (p->data.length>=request) )
{
if(q==NULL)
{
q=p;
ch=p->data.length-request;
}
else if(q->data.length > p->data.length)
{ q=p;
ch=p->data.length-request;
}
}
p=p->next;
}
if(q==NULL) return ERROR; //没有找到空闲块
else if(q->data.length==request)
{
q->data.state=1;
return OK;
}
else
{
temp->prior=q->prior;
temp->next=q;
temp->data.address=q->data.address;
temp->data.num=q->data.num;
q->prior->next=temp;
q->prior=temp;
q->data.address+=request;
q->data.length=ch;
q->data.num+=1;
return OK;
}
return OK;
}
Status Worst_fit(int request) //最差适应算法
{
int ch;//记录最大剩余空间
Node *p=first->next;
Node *q=NULL;//记录最佳插入位置
LinkList temp=(LinkList)malloc(sizeof(Node));
temp->data.length=request;
temp->data.state=1;
p->data.num=1;
while(p)//初始化最大空间和最佳位置
{
if(p->data.state==0 && (p->data.length>=request) )
{
if(q==NULL)
{
q=p;
ch=p->data.length-request;
}else if(q->data.length<p->data.length)
{
q=p;
ch=p->data.length-request;
}
}
p=p->next;
}
if(q==NULL) return ERROR;//没有找到空闲块
else if(q->data.length==request)
{
q->data.length=1;
return OK;
}else {
temp->prior=q->prior;
temp->next=q;
temp->data.address=q->data.address;
temp->data.num=q->data.num;
q->prior->next=temp;
q->prior=temp;
q->data.address+=request;
q->data.length=ch;
q->data.num+=1;
return OK;
} return OK;
}
//分配主存
Status allocation(int a)
{
int request;//申请内存大小
printf("请输入申请分配的主存大小(单位:KB):");
scanf("%d",&request);
if(request<0 ||request==0)
{
printf("分配大小不合适,请重试!");
return ERROR;
} switch(a)
{
case 1://默认首次适应算法
if(First_fit(request)==OK)
printf("\t****分配成功!****");
else printf("\t****内存不足,分配失败!****");
return OK;
break;
case 2://选择最佳适应算法
if(Best_fit(request)==OK)
printf("\t****分配成功!****");
else printf("\t****内存不足,分配失败!****");
return OK;
break;
case 3://选择最差适应算法
if(Worst_fit(request)==OK)
printf("\t****分配成功!****");
else printf("\t****内存不足,分配失败!****");
return OK;
break;
}
}
Status deal1(Node *p)//处理回收空间
{
Node *q=first;
for(;q!=NULL;q=q->next)
{
if(q==p)
{
if(q->prior->data.state==0&&q->next->data.state!=0)
{
q->prior->data.length+=q->data.length;
q->prior->next=q->next;
q->next->prior=q->prior;
q=q->prior;
q->data.state=0;
q->data.num=flag-1;
}
if(q->prior->data.state!=0&&q->next->data.state==0)
{
q->data.length+=q->next->data.length;
q->next=q->next->next;
q->next->next->prior=q;
q->data.state=0;
q->data.num=flag;
}
if(q->prior->data.state==0&&q->next->data.state==0)
{
q->prior->data.length+=q->data.length;
q->prior->next=q->next;
q->next->prior=q->prior;
q=q->prior;
q->data.state=0;
q->data.num=flag-1;
}
- 1
- 2
- 3
- 4
前往页