实验四存储器管理系统文档格式.docx
《实验四存储器管理系统文档格式.docx》由会员分享,可在线阅读,更多相关《实验四存储器管理系统文档格式.docx(16页珍藏版)》请在冰点文库上搜索。
程序运行的流程如下图:
5.实验运行情况
****************MENU***************
Youcanselectoneofthefollowing:
(1)Requiretobeallocate.请分配内存
(2)Requiretocollectethesize.将分配的内存回收
(3)Checkthememory.检查当前的内存情况
(4)Quit.退出
***********************************
1
Enteryourjobname:
JOB1
Enteryourjoblength:
1000
namelength(b)addre
JOB110002000
Thetotalleftis4000bytes
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"
\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"
"
scanf("
%s"
JobName);
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.实验结果