首次适应性算法实验报告.docx
《首次适应性算法实验报告.docx》由会员分享,可在线阅读,更多相关《首次适应性算法实验报告.docx(7页珍藏版)》请在冰豆网上搜索。
首次适应性算法实验报告
实验三 首次适应性算法
实验名称:
首次适应算法
实验目的:
熟悉首次适应算法,理解分配内存资源及回收资源的方法,加深记意。
实验内容:
从空闲分区表的第一个表目起查找该表,把最先能够满足要求的空闲区分配给作业,这种方法目的在于减少查找时间。
为适应这种算法,空闲分区表(空闲区链)中的空闲分区要按地址由低到高进行排序。
该算法优先使用低址部分空闲区,在低址空间造成许多小的空闲区,在高地址空间保留大的空闲区。
实验程序:
#include
#include
#defineNull0
typedefstructEmptyList /*链表节点数据结构*/
{
structEmptyList*pre; /*前驱*/
intstartaddress; /*起始地址*/
intspace; /*可用空间*/
intIsuse; /*使用状态,0为未使用*/
structEmptyList*next; /*后继节点*/
};
voidmain()
{
structEmptyList*head;
intRequest;
intnum;
structEmptyList*CreateEmptyList(intn); /*链表创建函数声明*/
structEmptyList*FirstFit(struct EmptyList*h,intrequest,intN);/*FF算法声明*/
voidPrint(struct EmptyList*node); /*输出函数声明*/
printf("\nInputemptynode:
"); /*输入节点个数*/
scanf("%d",&num);
head=CreateEmptyList(num); /*调用创建函数*/
Print(head); /*打印输出*/
again:
printf("\nInputrequestspace:
"); /*输入进程请求空间*/
scanf("%d",&Request);
head=FirstFit(head,Request,num); /*执行FF算法*/
printf("\nAfterAllocation:
");
Print(head);
gotoagain;
getch();
}
voidPrint(struct EmptyList*node) /*打印函数*/
{
printf("\nStartA\tSpace\tIsuse\n");
while(node!
=Null)
{
printf("%d\t%d\t%d\n",node->startaddress,node->space,node->Isuse);
node=node->next;
}
}
structEmptyList*CreateEmptyList(intn) /*创建函数*/
{
structEmptyList*h; /*头结点*/
structEmptyList*p1,*p2;
inti=0; /*I用以控制程序流程*/
h=p1=(structEmptyList*)malloc(sizeof(structEmptyList));
printf("Thebeginningaddress:
");
scanf("%d",&p1->startaddress);
printf("Available of Space:
");
scanf("%d",&p1->space);
printf("StateaboutAllocated:
");
scanf("%d",&p1->Isuse);
h->pre=Null;
h->next=p1->next=Null;
for(i=1;i{
p2=(structEmptyList*)malloc(sizeof(structEmptyList));
p2->pre=p1;
p2->pre->next=p2;
printf("Thebeginningaddress:
");
scanf("%d",&p2->startaddress);
printf("Available of Space:
");
scanf("%d",&p2->space);
printf("StateaboutAllocated:
");
scanf("%d",&p2->Isuse);
p1=p2;
p2->next=Null;
p2=Null;
}
returnh;
}
structEmptyList*FirstFit(structEmptyList*h,intrequest,intN)/*FF算法*/
{
inti=0; /*程序控制变量*/
intAddress; /*返回给进程的可用空间的其实地址*/
structEmptyList*p;
p=h;
do
{
i=i+1;
if(p->space>=request&&p->Isuse==0)break; /*找到空间满足且未分配的跳出循环*/
p=p->next;
}
while(p!
=Null);
if(p->space==request) /*正好满足要求的情形*/
{
Address=p->startaddress; /*返回起始地址*/
if(i==1) /*满足条件的为头结点*/
{
h=p->next;
h->pre=Null;
}
elseif(i==N) /*尾节点*/
p->pre->next=Null;
else /*其他节点*/
{
p->pre->next=p->next;
p->next->pre=p->pre;
}
}
elseif(p->space>request) /*可用空间大于进程请求空间*/
{
Address=p->startaddress;
p->startaddress=p->startaddress+request;
p->space=p->space-request;
}
elseAddress=0;
if(Address!
=0)
printf("\nAllocationsuccesful!
\nBeginningAddressofthespace:
%d",Address);
else
printf("\nNoEmptySpacefit!
");
returnh;
}
实验数据
空闲节点:
4个
StartA------>>空闲节点的起始地址
Space-------->>空闲节点空间
Isuse-------->>使用情况,0为未使用,非0为已分配
1、申请空间:
15。
结果如下:
2、继续申请空间:
35
3、继续申请空间:
500
实验总结:
FF算法要求空闲链已地址递增的次序连接。
分配内存时,从链首开始顺序查找,直到找到第一个满足要求的空间并分配给进程,把分配后余下的空间仍然留在链表中。
若从链首至链尾都不满足要求,则分配失败。
该算法倾向于优先使用低地址的空间,在不断分割中会产生很多空间碎片,并且每次都是从链首开始查找,这无疑增加了开销。
继续阅读