欢迎来到天天文库
浏览记录
ID:51422151
大小:25.90 KB
页数:10页
时间:2020-03-24
《操作系统实验五.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、暨南大学本科实验报告专用纸一,实验目的。了解动态分区分配方式中使用的数据结构和分配算法,并进一步加深对动态扮区存储管理方式及其实现过程的理解。二,实验内容。1分别实现首次适应算法和最佳适应算法的动态分区分配过程和回收过程。其中,空闲分区通过空闲区链进行管理;进行内存分配时,系统优先使用空闲区低端的空间。2、假设初试状态下,可用的内存空间为640KB,并有下列的请求序列:作业1申请130KB作业2申请60KB作业3申请100KB作业2释放60KB作业4申请200KB作业3释放100KB作业1释放130KB作业5申请140KB
2、作业6申请60KB作业7申请50KB作业6释放60KB3、每次分配和回收后显示空闲内存分区链情况。三,实验源代码。最佳适应算法:#include"stdio.h"#include"stdlib.h"inti=1;//idtypedefstructmem{intstart;intend;structmem*next;}mem;typedefstructwork{intid;intsize;//memsizeintstart;structwork*next;}work; work*initwork(intsize){work
3、*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
4、;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,intstar
5、t,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->start>pb->start&&pb->next!=NULL){pf=pb;pb=pb->next;} if(pi->startsta
6、rt) { 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+2>pb->start) {
7、 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
8、;while(pb!=NULL){ if(size<=(pb->end-pb->start+1)) { a=pb->start; pb->start=pb->start+size; returna; } pb=pb->next;} return0;}work*free
此文档下载收益归作者所有