《实验四-存储器管理.doc》由会员分享,可在线阅读,更多相关《实验四-存储器管理.doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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.hinclude dos.hinclude stdlib。hinclude conio.h#include back=NULL; UnusedSpace=TOTALSPACE; p_JobMat=NULL; CurrentJobCnt=0; JobSize=0; memset(JobName, 0, 10); JobAddress=0;void ffallocation()struct FreeArea fp;struct JobMat jp;struct JobMat jp1;struct JobMat *jp2;JobAddres
6、s=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) name,JobName);jp2length=JobSize;jp2address=fp-address;JobAddress=jp2-address;if (p_JobMat=NULL)jp2next=NULL;jp2-back=NULL;p_JobMat=jp2;elsejp=p_JobMat;while
7、(jp!=NULL)&(jp2addressjp-address))jp1=jp;jp=jp-next;jp2next=jp;if (jp=NULL)jp2back=jp1;jp1next=jp2; elsejp2-back=jp-back;if (jp-back!=NULL) jp1-next=jp2; else p_JobMat=jp2; jp-back=jp2; if (fpsizeJobSizeback=fpback;if (fp-back!=NULL) fpbacknext=fp-next;else p_FreeArea=fpnext;elsefp-size=fpsize-JobSi
8、ze;fpaddress=fpaddress+JobSize;/*exit(1);*/return;void ffcollection()struct FreeArea fp;struct FreeArea *fp1;struct FreeArea *fp2;struct JobMat *jp;int f;jp=p_JobMat;f=0;while((jp!=NULL) & strcmp(jpname, JobName) != 0)jp=jp-next;if (jp!=NULL)CurrentJobCnt=CurrentJobCnt-1;UnusedSpace=UnusedSpace+jple
9、ngth;if (p_FreeArea=NULL) p_FreeArea=(struct FreeArea *)malloc(sizeof(struct FreeArea); p_FreeAreaaddress=jp-address; p_FreeArea-size=jp-length; p_FreeAreanext=NULL; p_FreeArea-back=NULL;elsefp=p_FreeArea;while((fp!=NULL)&(fpaddressjp-address)fp1=fp;fp=fpnext;if (fp!=NULL)if ((fp-next!=NULL)&(fp-nex
10、t-address=jpaddress+jplength)f=f+1;if ((fpback!=NULL)&(jp-address=fp1address+fp1size))f=f+2;else if ((jpaddress) = (fp1-address+(fp1size)) f=f+2;switch (f)case 0:fp2=(struct FreeArea )malloc(sizeof(struct FreeArea);fp2address=jp-address;fp2-size=jplength;fp2next=fp;if (fp!=NULL)fp2-back=fpback;if (f
11、p-back!=NULL) fp1next=fp2;else p_FreeArea=fp2;fp-back=fp2;elsefp2back=fp1;fp1-next=fp2;break;case 1:fp-size=fpsize+jplength;fpaddress=jpaddress;break;case 2:fp1size=fp1size+jplength;break;case 3:fp1-size=fp1-size+jp-length+fpsize;fp1next=fp-next;if (fpnext!=NULL)fpnext-back=fp2;free (fp);break;if (j
12、p=p_JobMat) p_JobMat=jpnext;if (jpnext!=NULL) jpnextback=jpback;if (jpback!=NULL) jpbacknext=jpnext;free(jp);void showyou()struct JobMat jp;/*clrscr();/if (CurrentJobCntname, jp-length,jp-address);jp=jp-next;printf(”The total left is %d bytesn,UnusedSpace);void coalesce()struct FreeArea fp;struct Fr
13、eeArea *fp1;struct JobMat jp;int bottom;if (CurrentJobCnt0)jp=p_JobMat;bottom=TOTALSPACE+INITADDRESS;while (jp!=NULL)jpaddress=bottomjp-length;bottom=bottom-jplength;jp=jp-next;fp=p_FreeArea;while(fp!=NULL)fp1=fp;fp=fp-next;free(fp1);p_FreeArea=(struct FreeArea )malloc(sizeof(struct FreeArea);p_Free
14、Areasize=UnusedSpace;p_FreeAreaaddress=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
15、((4)Quit。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。实验结果11
限制150内