资源描述:
《操作系统“最佳适应算法”代码(c++)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、3、最佳适应算法#includestructkongkuai{intstartaddr;intsize;intflag;}kongxq[6]={{10,20,1},{50,50,1},{150,100,1},{300,60,1},{400,150,1},{700,200,1}};intallocate(intjobsize){inti;intt=0;for(i=0;i<6;i++)if(kongxq[i].flag==1&&kongxq[i].size>jobsize){kongx
2、q[i].startaddr+=jobsize;kongxq[i].size-=jobsize;t=1;returnkongxq[i].startaddr-jobsize;}elseif(kongxq[i].flag==1&&kongxq[i].size==jobsize){kongxq[i].flag=0;t=1;returnkongxq[i].startaddr;}if(t==0)returnfalse;return1;}circle(){inti,j;structkongkuaitemp;fo
3、r(i=0;i<6;i++)for(j=0;j<6;j++)if(kongxq[j].size>kongxq[j+1].size){temp.startaddr=kongxq[j].startaddr;temp.size=kongxq[j].size;temp.flag=kongxq[j].flag;kongxq[j].startaddr=kongxq[j+1].startaddr;kongxq[j].size=kongxq[j+1].size;kongxq[j].flag=kongxq[j+1].
4、flag;kongxq[j+1].startaddr=temp.startaddr;kongxq[j+1].size=temp.size;kongxq[j+1].flag=temp.flag;}for(i=0;i<6;i++)for(j=0;j<6;j++)if(kongxq[j].flag==0&&kongxq[j+1].flag==1){temp.startaddr=kongxq[j].startaddr;temp.size=kongxq[j].size;temp.flag=kongxq[j].
5、flag;kongxq[j].startaddr=kongxq[j+1].startaddr;kongxq[j].size=kongxq[j+1].size;kongxq[j].flag=kongxq[j+1].flag;kongxq[j+1].startaddr=temp.startaddr;kongxq[j+1].size=temp.size;kongxq[j+1].flag=temp.flag;}return1;}callback(){ints,len,t1=0,t2=0,t3=0,i,j;p
6、rintf("请输入回收区的起始地址:");scanf("%d",&s);printf("请输入回收区的大小:");scanf("%d",&len);for(i=0;i<6;i++){if((kongxq[i].startaddr==s+len)&&(kongxq[i].flag==1)){len+=kongxq[i].size;t1=1;for(j=0;j<6;j++)if((kongxq[j].startaddr+kongxq[j].size==s)&&(kongxq[j].flag==
7、1)){kongxq[i].flag=0;//kongxq[j].startaddr=kongxq[j].size=kongxq[j].size+len;t2=1;break;}if(t2==0){kongxq[i].startaddr=s;kongxq[i].size=len;break;}}}if(t1==0){for(i=0;i<6;i++){if((kongxq[i].startaddr+kongxq[i].size==s)&&(kongxq[i].flag==1)){kongxq[i].s
8、ize+=len;t3=1;break;}if(t3==0)for(j=0;j<6;j++)if(kongxq[j].flag==0){kongxq[j].startaddr=s;kongxq[j].size=len;kongxq[j].flag=1;break;}}}return1;}voidprint(){inti;printf("起始地址
9、大小
10、标记");for(i=0;i<6;i++){printf("%3d
11、%3d
12、%3d",kongxq[i