1实验四存储器管理1、目的与要求本实验的目的是让学生熟悉存储器管理的方法,加深对所学各种存储器管理方案的了解;要求采用一些常用的存储器分配算法,设计一个存储器管理模拟系统,模拟内存空间的分配和释放。2、实验内容①设计一个存放空闲块的自由链和一个内存作业分配表,存放内存中已经存在的作业。②编制一个按照首次适应法分配内存的算法,进行内存分配。③同时设计内存的回收以及内存清理(如果要分配的作业块大于任何一个空闲块,但小于总的空闲分区,则需要进行内存的清理,空出大块的空闲分区)的算法。3.实验环境①PC兼容机②Windows、DOS系统、Turboc2.0③C语言4.实验提示一、数据结构1、自由链内存空区采用自由链结构,链首由指针freep指向,链中各空区按地址递增次序排列。初启动时整个用户内存区为一个大空区,每个空区首部设置一个区头(freearea)结构,区头信息包括:Size空区大小Next前向指针,指向下一个空区Back反向指针,指向上一个空区Adderss本空区首地址2、内存分配表JOBMAT系统设置一个MAT,每个运行的作业都在MAT中占有一个表目,回收分区时清除相应表目,表目信息包括:Name用户作业名Length作业区大小Addr作业区首地址二、算法存储分配算法采用首次适应法,根据指针freep查找自由链,当找到第一块可满足分配请求的空区便分配,当某空区被分配后的剩余空闲空间大于所规定的碎片最小量mini时,则形成一个较小的空区留在自由链中。2回收时,根据MAT将制定分区链入自由链,若该分区有前邻或后邻分区,则将他们拼成一个较大的空区。当某个分配请求不能被满足,但此时系统中所有碎片总容量满足分配请求的容量时,系统立即进行内存搬家,消除碎片。即将各作业占用区集中下移到用户内存区的下部(高地址部分),形成一片连续的作业区,而在用户内存区的上部形成一块较大的空闲,然后再进行分配。本系统的主要程序模块包括:分配模块ffallocation,回收模块ffcollection,搬家模块coalesce及命令处理模块menu,menu用以模拟系统的输入,通过键盘命令选择进行分配模块、回收模块、内存查询以及退出的操作。程序运行的流程如下图:5.实验运行情况****************MENU***************Youcanselectoneofthefollowing:(1)Requiretobeallocate.请分配内存(2)Requiretocollectethesize.将分配的内存回收(3)Checkthememory.检查当前的内存情况(4)Quit.退出***********************************1Enteryourjobname:JOB1Enteryourjoblength:1000namelength(b)addreJOB110002000Thetotalleftis4000bytes6.实验程序#includestdio.h#includedos.h#includestdlib.h#includeconio.h#includeio.h#includestring.h#defineTOTALSPACE5000/*起始空闲空间大小#defineINITADDRESS2000/*起始地址#defineMINSPACE100#defineMAXJOBCNT10/*内存中作业数量最大值structJobMat3{charname[10];intaddress;intlength;structJobMat*next;structJobMat*back;};structFreeArea{intaddress;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;p_FreeArea-address=INITADDRESS;p_FreeArea-next=NULL;p_FreeArea-back=NULL;UnusedSpace=TOTALSPACE;p_JobMat=NULL;CurrentJobCnt=0;JobSize=0;memset(JobName,0,10);JobAddress=0;}voidffallocation()4{structFreeArea*fp;structJobMat*jp;structJobMat*jp1;structJobMat*jp2;JobAddress=-1;if(UnusedSpaceJobSize){/*printf(Sorry,youinputjoblengthistoobig^_^\n);*/return;}JobAddress=0;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;jp2-address=fp-address;JobAddress=jp2-address;if(p_JobMat==NULL){jp2-next=NULL;jp2-back=NULL;p_JobMat=jp2;}else{jp=p_JobMat;while((jp!=NULL)&&(jp2-addressjp-address)){jp1=jp;jp=jp-next;}jp2-next=jp;if(jp==NULL){5jp2-back=jp1;jp1-next=jp2;}else{jp2-back=jp-back;if(jp-back!=NULL)jp1-next=jp2;elsep_JobMat=jp2;jp-back=jp2;}}if(fp-size-JobSizeMINSPACE){if(fp-next!=NULL)fp-next-back=fp-back;if(fp-back!=NULL)fp-back-next=fp-next;elsep_FreeArea=fp-next;}else{fp-size=fp-size-JobSize;fp-address=fp-address+JobSize;}/*exit(1);*/return;}}voidffcollection(){structFreeArea*fp;structFreeArea*fp1;structFreeArea*fp2;structJobMat*jp;intf;jp=p_JobMat;f=0;while((jp!=NULL)&&strcmp(jp-name,JobName)!=0){jp=jp-next;}if(jp!=NULL){CurrentJobCnt=CurrentJobCnt-1;UnusedSpace=UnusedSpace+jp-length;6if(p_FreeArea==NULL){p_FreeArea=(structFreeArea*)malloc(sizeof(structFreeArea));p_FreeArea-address=jp-address;p_FreeArea-size=jp-length;p_FreeArea-next=NULL;p_FreeArea-back=NULL;}else{fp=p_FreeArea;while((fp!=NULL)&&(fp-addressjp-address)){fp1=fp;fp=fp-next;}if(fp!=NULL){if((fp-next!=NULL)&&(fp-next-address==jp-address+jp-length)){f=f+1;}if((fp-back!=NULL)&&(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-address=jp-address;fp2-size=jp-length;fp2-next=fp;if(fp!=NULL){fp2-back=fp-back;if(fp-back!=NULL)fp1-next=fp2;elsep_FreeArea=fp2;fp-back=fp2;7}else{fp2-back=fp1;fp1-next=fp2;}break;case1:fp-size=fp-size+jp-length;fp-address=jp-address;break;case2:fp1-size=fp1-size+jp-length;break;case3:fp1-size=fp1-size+jp-length+fp-size;fp1-next=fp-next;if(fp-next!=NULL){fp-next-back=fp2;}free(fp);break;}}if(jp==p_JobMat)p_JobMat=jp-next;if(jp-next!=NULL){jp-next-back=jp-back;}if(jp-back!=NULL)jp-back-next=jp-next;free(jp);}}voidshowyou(){structJobMat*jp;/*clrscr();*/if(CurrentJobCnt=0){printf(Nojob.\n);}else{printf(namelength(b)addre\n);jp=p_JobMat;8while(jp!=NULL){printf(%s%d%d\n,jp-name,jp-length,jp-address);jp=jp-next;}}printf(Thetotalleftis%dbytes\n,UnusedSpace);}voidcoalesce(){structFreeArea*fp;structFreeArea*fp1;structJobMat*jp;intbottom;if(CurrentJobCnt0){jp=p_JobMat;bottom=TOTALSPACE+INITADDRESS;while(jp!=NULL){jp-address=bottom-jp-length;bottom=bottom-jp-length;jp=jp-next;}fp=p_FreeArea;while(fp!=NULL){fp1=fp;fp=fp-next;free(fp1);}p_FreeArea=(structFreeArea*)malloc(sizeof(structFreeArea));p_FreeArea-size