实验四 存储器管理 结果Word文档下载推荐.docx
《实验四 存储器管理 结果Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验四 存储器管理 结果Word文档下载推荐.docx(15页珍藏版)》请在冰豆网上搜索。
![实验四 存储器管理 结果Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2022-11/26/cb905ca8-6045-46ce-898a-a48431253e92/cb905ca8-6045-46ce-898a-a48431253e921.gif)
分配模块ffallocation,回收模块ffcollection,搬家模块coalesce及命令处理模块menu,menu用以模拟系统的输入,通过键盘命令选择进行分配模块、回收模块、内存查询以及退出的操作。
程序运行的流程如下图:
6.实验程序
#include<
stdio.h>
dos.h>
stdlib.h>
conio.h>
io.h>
#include"
string.h"
#defineTOTALSPACE5000/*起始空闲空间大小
#defineINITADDRESS2000/*起始地址
#defineMINSPACE100
#defineMAXJOBCNT10/*内存中作业数量最大值
structJobMat
{
charname[10];
intaddress;
intlength;
structJobMat*next;
structJobMat*back;
};
structFreeArea
intsize;
structFreeArea*next;
structFreeArea*back;
structJobMat*p_JobMat;
structFreeArea*p_FreeArea;
intUnusedSpace;
/*未分配的空闲空间大小
intCurrentJobCnt;
/*目前内存中的作业数量
intJobSize;
/*目前要分配的作业的大小
charJobName[10];
intJobAddress;
voidinitiation(){
p_FreeArea=(structFreeArea*)malloc(sizeof(structFreeArea));
p_FreeArea->
size=TOTALSPACE;
address=INITADDRESS;
next=NULL;
back=NULL;
UnusedSpace=TOTALSPACE;
p_JobMat=NULL;
CurrentJobCnt=0;
JobSize=0;
memset(JobName,0,10);
JobAddress=0;
}
voidffallocation()
structFreeArea*fp;
structJobMat*jp;
structJobMat*jp1;
structJobMat*jp2;
JobAddress=-1;
if(UnusedSpace<
JobSize)
{
/*printf("
Sorry,youinputjoblengthistoobig^_^\n"
);
*/
return;
}
fp=p_FreeArea;
while(fp!
=NULL)
if((fp->
size)<
JobSize)
{
fp=fp->
next;
}
else
CurrentJobCnt=CurrentJobCnt+1;
UnusedSpace=UnusedSpace-JobSize;
jp2=(structJobMat*)malloc(sizeof(structJobMat));
strcpy(jp2->
name,JobName);
jp2->
length=JobSize;
address=fp->
address;
JobAddress=jp2->
if(p_JobMat==NULL)
{
jp2->
p_JobMat=jp2;
}
else
jp=p_JobMat;
while((jp!
=NULL)&
&
(jp2->
address<
jp->
address))
{
jp1=jp;
jp=jp->
}
next=jp;
if(jp==NULL)
jp2->
back=jp1;
jp1->
next=jp2;
back=jp->
back;
if(jp->
back!
=NULL)jp1->
elsep_JobMat=jp2;
jp->
back=jp2;
if(fp->
size-JobSize<
MINSPACE)
if(fp->
next!
=NULL)fp->
next->
back=fp->
back->
next=fp->
elsep_FreeArea=fp->
fp->
size=fp->
size-JobSize;
address+JobSize;
/*exit
(1);
voidffcollection()
structFreeArea*fp;
structFreeArea*fp1;
structFreeArea*fp2;
intf;
jp=p_JobMat;
f=0;
while((jp!
=NULL)&
strcmp(jp->
name,JobName)!
=0)
jp=jp->
if(jp!
CurrentJobCnt=CurrentJobCnt-1;
UnusedSpace=UnusedSpace+jp->
length;
if(p_FreeArea==NULL)
p_FreeArea=(structFreeArea*)malloc(sizeof(structFreeArea));
p_FreeArea->
address=jp->
size=jp->
fp=p_FreeArea;
while((fp!
(fp->
fp1=fp;
fp=fp->
if(fp!
if((fp->
address==jp->
address+jp->
length))
f=f+1;
(jp->
address==fp1->
address+fp1->
size))
f=f+2;
elseif((jp->
address)==(fp1->
address+(fp1->
size)))
f=f+2;
switch(f)
case0:
fp2=(structFreeArea*)malloc(sizeof(structFreeArea));
fp2->
next=fp;
if(fp!
{
fp2->
if(fp->
=NULL)fp1->
next=fp2;
elsep_FreeArea=fp2;
fp->
back=fp2;
}
else
back=fp1;
fp1->
break;
case1:
fp->
size+jp->
case2:
fp1->
size=fp1->
case3:
length+fp->
size;
if(fp->
free(fp);
if(jp==p_JobMat)p_JobMat=jp->
if(jp->
=NULL){jp->
=NULL)jp->
next=jp->
free(jp);
voidshowyou()
/*clrscr();
if(CurrentJobCnt<
=0)
printf("
Nojob.\n"
else
namelength(b)addre\n"
jp=p_JobMat;
while(jp!
printf("
%s%d%d\n"
jp->
name,jp->
length,jp->
address);
jp=jp->
printf("
Thetotalleftis%dbytes\n"
UnusedSpace);
voidcoalesce()
intbottom;
if(CurrentJobCnt>
0)
bottom=TOTALSPACE+INITADDRESS;
jp->
address=bottom-jp->
bottom=bottom-jp->
fp=p_FreeArea;
while(fp!
fp1=fp;
free(fp1);
p_FreeArea=(structFreeArea*)malloc(sizeof(structFreeArea));
p_FreeArea->
size=UnusedSpace;
voidmenu()
intselect;
\n\n\n****************MENU***************\n"
Youcanselectoneofthefollowing:
\n"
(1)Requiretobeallocate.\n"
(2)Requiretocollectethesize.\n"
(3)Checkthememory.\n"
(4)Quit.\n"
***********************************\n"
scanf("
%d"
&
select);
switch(select)
case1:
if(CurrentJobCnt>
=MAXJOBCNT)
printf("
Thejobistoomany"
Enteryourjobname:
"
scanf("
%s"
JobName);
Enteryourjoblength:
JobSize);
ffallocation();
switch(JobAddress)
case-1:
printf("
thememoryisfull"
coalesce();
ffallocation();
default:
break;
case2:
Enterthenameofjob:
ffcollection();
case3:
case4:
exit
(1);
default:
Youinputawrongnumber!
voidmain()
initiation();
while
(1)
menu();
showyou();
7、实验结果截图并分析
结果分析:
1、超出分配单元总容量做出提示;
2、对于碎片,在单个碎片不足分配而所有空闲碎片总和可以分配时作出合并再分配。
8、实验心得
在本次实验中,熟悉了存储器管理的方法,加深了我对所学存储器管理方案的了解。