欢迎来到天天文库
浏览记录
ID:18535757
大小:91.00 KB
页数:8页
时间:2018-09-19
《操作系统存储器管理——最佳适应算法》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、存储管理上机作业——最佳适应算法以最佳适应为例,给出可变式分区的分配回收算法。源程序如下:#include#defineN5structfreearea{intstartaddress;intsize;intstate;}freeblock[N]={{20,20,1},{80,30,1},{160,10,1},{200,40,0},{250,50,1}};intallocate(intapplyarea){inti,tag=0;for(i=0;iapplyarea&&freeblock[i].s
2、tate==1){freeblock[i].startaddress=freeblock[i].startaddress+applyarea;freeblock[i].size=freeblock[i].size-applyarea;tag=1;//有满足条件的分区时,tag置1returnfreeblock[i].startaddress-applyarea;}elseif(freeblock[i].size==applyarea&&freeblock[i].state==1){freeblock[i].state=0;tag=1;//有满足条件的分区时,tag置1r
3、eturnfreeblock[i].startaddress;}}if(tag==0)return-1;}voidset_free(){inti,j,s,l,tag1=0,tag2=0,tag3=0;printf("pleaseinputthestartaddressyouwanttosetfree:");scanf("%d",&s);printf("pleaseinputthesize:");scanf("%d",&l);for(i=0;i4、eeblock[i].state==1)//释放区与某一空闲块的高地址相邻接{l=freeblock[i].size+l;tag1=1;//释放区与某一空闲块的高地址相邻接for(j=0;j5、startaddress;freeblock[i].state=0;tag2=1;//标记同时与两个空闲块相接break;}}if(tag2==0){freeblock[i].startaddress=s;freeblock[i].size=l;break;}}}//forif(tag1==0)//不与空闲块的高地址相接{for(i=0;i6、ock[i].size=freeblock[i].size+l;tag3=1;break;}if(tag3==0)//不与任何空闲块相接{for(j=0;jf7、reeblock[j].size){temp.size=freeblock[i].size;temp.startaddress=freeblock[i].startaddress;temp.state=freeblock[i].state;freeblock[i].size=freeblock[j].size;freeblock[i].startaddress=freeblock[j].startaddress;freeblock[i].state=freeblock[j].state;freeblock[j].size=temp.siz
4、eeblock[i].state==1)//释放区与某一空闲块的高地址相邻接{l=freeblock[i].size+l;tag1=1;//释放区与某一空闲块的高地址相邻接for(j=0;j5、startaddress;freeblock[i].state=0;tag2=1;//标记同时与两个空闲块相接break;}}if(tag2==0){freeblock[i].startaddress=s;freeblock[i].size=l;break;}}}//forif(tag1==0)//不与空闲块的高地址相接{for(i=0;i6、ock[i].size=freeblock[i].size+l;tag3=1;break;}if(tag3==0)//不与任何空闲块相接{for(j=0;jf7、reeblock[j].size){temp.size=freeblock[i].size;temp.startaddress=freeblock[i].startaddress;temp.state=freeblock[i].state;freeblock[i].size=freeblock[j].size;freeblock[i].startaddress=freeblock[j].startaddress;freeblock[i].state=freeblock[j].state;freeblock[j].size=temp.siz
5、startaddress;freeblock[i].state=0;tag2=1;//标记同时与两个空闲块相接break;}}if(tag2==0){freeblock[i].startaddress=s;freeblock[i].size=l;break;}}}//forif(tag1==0)//不与空闲块的高地址相接{for(i=0;i6、ock[i].size=freeblock[i].size+l;tag3=1;break;}if(tag3==0)//不与任何空闲块相接{for(j=0;jf7、reeblock[j].size){temp.size=freeblock[i].size;temp.startaddress=freeblock[i].startaddress;temp.state=freeblock[i].state;freeblock[i].size=freeblock[j].size;freeblock[i].startaddress=freeblock[j].startaddress;freeblock[i].state=freeblock[j].state;freeblock[j].size=temp.siz
6、ock[i].size=freeblock[i].size+l;tag3=1;break;}if(tag3==0)//不与任何空闲块相接{for(j=0;jf
7、reeblock[j].size){temp.size=freeblock[i].size;temp.startaddress=freeblock[i].startaddress;temp.state=freeblock[i].state;freeblock[i].size=freeblock[j].size;freeblock[i].startaddress=freeblock[j].startaddress;freeblock[i].state=freeblock[j].state;freeblock[j].size=temp.siz
此文档下载收益归作者所有