动态内存分配算法实验报告.docx
- 文档编号:9864665
- 上传时间:2023-02-07
- 格式:DOCX
- 页数:25
- 大小:103.37KB
动态内存分配算法实验报告.docx
《动态内存分配算法实验报告.docx》由会员分享,可在线阅读,更多相关《动态内存分配算法实验报告.docx(25页珍藏版)》请在冰豆网上搜索。
动态内存分配算法实验报告
动
态
存
分
配
算
法
实
验
报
告
院系:
计算机与通信工程学院
班级:
计科08-1班
:
胡太祥
学号:
200807010112
一.实验题目:
动态存分配算法
二、实验目的
深入了解动态分区存储管理方式存分配与回收的实现
三、实验要求
熟悉存储管理中动态分区的管理方式及Windows环境下,VC++程序设计方法
四、实验容
1,确定定存空闲分配表和进程存分配表
2,采用首次适应算法完成存空间的分配
3,采用最佳适应算法完成存空间的分配
4,采用最坏适应算法完成存空间的分配
5,实现存回收功能
五、实验结果
首次适应算法结果
最佳适应算法
最坏适应算法
六、实验总结
首次适应算法:
从表头指针开始查找课利用空间表,将找到的第一个大小不小于“请求”的空闲块的一部分分配给用户。
最佳适应算法:
将可利用空间表中一个大小不小于“请求”且最接近“请求”的空闲块的一部分分配给用户。
最坏适应算法:
将可利用空间表中一个大小不小于“请求”且是链表中最大的空闲块的一部分分配给用户。
以上是动态存分配的三种算法思想,算法采用数据结构中的双向链表实现。
附录(算法代码):
#include
#include
#defineFree0//空闲状态
#defineUsed1//已用状态
#defineOK1//完成
#defineERROR0//出错
#defineMAX_length32767//最大存空间为32767KB
typedefintStatus;//typedef将标识符Status定义成一个数据型标识符
intn=0;
typedefstructfreearea{//定义一个结构体freearea,并对这个空闲分区进行说明
intID;//分区号
longsize;//分区大小
longaddress;//分区地址
intstate;//当前状态
}ElemType;
typedefstructDuLNode{//doublelinkedlist//线性表的双向链表存储结构
ElemTypedata;
structDuLNode*prior;//前趋指针
structDuLNode*next;//后继指针
}DuLNode,*DuLinkList;
DuLinkListfree_list;//空闲链表
DuLinkListalloc_list;//已分配链表
Statusalloc(int);//存分配
voidfree_memory(intID,intmethod);//存回收
Statusfirst_fit(intID,intsize);//首次适应算法
Statusbest_fit(intID,intsize);//最佳适应算法
Statusworst_fit(intID,intsize);//最坏适应算法
voidfirst_fit_insert(DuLNode*insert);//首次适应插入排序
voidbest_fit_insert(DuLNode*insert);//最佳适应插入排序
voidworst_fit_insert(DuLNode*insert);//最坏适应插入排序
DuLNode*independent_node(DuLNode*node);//断开节点node与相邻节点的联系,使其孤立
//将节点node分割,返回分配的节点信息,node为剩余存信息
//node为双指针形式,因为可能需要对node的值进行修改
DuLNode*slice_node(DuLNode**node,intID,intsize);
voidshow();//查看分配
StatusInitblock();//开创空间表
StatusInitblock()//开创带头节点的存空间链表,头节点不用
{
alloc_list=(DuLinkList)malloc(sizeof(DuLNode));
free_list=(DuLinkList)malloc(sizeof(DuLNode));
//头节点不用
alloc_list->prior=alloc_list->next=NULL;
free_list->prior=free_list->next=NULL;
//空闲列表初始为整个存大小,放到node节点中
DuLNode*node=(DuLNode*)malloc(sizeof(DuLNode));
node->data.address=0;
node->data.size=MAX_length;
node->data.ID=0;
node->data.state=Free;
//将node节点放到空闲链表中
node->prior=free_list;
node->next=NULL;
free_list->next=node;
returnOK;
}
//将所插入节点按首址从小到大顺序插入到空闲链表中
voidfirst_fit_insert(DuLNode*insert)//首次适应插入排序
{
DuLNode*p=free_list->next;
//空闲链表为空,则将节点放到头节点后
if(p==NULL){
free_list->next=insert;
insert->prior=free_list;
return;
}
//按首址从小到大的顺序插入节点
while(p){
//找到插入位置:
p之前
if(insert->data.address<=p->data.address){
insert->next=p;
insert->prior=p->prior;
p->prior->next=insert;
p->prior=insert;
break;
}
//插入位置为链表尾
if(p->next==NULL){
p->next=insert;
insert->prior=p;
break;//还是提前退出循环的好,不然会再次进入循环
}
p=p->next;//搜索下一个节点
}
}
//最佳适应插入排序:
//将所插入节点按空间从小到大插入链表
voidbest_fit_insert(DuLNode*insert)
{
DuLNode*p=free_list->next;
//空闲链表为空,则插入到头节点后
if(p==NULL){
free_list->next=insert;
insert->prior=free_list;
return;
}
//按空间从小到大插入节点
while(p){
//在p前插入
if(insert->data.size<=p->data.size){
insert->next=p;
insert->prior=p->prior;
p->prior->next=insert;
p->prior=insert;
break;
}
//插入位置为链表尾
if(p->next==NULL){
p->next=insert;
insert->prior=p;
break;//还是提前退出循环的好,不然会再次进入循环
}
p=p->next;//搜索下一个节点
}
}
//最坏适应插入排序:
//将所插入节点按空间从大到小插入链表
voidworst_fit_insert(DuLNode*insert)
{
DuLNode*p=free_list->next;
//链表为空,则插入到头节点后
if(p==NULL){
free_list->next=insert;
insert->prior=free_list;
return;
}
//按空间从大到小插入节点
while(p){
//在p前插入节点
if(insert->data.size>=p->data.size){
insert->next=p;
insert->prior=p->prior;
p->prior->next=insert;
p->prior=insert;
break;
}
//插入位置为链表尾
if(p->next==NULL){
p->next=insert;
insert->prior=p;
break;//还是提前退出循环的好,不然会再次进入循环
}
p=p->next;//搜索下一个节点
}
}
//断开节点node与相邻节点间的联系,使其孤立
//返回孤立后的节点指针
DuLNode*independent_node(DuLNode*node)
{
if(node!
=NULL){
if(node->prior!
=NULL){
node->prior->next=node->next;
}
if(node->next!
=NULL){
node->next->prior=node->prior;
}
node->prior=node->next=NULL;
}
returnnode;
}
//将节点node分片,其中一片allocnode为为用户所分配的存片,
//另一片为分配后原node节点所剩余的存片,放到node中
//若node节点原来大小等于所要开辟的大小,则node赋值为NULL
//返回分配结果
DuLNode*slice_node(DuLNode**node,intID,intsize)
{
DuLNode*allocnode=NULL;
//node的大小正好等于所要分配的大小,
//则直接将该节点放到分配链表中,node赋值为NULL
if((*node)->data.size==size){
//将node从空闲链表中分出来
independent_node((*node));
//得到所分配的存片
allocnode=(*node);
allocnode->data.ID=ID;
allocnode->data.state=Used;
//没有剩余空间,这个地方导致必须用双重指针
(*node)=NULL;
}elseif((*node)->data.size>size){//node的大小大于所需的大小,故将node分为两片
//一片为所分配的存片,分配地址和空间
allocnode=(DuLNode*)malloc(sizeof(DuLNode));
allocnode->data.size=size;
allocnode->data.address=(*node)->data.address;
allocnode->data.ID=ID;
allocnode->data.state=Used;
allocnode->prior=allocnode->next=NULL;
//另一片为分配后剩下的存片,修改存地址和大小
(*node)->data.address+=size;
(*node)->data.size-=size;
}
//返回分配结果
returnallocnode;
}
//按不同的分配方法归还存
voidfree_memory(intID,intmethod)//存回收
{
DuLNode*p,*prior,*next,*freenode;
p=prior=next=freenode=NULL;
//从已分配链表中找到所要归还的存信息
p=alloc_list->next;
while(p){
if(p->data.ID==ID){//找到所要归还的存信息
freenode=p;
freenode->data.state=Free;
freenode->data.ID=Free;
break;
}
p=p->next;//继续查找
}
//将freenode从已分配链表中分离出来
independent_node(freenode);
if(freenode==NULL){//没有所要归还的存
return;
}
//合并空闲存片
p=free_list->next;
while(p){
//找到先前合并存节点prior
if(p->data.address+p->data.size==freenode->data.address){
prior=p;
}elseif(freenode->data.address+freenode->data.size==p->data.address){
//找到向后合并存节点next
next=p;
//在这儿,如果是首次适应算法可以直接中断循环了
}
p=p->next;//继续查找
}
//向前合并节点存在,则进行合并
if(prior!
=NULL){
independent_node(prior);//将prior从空闲链表中分离出来
//freenode与prior合并
freenode->data.address=prior->data.address;
freenode->data.size+=prior->data.size;
//释放prior节点
free(prior);
}
//向后合并节点存在,则进行合并
if(next!
=NULL){
independent_node(next);//将next从空闲链表中分离出来
//freenode与next合并
freenode->data.size+=next->data.size;
//释放next节点
free(next);
}
//按不同的分配方式,重新插入合并后的节点
if(method==1){
first_fit_insert(freenode);
}elseif(method==2){
best_fit_insert(freenode);
}elseif(method==3){
worst_fit_insert(freenode);
}
}
Statusalloc(intch)//分配主存
{
intID,request;
cout<<"请输入作业(分区号):
";
cin>>ID;
cout<<"请输入需要分配的主存大小(单位:
KB):
";
cin>>request;
if(request<0||request==0){
cout<<"分配数据不合适,请重新输入!
"< returnERROR; } if(ch==1){//首次适应算法 if(first_fit(ID,request)==OK){ cout<<"分配成功! "< }else{ cout<<"存不足,分配失败! "< } returnOK; }elseif(ch==2){//选择最佳适应算法 if(best_fit(ID,request)==OK){ cout<<"分配成功! "< }else{ cout<<"存不足,分配失败! "< } returnOK; }elseif(ch==3){//最坏适应算法 if(worst_fit(ID,request)==OK){ cout<<"分配成功! "< }else{ cout<<"存不足,分配失败! "< } returnOK; } returnERROR; } Statusfirst_fit(intID,intsize)//传入作业名及申请量//首次适应算法- { DuLNode*p=free_list->next; DuLNode*allocnode=NULL; while(p){ //找到合适空闲存节点 if(p->data.size>=size){ break; } p=p->next; } if(p==NULL){//存空间不足 returnERROR; } //将p分片,返回所要分配的存信息 //由于是按首址从小到大排列空闲存节点 //故不需再重排剩余存节点 allocnode=slice_node(&p,ID,size); DuLNode*q=alloc_list->next; //将所分配节点放到已分配链表头节点后 if(q! =NULL){ allocnode->next=q; allocnode->prior=q->prior; q->prior->next=allocnode; q->prior=allocnode; }else{ alloc_list->next=allocnode; allocnode->prior=alloc_list; } returnOK; } Statusbest_fit(intID,intsize)//最佳适应算法 { DuLNode*p=free_list->next; DuLNode*allocnode=NULL,*leftnode=NULL; while(p){ //找到了合适的存节点 if(p->data.size>=size){ break; } p=p->next; } if(p==NULL){//存不足 returnERROR; } //分片,剩余存片需重新排序 allocnode=slice_node(&p,ID,size); leftnode=p; DuLNode*q=alloc_list->next; //将所分配节点放到已分配链表头节点后 if(q! =NULL){ allocnode->next=q; allocnode->prior=alloc_list; q->prior->next=allocnode; q->prior=allocnode; }else{ alloc_list->next=allocnode; allocnode->prior=alloc_list; } //重新插入剩余存片 if(leftnode! =NULL){ //分离剩余存片 independent_node(leftnode); //插入该片 best_fit_insert(leftnode); } returnOK; } Statusworst_fit(intID,intsize)//最坏适应算法,直接找第一个节点 { DuLNode*p=free_list->next; DuLNode*allocnode=NULL,*leftnode=NULL; if(p==NULL||p->data.size returnERROR; } //分片,剩余存片需重新排序 allocnode=slice_node(&p,ID,size); leftnode=p; DuLNode*q=alloc_list->next; //将所分配节点放到已分配链表头节点后 if(q! =NULL){ allocnode->next=q; allocnode->prior=alloc_list; q->prior->next=allocnode; q->prior=allocnode; }else{ alloc_list->next=allocnode; allocnode->prior=alloc_list; } //重新插入剩余存片 if(leftnode! =NULL){ //分离剩余存片 independent_node(leftnode); //插入该片 worst_fit_insert(leftnode); } returnOK; } voidshow()//显示主存分配情况 { cout<<"主存分配情况\n"; DuLNode*p=free_list->next; cout<<"\n空闲分区\n\n"; while(p){ cout<<"分区号: Free"< cout<<"起始地址: "< cout<<"分区大小: "< cout<<"-----------------------"< p=p->next; } cout<<"\n已分配分区\n\n"; p=alloc_list->next; while(p){ cout<<"分区号: "< cout<<"起始地址: "< cout<<"分区大小: "< cout<<"-----------------------"< p=p->next; } } vo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 动态 内存 分配 算法 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)