操作系统实验五Word文档下载推荐.docx
《操作系统实验五Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《操作系统实验五Word文档下载推荐.docx(9页珍藏版)》请在冰豆网上搜索。
{
intstart;
intend;
structmem*next;
}mem;
typedefstructwork
intid;
intsize;
//memsize
structwork*next;
}work;
work*initwork(intsize)
work*head=(work*)malloc(sizeof(head));
head->
id=i;
start=1;
size=size;
next=NULL;
returnhead;
}
work*insertwork(work*head,intstart,intsize)
i++;
work*pi,*pb;
//piistheinsertone##pbisthepoint
pi=(work*)malloc(sizeof(work));
pi->
start=start;
if(head==NULL){head=pi;
pb=head;
while(pb->
next!
=NULL){pb=pb->
next;
pb->
next=pi;
mem*initmem(intsize)
mem*head=(mem*)malloc(sizeof(mem));
end=640;
mem*insertmem(mem*head,intstart,intsize)
mem*pi,*pb,*pf;
intpbsize;
pbsize=pb->
end-pb->
start+1;
pi=(mem*)malloc(sizeof(mem));
end=size+start-1;
if(pb==NULL){head=pi;
else
while(pi->
start>
start&
&
=NULL){pf=pb;
pb=pb->
if(pi->
start<
start)
{
if(pb==head)
head=pi;
//头节点
pi->
next=pb;
}
else
{
pf->
//其他位置
else
pb->
//在表末插入
}
//合并相邻的内存
pf=pb=head;
=NULL)
if(pf->
end+2>
end=pb->
end;
next=pb->
pf=pb;
pb=pb->
intgetstart(work*head,intsize)
work*pb;
while(pb!
if(pb->
size==size)
returnpb->
start;
return0;
intalloc(mem*head,intsize)
mem*pb;
inta;
if(size<
=(pb->
start+1))
a=pb->
start=pb->
start+size;
returna;
work*free1(work*head,intsize)
work*pb,*pf;
while(head==NULL){printf("
nothisnod"
);
gotoend;
size!
=size&
if(pb==head)head=pb->
elsepf->
end:
voidprintw(work*head)
printf("
id
start
size---->
\n"
%d%7d%8d\n"
pb->
id,pb->
start,pb->
size);
voidprintm(mem*head)
start
end---->
%d%9d\n"
end);
voidmain()
{
intwrec;
//接收返回的地址
intmrec;
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);
//3
wrec=alloc(mhead,100);
whead=insertwork(whead,wrec,100);
//4
mrec=getstart(whead,60);
whead=free1(whead,60);
mhead=insertmem(mhead,mrec,60);
//5
wrec=alloc(mhead,200);
whead=insertwork(whead,wrec,200);
//6
mrec=getstart(whead,100);
whead=free1(whead,100);
mhead=insertmem(mhead,mrec,100);
//7
mrec=getstart(whead,130);
whead=free1(whead,130);
mhead=insertmem(mhead,mrec,130);
//8
wrec=alloc(mhead,140);
whead=insertwork(whead,wrec,140);
//9
//10
wrec=alloc(mhead,50);
whead=insertwork(whead,wrec,50);
//11
mhead=insertmem(mhead,mrec,60);
printf("
作业的链表\n"
printw(whead);
========================\n"
空闲分区链表\n"
printm(mhead);
首次适应算法:
#include"
structallocquery/*定义请求序列结构体*/
intnum;
intstate;
/*a表示申请,f表示释放*/
intlength;
};
structallocqueryallocq[11];
structfreequery/*定义内存分配队列*/
intflag;
/*IDNo.0表示空闲,其他数值表示相应作业*/
intfirstadd;
/*起始地址*/
/*占有长度*/
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='
allocq[1].length=60;
allocq[2].num=3;
allocq[2].state='
allocq[2].length=100;
allocq[3].num=2;
allocq[3].state='
f'
allocq[3].length=60;
allocq[4].num=4;
allocq[4].state='
allocq[4].length=200;
allocq[5].num=3;
allocq[5].state='
allocq[5].length=100;
allocq[6].num=1;
allocq[6].state='
allocq[6].length=130;
allocq[7].num=5;
allocq[7].state='
allocq[7].length=140;
allocq[8].num=6;
allocq[8].state='
allocq[8].length=60;
allocq[9].num=7;
allocq[9].state='
allocq[9].length=50;
allocq[10].num=6;
allocq[10].state='
allocq[10].length=60;
freeq[0].flag=0;
freeq[0].firstadd=0;
freeq[0].length=640;
for(i=0;
i<
11;
i++)
first_alg(allocq[i],&
Freetotal,freeq);
fprintf(fp,"
\nTotalfreeblocks:
%d"
Freetotal);
\nIDNo.addresslength"
for(j=0;
freeq[j].length!
=0;
j++)
\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=='
)/*表示申请空间*/
for(i=0;
if((pfreeq[i].flag==0)&
(pfreeq[i].length>
allocqnow.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.length;
if(pfreeq[i+1].length==0)
pfreeq[i+1].flag=temp_num;
pfreeq[i+1].firstadd=temp_add;
pfreeq[i+1].length=temp_length;
else
if(pfreeq[i+1].firstadd!
=temp_add)
temp_f1.flag=temp_num;
temp_f1.firstadd=temp_add;
temp_f1.length=temp_length;
temp_f2=pfreeq[i+1];
for(j=i+1;
pfreeq[j].length!
pfreeq[j]=temp_f1;
temp_f1=temp_f2;
temp_f2=pfreeq[j+1];
break;
else/*释放空间*/
if(pfreeq[i].flag==allocqnow.num)
if((pfreeq[i-1].flag==0)&
(pfreeq[i+1].flag==0)&
(i>
0))
pfreeq[i-1].length=pfreeq[i-1].length+allocqnow.length+pfreeq[i+1].length;
for(j=i;
pfreeq[j].flag=pfreeq[j+2].flag;
pfreeq[j].firstadd=pfreeq[j].firstadd;
pfreeq[j].length=pfreeq[j].length;
elseif((pfreeq[i-1].flag==0)&
pfreeq[i-1].length=pfreeq[i-1].length+allocqnow.length;
pfreeq[j].flag=pfreeq[j+1].flag;
pfreeq[j].firstadd=pfreeq[j+1].firstadd;
pfreeq[j].length=pfreeq[j+1].length;
elseif(pfreeq[i+1].flag==0)
pfreeq[i].flag=0;
pfreeq[i].length=allocqnow.length+pfreeq[i+1].length;
num=0;
/*统计空闲块*/
pfreeq[i].length!
if(pfreeq[i].flag==0)num++;
*ptotal=num;
四,实验结果。
五、实验心得
通过这次实验,取用首次适应算法和最佳适应算法了解了动态分区分配方式中使用的数据结构和分配算法,以及了解了动态分区存储管理方式。