操作系统实验五.docx_第1页
操作系统实验五.docx_第2页
操作系统实验五.docx_第3页
操作系统实验五.docx_第4页
操作系统实验五.docx_第5页
已阅读5页,还剩5页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

暨南大学本科实验报告专用纸一, 实验目的。了解动态分区分配方式中使用的数据结构和分配算法,并进一步加深对动态扮区存储管理方式及其实现过程的理解。二, 实验内容。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.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;head-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=pb-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+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-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; 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; return 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;while(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=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=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);/8 wrec=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);首次适应算法:#include stdio.h struct allocquery /* 定义请求序列结构体*/ int num; int state; /* a表示申请,f表示释放 */ int length; ; struct allocquery allocq11; 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 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.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=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_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=pfreeqi.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=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; pfreeqj.firstadd=pfreeqj.firstadd; pfreeqj.length=pfreeqj.length; else if (pfreeqi-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论