1、操作系统实验五暨南大学本科实验报告专用纸一, 实验目的。了解动态分区分配方式中使用的数据结构和分配算法,并进一步加深对动态扮区存储管理方式及其实现过程的理解。二, 实验内容。1分别实现首次适应算法和最佳适应算法的动态分区分配过程和回收过程。其中,空闲分区通过空闲区链进行管理;进行内存分配时,系统优先使用空闲区低端的空间。2、假设初试状态下,可用的内存空间为640KB,并有下列的请求序列:作业1申请 130KB作业2申请 60KB作业3申请 100KB作业2释放 60KB作业4申请 200KB作业3释放 100KB作业1释放 130KB作业5申请 140KB作业6申请 60KB作业7申请 50K
2、B作业6释放 60KB3、每次分配和回收后显示空闲内存分区链情况。三, 实验源代码。最佳适应算法:#includestdio.h#includestdlib.hint i=1;/idtypedef struct memint start;int end;struct mem *next;mem;typedef struct workint id;int size;/memsizeint start;struct work *next;work;work* initwork(int size)work *head=(work *)malloc(sizeof(head);head-id=i;hea
3、d-start=1;head-size=size;head-next=NULL;return head;work* insertwork(work *head,int start,int size)i+;work *pi,*pb;/pi is the insert one #pb is the pointpi=(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=p
4、b-next;pb-next=pi;return head;mem *initmem(int size)mem *head=(mem*)malloc(sizeof(mem);head-start=1;head-end=640;head-next=NULL;return head;mem *insertmem(mem *head,int start,int size)mem *pi,*pb,*pf;int pbsize;pb=head;pbsize=pb-end-pb-start+1;pi=(mem*)malloc(sizeof(mem);pi-start=start;pi-end=size+s
5、tart-1;if(pb=NULL)head=pi;pi-next=NULL;else while(pi-startpb-start&pb-next!=NULL)pf=pb;pb=pb-next; if(pi-startstart) 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
6、; pf-next=pb-next; pf=pb; pb=pb-next;return head;int getstart(work*head,int size)work *pb;pb=head;while(pb!=NULL) if(pb-size=size) return pb-start; pb=pb-next; return 0; int alloc(mem *head,int size)mem *pb;pb=head;int a;while(pb!=NULL) if(sizeend-pb-start+1) a=pb-start; pb-start=pb-start+size; retu
7、rn a; pb=pb-next; return 0; work*free1(work *head,int size)work *pb,*pf;while(head=NULL)printf(no this nod);goto end;pb=head;while(pb-size!=size&pb-next!=NULL) pf=pb; pb=pb-next; if(pb-size=size) if(pb=head)head=pb-next; else pf-next=pb-next; end:return head;void printw(work *head)work *pb;pb=head;w
8、hile(pb!=NULL) printf(id start size-n); printf(%d%7d%8dn,pb-id,pb-start,pb-size); pb=pb-next;void printm(mem *head)mem *pb;pb=head;while(pb!=NULL) printf(start end-n); printf(%d%9dn,pb-start,pb-end); pb=pb-next;void main() int wrec;/接收返回的地址int mrec;mem *mhead;mhead=initmem(640);work *whead;/1 whead=
9、initwork(130);wrec=alloc(mhead,130);/2 wrec=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);/6 mrec=getsta
10、rt(whead,100);whead=free1(whead,100);mhead=insertmem(mhead,mrec,100);/7mrec=getstart(whead,130);whead=free1(whead,130);mhead=insertmem(mhead,mrec,130);/8 wrec=alloc(mhead,140);whead=insertwork(whead,wrec,140);/9wrec=alloc(mhead,60);whead=insertwork(whead,wrec,60);/10wrec=alloc(mhead,50);whead=insert
11、work(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);首次适应算法:#include stdio.h struct allocquery /* 定义请求序列结构体*/ int num; int state; /* a表示申请,f表示释放 */ int length; ; struct allocquery all
12、ocq11; struct freequery /* 定义内存分配队列 */ int flag; /* IDNo. 0表示空闲,其他数值表示相应作业 */ int firstadd; /* 起始地址 */ int length; /* 占有长度 */ ; struct freequery freeq11; /* 首次适应算法函数*/ void first_alg(struct allocquery allocqnow,int *ptotal,struct freequery *pfreeq); main() int i,j; FILE *fp; char *fname=c:a.txt; int
13、 Freetotal=1; fp=fopen(fname,w+); allocq0.num=1; allocq0.state=a; allocq0.length=130; allocq1.num=2; allocq1.state=a; allocq1.length=60; allocq2.num=3; allocq2.state=a; allocq2.length=100; allocq3.num=2; allocq3.state=f; allocq3.length=60; allocq4.num=4; allocq4.state=a; allocq4.length=200; allocq5.
14、num=3; allocq5.state=f; allocq5.length=100; allocq6.num=1; allocq6.state=f; allocq6.length=130; allocq7.num=5; allocq7.state=a; allocq7.length=140; allocq8.num=6; allocq8.state=a; allocq8.length=60; allocq9.num=7; allocq9.state=a; allocq9.length=50; allocq10.num=6; allocq10.state=f; allocq10.length=
15、60; freeq0.flag=0; freeq0.firstadd=0; freeq0.length=640; for(i=0;i11;i+) first_alg(allocqi,&Freetotal,freeq); fprintf(fp,nTotal free blocks:%d ,Freetotal) ; fprintf(fp,nIDNo. address length); for(j=0;freeqj.length!=0;j+) fprintf(fp,n%5d%10d%10d,freeqj.flag,freeqj.firstadd,freeqj.length); void first_
16、alg(struct allocquery allocqnow,int *ptotal,struct freequery *pfreeq) int i,j,num; int temp_num,temp_add,temp_length; struct freequery temp_f1,temp_f2; if (allocqnow.state=a) /* 表示申请空间 */ for (i=0;iallocqnow.length) temp_num=pfreeqi.flag; temp_add=pfreeqi.firstadd+allocqnow.length; temp_length=pfree
17、qi.length-allocqnow.length; pfreeqi.flag=allocqnow.num; pfreeqi.length=allocqnow.length; if (pfreeqi+1.length=0) pfreeqi+1.flag=temp_num; pfreeqi+1.firstadd=temp_add; pfreeqi+1.length=temp_length; else if (pfreeqi+1.firstadd!=temp_add) temp_f1.flag=temp_num; temp_f1.firstadd=temp_add; temp_f1.length
18、=temp_length; temp_f2=pfreeqi+1; for (j=i+1;pfreeqj.length!=0;j+) pfreeqj=temp_f1; temp_f1=temp_f2; temp_f2=pfreeqj+1; pfreeqj=temp_f1; break; else /* 释放空间*/ for (i=0;i0) pfreeqi-1.length=pfreeqi-1.length+allocqnow.length+pfreeqi+1.length; for (j=i;pfreeqj.length!=0;j+) pfreeqj.flag=pfreeqj+2.flag;
19、pfreeqj.firstadd=pfreeqj.firstadd; pfreeqj.length=pfreeqj.length; else if (pfreeqi-1.flag=0)&(i0) pfreeqi-1.length=pfreeqi-1.length+allocqnow.length; for (j=i;pfreeqj.length!=0;j+) pfreeqj.flag=pfreeqj+1.flag; pfreeqj.firstadd=pfreeqj+1.firstadd; pfreeqj.length=pfreeqj+1.length; else if (pfreeqi+1.f
20、lag=0) pfreeqi.flag=0; pfreeqi.length=allocqnow.length+pfreeqi+1.length; for (j=i+1;pfreeqj.length!=0;j+) pfreeqj.flag=pfreeqj+1.flag; pfreeqj.firstadd=pfreeqj+1.firstadd; pfreeqj.length=pfreeqj+1.length; else pfreeqi.flag=0; num=0; /* 统计空闲块*/ for (i=0;pfreeqi.length!=0;i+) if (pfreeqi.flag=0) num+; *ptotal=num; 四, 实验结果。五、实验心得通过这次实验,取用首次适应算法和最佳适应算法了解了动态分区分配方式中使用的数据结构和分配算法,以及了解了动态分区存储管理方式。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1