1、typedef struct workint id;int size;/memsizestruct work *next;work;work* initwork(int size)work *head=(work *)malloc(sizeof(head);head-id=i;start=1;size=size;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(siz
2、eof(work);pi-start=start;if(head=NULL)head=pi;pb=head;while(pb-next!=NULL)pb=pb-next;pb-next=pi;mem *initmem(int size)mem *head=(mem*)malloc(sizeof(mem);end=640;mem *insertmem(mem *head,int start,int size)mem *pi,*pb,*pf;int pbsize;pbsize=pb-end-pb-start+1;pi=(mem*)malloc(sizeof(mem);end=size+start-
3、1;if(pb=NULL)head=pi;else while(pi-startstart&=NULL)pf=pb;pb=pb- if(pi-startnext=pb; else pf-/其他位置 else pb-/在表末插入 /合并相邻的内存 pf=pb=head;=NULL) if(pf-end+2end=pb-end;next=pb- pf=pb; pb=pb-int getstart(work*head,int size)work *pb;while(pb! if(pb-size=size) return pb-start; return 0;int alloc(mem *head,i
4、nt size)mem *pb;int a; if(sizestart+1) a=pb-start=pb-start+size; return a;work*free1(work *head,int size)work *pb,*pf;while(head=NULL)printf(no this nod);goto end;size!=size& if(pb=head)head=pb- else pf-end:void printw(work *head) printf(id start size-n%d%7d%8dn,pb-id,pb-start,pb-size);void printm(m
5、em *head)start end-%d%9dnend);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=
6、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
7、=insertwork(whead,wrec,140);/9/10wrec=alloc(mhead,50);whead=insertwork(whead,wrec,50);/11mhead=insertmem(mhead,mrec,60);printf(作业的链表nprintw(whead);=n空闲分区链表nprintm(mhead);首次适应算法:#include struct allocquery /* 定义请求序列结构体*/ int num;int state; /* a表示申请,f表示释放 */ int length;struct allocquery allocq11;struct
8、 freequery /* 定义内存分配队列 */ int flag; /* IDNo. 0表示空闲,其他数值表示相应作业 */ int firstadd; /* 起始地址 */ /* 占有长度 */ 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+al
9、locq0.num=1; allocq0.state=a allocq0.length=130;allocq1.num=2; allocq1.state= allocq1.length=60;allocq2.num=3; allocq2.state= allocq2.length=100;allocq3.num=2; allocq3.state=f allocq3.length=60;allocq4.num=4; allocq4.state= allocq4.length=200;allocq5.num=3; allocq5.state= allocq5.length=100;allocq6.
10、num=1; allocq6.state= allocq6.length=130;allocq7.num=5; allocq7.state= allocq7.length=140;allocq8.num=6; allocq8.state= allocq8.length=60;allocq9.num=7; allocq9.state= allocq9.length=50;allocq10.num=6; allocq10.state= allocq10.length=60;freeq0.flag=0; freeq0.firstadd=0; freeq0.length=640;for(i=0;ial
11、locqnow.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 (pf
12、reeqi+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!pfreeqj=temp_f1;temp_f1=temp_f2;temp_f2=pfreeqj+1;break;else /* 释放空间*/ if (pfreeqi.flag=allocqnow.num) if (pfreeqi-1.flag=0)&(pfreeqi+1.flag=0)&(i0) pf
13、reeqi-1.length=pfreeqi-1.length+allocqnow.length+pfreeqi+1.length;for (j=i;pfreeqj.flag=pfreeqj+2.flag;pfreeqj.firstadd=pfreeqj.firstadd;pfreeqj.length=pfreeqj.length;else if (pfreeqi-1.flag=0)&pfreeqi-1.length=pfreeqi-1.length+allocqnow.length;pfreeqj.flag=pfreeqj+1.flag;pfreeqj.firstadd=pfreeqj+1.firstadd;pfreeqj.length=pfreeqj+1.length;else if (pfreeqi+1.flag=0) pfreeqi.flag=0;pfreeqi.length=allocqnow.length+pfreeqi+1.length;num=0; /* 统计空闲块*/ pfreeqi.length!if (pfreeqi.flag=0) num+;*ptotal=num;四, 实验结果。五、实验心得通过这次实验,取用首次适应算法和最佳适应算法了解了动态分区分配方式中使用的数据结构和分配算法,以及了解了动态分区存储管理方式。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1