1、实验四 存储器管理1、目的与要求 本实验的目的是让学生熟悉存储器管理的方法,加深对所学各种存储器管理方案的了解;要求采用一些常用的存储器分配算法,设计一个存储器管理模拟系统,模拟内存空间的分配和释放。2、实验内容设计一个存放空闲块的自由链和一个内存作业分配表,存放内存中已经存在的作业。编制一个按照首次适应法分配内存的算法,进行内存分配。同时设计内存的回收以及内存清理(如果要分配的作业块大于任何一个空闲块,但小于总的空闲分区,则需要进行内存的清理,空出大块的空闲分区)的算法。3实验环境PC兼容机Windows、DOS系统、Turbo c 2。0C语言4实验提示一、数据结构1、自由链内存空区采用自
2、由链结构,链首由指针freep指向,链中各空区按地址递增次序排列.初启动时整个用户内存区为一个大空区,每个空区首部设置一个区头(freearea)结构,区头信息包括:Size 空区大小Next 前向指针,指向下一个空区Back 反向指针,指向上一个空区Adderss 本空区首地址2、内存分配表JOBMAT系统设置一个MAT,每个运行的作业都在MAT中占有一个表目,回收分区时清除相应表目,表目信息包括:Name 用户作业名Length 作业区大小Addr 作业区首地址二、算法存储分配算法采用首次适应法,根据指针freep查找自由链,当找到第一块可满足分配请求的空区便分配,当某空区被分配后的剩余空
3、闲空间大于所规定的碎片最小量mini时,则形成一个较小的空区留在自由链中。回收时,根据MAT将制定分区链入自由链,若该分区有前邻或后邻分区,则将他们拼成一个较大的空区。当某个分配请求不能被满足,但此时系统中所有碎片总容量满足分配请求的容量时,系统立即进行内存搬家,消除碎片.即将各作业占用区集中下移到用户内存区的下部(高地址部分),形成一片连续的作业区,而在用户内存区的上部形成一块较大的空闲,然后再进行分配。本系统的主要程序模块包括:分配模块ffallocation,回收模块ffcollection,搬家模块coalesce及命令处理模块menu,menu用以模拟系统的输入,通过键盘命令选择进行
4、分配模块、回收模块、内存查询以及退出的操作。程序运行的流程如下图:5实验运行情况*MENU*You can select one of the following:(1)Require to be allocate。 请分配内存(2)Require to collecte the size。 将分配的内存回收(3)Check the memory. 检查当前的内存情况(4)Quit。 退出*1Enter your job name:JOB1Enter your job length:1000name length(b) addreJOB1 1000 2000The total left is
5、4000 bytes6实验程序include stdio。h#include dos。h#include stdlib.hinclude size=TOTALSPACE; p_FreeArea-address=INITADDRESS; p_FreeAreanext=NULL; p_FreeArea-back=NULL; UnusedSpace=TOTALSPACE; p_JobMat=NULL; CurrentJobCnt=0; JobSize=0; memset(JobName, 0, 10); JobAddress=0;void ffallocation()struct FreeArea
6、fp;struct JobMat jp;struct JobMat *jp1;struct JobMat jp2;JobAddress=1;if (UnusedSpaceJobSize)/printf(Sorry, you input job length is too big _ n”);/return;JobAddress=0;fp=p_FreeArea;while (fp!=NULL)if((fp-size) JobSize)fp=fp-next;elseCurrentJobCnt=CurrentJobCnt+1;UnusedSpace=UnusedSpace-JobSize;jp2=(
7、struct JobMat )malloc(sizeof(struct JobMat));strcpy(jp2name,JobName);jp2length=JobSize;jp2-address=fp-address;JobAddress=jp2address;if (p_JobMat=NULL)jp2next=NULL;jp2-back=NULL;p_JobMat=jp2;elsejp=p_JobMat;while(jp!=NULL)(jp2addressjp-address)jp1=jp;jp=jpnext;jp2next=jp;if (jp=NULL)jp2-back=jp1;jp1n
8、ext=jp2; elsejp2back=jp-back;if (jp-back!=NULL) jp1next=jp2; else p_JobMat=jp2; jpback=jp2; if (fpsize-JobSizeMINSPACE)if (fpnext!=NULL) fpnext-back=fp-back;if (fp-back!=NULL) fpback-next=fp-next;else p_FreeArea=fp-next;elsefpsize=fp-sizeJobSize;fp-address=fpaddress+JobSize;/exit(1);/return;void ffc
9、ollection()struct FreeArea fp;struct FreeArea *fp1;struct FreeArea *fp2;struct JobMat *jp;int f;jp=p_JobMat;f=0;while((jp!=NULL) & strcmp(jp-name, JobName) != 0)jp=jpnext;if (jp!=NULL)CurrentJobCnt=CurrentJobCnt-1;UnusedSpace=UnusedSpace+jp-length;if (p_FreeArea=NULL) p_FreeArea=(struct FreeArea )ma
10、lloc(sizeof(struct FreeArea); p_FreeArea-address=jp-address; p_FreeAreasize=jplength; p_FreeAreanext=NULL; p_FreeArea-back=NULL;elsefp=p_FreeArea;while((fp!=NULL)&(fpaddressnextaddress=jp-address+jp-length)f=f+1;if ((fpback!=NULL)(jp-address=fp1-address+fp1size)f=f+2;else if (jp-address) = (fp1-addr
11、ess+(fp1size))) f=f+2;switch (f)case 0:fp2=(struct FreeArea )malloc(sizeof(struct FreeArea));fp2address=jpaddress;fp2-size=jplength;fp2next=fp;if (fp!=NULL)fp2-back=fp-back;if (fpback!=NULL) fp1next=fp2;else p_FreeArea=fp2;fp-back=fp2;elsefp2-back=fp1;fp1next=fp2;break;case 1:fpsize=fp-size+jplength
12、;fp-address=jpaddress;break;case 2:fp1-size=fp1-size+jplength;break;case 3:fp1size=fp1size+jp-length+fpsize;fp1next=fp-next;if (fp-next!=NULL)fpnext-back=fp2;free (fp);break;if (jp=p_JobMat) p_JobMat=jpnext;if (jp-next!=NULL) jpnext-back=jpback;if (jpback!=NULL) jp-backnext=jpnext;free(jp);void show
13、you()struct JobMat jp;/clrscr();*/if (CurrentJobCnt=0)printf(”No job.n);elseprintf(”name length(b) addren”);jp=p_JobMat;while (jp!=NULL)printf(”%s %d %dn”, jp-name, jplength,jp-address);jp=jpnext;printf(”The total left is %d bytesn”,UnusedSpace);void coalesce()struct FreeArea *fp;struct FreeArea fp1
14、;struct JobMat *jp;int bottom;if (CurrentJobCnt0)jp=p_JobMat;bottom=TOTALSPACE+INITADDRESS;while (jp!=NULL)jpaddress=bottomjplength;bottom=bottom-jp-length;jp=jp-next;fp=p_FreeArea;while(fp!=NULL)fp1=fp;fp=fpnext;free(fp1);p_FreeArea=(struct FreeArea )malloc(sizeof(struct FreeArea));p_FreeAreasize=U
15、nusedSpace;p_FreeArea-address=INITADDRESS;p_FreeAreanext=NULL;p_FreeArea-back=NULL;void menu()int select;printf(”nnn*MENU*n”);printf(”You can select one of the following:n);printf((1)Require to be allocate。n”);printf(”(2)Require to collecte the size。n);printf(”(3)Check the memory.n”);printf((4)Quit。
16、n);printf(*n”);scanf(”d”,&select);switch(select)case 1:if( CurrentJobCnt=MAXJOBCNT)printf(”The job is too many”);elseprintf(Enter your job name:);scanf(”s,JobName);printf(”Enter your job length:”);scanf(d”,JobSize);ffallocation();switch (JobAddress)case 1:printf(”the memory is full”);break;case 0:coalesce();ffallocation();break;default:break;break;case 2:printf(”Enter the name of job:”);scanf(”s”,JobName);ffcollection();break;case 3:break;case 4:exit(1);break;default:printf(”You input a wrong number!n);break;void main()initiation();while (1)menu();showyou();7.实验结果10
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100