您好,欢迎访问三七文档
暨南大学本科实验报告专用纸一,实验目的。了解动态分区分配方式中使用的数据结构和分配算法,并进一步加深对动态扮区存储管理方式及其实现过程的理解。二,实验内容。1分别实现首次适应算法和最佳适应算法的动态分区分配过程和回收过程。其中,空闲分区通过空闲区链进行管理;进行内存分配时,系统优先使用空闲区低端的空间。2、假设初试状态下,可用的内存空间为640KB,并有下列的请求序列:作业1申请130KB作业2申请60KB作业3申请100KB作业2释放60KB作业4申请200KB作业3释放100KB作业1释放130KB作业5申请140KB作业6申请60KB作业7申请50KB作业6释放60KB3、每次分配和回收后显示空闲内存分区链情况。三,实验源代码。最佳适应算法:#includestdio.h#includestdlib.hinti=1;//idtypedefstructmem{intstart;intend;structmem*next;}mem;typedefstructwork{intid;intsize;//memsizeintstart;structwork*next;}work;work*initwork(intsize){work*head=(work*)malloc(sizeof(head));head-id=i;head-start=1;head-size=size;head-next=NULL;returnhead;}work*insertwork(work*head,intstart,intsize){i++;work*pi,*pb;//piistheinsertone##pbisthepointpi=(work*)malloc(sizeof(work));pi-id=i;pi-start=start;pi-size=size;pi-next=NULL;if(head==NULL){head=pi;head-next=NULL;}pb=head;while(pb-next!=NULL){pb=pb-next;}pb-next=pi;returnhead;}mem*initmem(intsize){mem*head=(mem*)malloc(sizeof(mem));head-start=1;head-end=640;head-next=NULL;returnhead;}mem*insertmem(mem*head,intstart,intsize){mem*pi,*pb,*pf;intpbsize;pb=head;pbsize=pb-end-pb-start+1;pi=(mem*)malloc(sizeof(mem));pi-start=start;pi-end=size+start-1;if(pb==NULL){head=pi;pi-next=NULL;}else{while(pi-startpb-start&&pb-next!=NULL){pf=pb;pb=pb-next;}if(pi-startpb-start){if(pb==head){head=pi;//头节点pi-next=pb;}else{pf-next=pi;//其他位置pi-next=pb;}}else{pb-next=pi;pi-next=NULL;//在表末插入}}//合并相邻的内存pf=pb=head;while(pb-next!=NULL){if(pf-end+2pb-start){pf-end=pb-end;pf-next=pb-next;}pf=pb;pb=pb-next;}returnhead;}intgetstart(work*head,intsize){work*pb;pb=head;while(pb!=NULL){if(pb-size==size){returnpb-start;}pb=pb-next;}return0;}intalloc(mem*head,intsize){mem*pb;pb=head;inta;while(pb!=NULL){if(size=(pb-end-pb-start+1)){a=pb-start;pb-start=pb-start+size;returna;}pb=pb-next;}return0;}work*free1(work*head,intsize){work*pb,*pf;while(head==NULL){printf(nothisnod);gotoend;}pb=head;while(pb-size!=size&&pb-next!=NULL){pf=pb;pb=pb-next;}if(pb-size==size){if(pb==head)head=pb-next;elsepf-next=pb-next;}end:returnhead;}voidprintw(work*head){work*pb;pb=head;while(pb!=NULL){printf(idstartsize----\n);printf(%d%7d%8d\n,pb-id,pb-start,pb-size);pb=pb-next;}}voidprintm(mem*head){mem*pb;pb=head;while(pb!=NULL){printf(startend----\n);printf(%d%9d\n,pb-start,pb-end);pb=pb-next;}}voidmain(){intwrec;//接收返回的地址intmrec;mem*mhead;mhead=initmem(640);work*whead;//1whead=initwork(130);wrec=alloc(mhead,130);//2wrec=alloc(mhead,60);whead=insertwork(whead,wrec,60);//3wrec=alloc(mhead,100);whead=insertwork(whead,wrec,100);//4mrec=getstart(whead,60);whead=free1(whead,60);mhead=insertmem(mhead,mrec,60);//5wrec=alloc(mhead,200);whead=insertwork(whead,wrec,200);//6mrec=getstart(whead,100);whead=free1(whead,100);mhead=insertmem(mhead,mrec,100);//7mrec=getstart(whead,130);whead=free1(whead,130);mhead=insertmem(mhead,mrec,130);//8wrec=alloc(mhead,140);whead=insertwork(whead,wrec,140);//9wrec=alloc(mhead,60);whead=insertwork(whead,wrec,60);//10wrec=alloc(mhead,50);whead=insertwork(whead,wrec,50);//11mrec=getstart(whead,60);whead=free1(whead,60);mhead=insertmem(mhead,mrec,60);printf(作业的链表\n);printw(whead);printf(========================\n);printf(空闲分区链表\n);printm(mhead);}首次适应算法:#includestdio.hstructallocquery/*定义请求序列结构体*/{intnum;intstate;/*a表示申请,f表示释放*/intlength;};structallocqueryallocq[11];structfreequery/*定义内存分配队列*/{intflag;/*IDNo.0表示空闲,其他数值表示相应作业*/intfirstadd;/*起始地址*/intlength;/*占有长度*/};structfreequeryfreeq[11];/*首次适应算法函数*/voidfirst_alg(structallocqueryallocqnow,int*ptotal,structfreequery*pfreeq);main(){inti,j;FILE*fp;char*fname=c:\\a.txt;intFreetotal=1;fp=fopen(fname,w+);allocq[0].num=1;allocq[0].state='a';allocq[0].length=130;allocq[1].num=2;allocq[1].state='a';allocq[1].length=60;allocq[2].num=3;allocq[2].state='a';allocq[2].length=100;allocq[3].num=2;allocq[3].state='f';allocq[3].length=60;allocq[4].num=4;allocq[4].state='a';allocq[4].length=200;allocq[5].num=3;allocq[5].state='f';allocq[5].length=100;allocq[6].num=1;allocq[6].state='f';allocq[6].length=130;allocq[7].num=5;allocq[7].state='a';allocq[7].length=140;allocq[8].num=6;allocq[8].state='a';allocq[8].length=60;allocq[9].num=7;allocq[9].state='a';allocq[9].length=50;allocq[10].num=6;allocq[10].state='f';allocq[10].length=60;freeq[0].flag=0;freeq[0].firstadd=0;freeq[0].length=640;for(i=0;i11;i++){first_alg(allocq[i],&Freetotal,freeq);fprintf(fp,\nTotalfreeblocks:%d,Freetotal);fprintf(fp,\nIDNo.addresslength);for(j=0;freeq[j].length!=0;j++)fprintf(fp,\n%5d%10d%10d,freeq[j].flag,freeq[j].firstadd,freeq[j].length);}}voidfirst_alg(structallocqueryallocqnow,int*ptotal,structfreequery*pfreeq){inti,j,num;inttemp_num,temp_add,temp_length;structfreequerytemp_f1,temp_f2;if(allocqnow.state=='a')/*表示申请空间*/{for(i=0;i11;i++){if((pfreeq[i].flag==0)&(pfreeq[i].lengthallocqnow.length)){temp_num=pfreeq[i].flag;temp_add=pfreeq[i].firstadd+allocqnow.length;temp_length=pfreeq[i].length-allocqnow.length;pfreeq[i].flag=allocqnow.num;pfreeq[i].length=allocqnow.
本文标题:操作系统实验五
链接地址:https://www.777doc.com/doc-4221758 .html