实验四 存储器管理.docx
- 文档编号:29855374
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:21
- 大小:51.47KB
实验四 存储器管理.docx
《实验四 存储器管理.docx》由会员分享,可在线阅读,更多相关《实验四 存储器管理.docx(21页珍藏版)》请在冰豆网上搜索。
实验四存储器管理
实验四存储器管理
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时,则形成一个较小的空区留在自由链中。
回收时,根据MAT将制定分区链入自由链,若该分区有前邻或后邻分区,则将他们拼成一个较大的空区。
当某个分配请求不能被满足,但此时系统中所有碎片总容量满足分配请求的容量时,系统立即进行内存搬家,消除碎片。
即将各作业占用区集中下移到用户内存区的下部(高地址部分),形成一片连续的作业区,而在用户内存区的上部形成一块较大的空闲,然后再进行分配。
本系统的主要程序模块包括:
分配模块ffallocation,回收模块ffcollection,搬家模块coalesce及命令处理模块menu,menu用以模拟系统的输入,通过键盘命令选择进行分配模块、回收模块、内存查询以及退出的操作。
程序运行的流程如下图:
5.实验运行情况
****************MENU***************
Youcanselectoneofthefollowing:
(1)Requiretobeallocate.请分配内存
(2)Requiretocollectethesize.将分配的内存回收
(3)Checkthememory.检查当前的内存情况
(4)Quit.退出
***********************************
1
Enteryourjobname:
JOB1
Enteryourjoblength:
1000
namelength(b)addre
JOB110002000
Thetotalleftis4000bytes
6.实验程序
#include
#include
#include
#include
#include
#include"string.h"
#defineTOTALSPACE5000/*起始空闲空间大小
#defineINITADDRESS2000/*起始地址
#defineMINSPACE100
#defineMAXJOBCNT10/*内存中作业数量最大值
structJobMat
{
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()
{
structFreeArea*fp;
structJobMat*jp;
structJobMat*jp1;
structJobMat*jp2;
JobAddress=-1;
if(UnusedSpace { /*printf("Sorry,youinputjoblengthistoobig^_^\n");*/ return; } JobAddress=0; fp=p_FreeArea; while(fp! =NULL) if((fp->size) { 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->address { jp1=jp; jp=jp->next; } jp2->next=jp; if(jp==NULL) { jp2->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-JobSize { 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; if(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->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; } 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; while(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(CurrentJobCnt>0) { 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=UnusedSpace; p_FreeArea->address=INITADDRESS; p_FreeArea->next=NULL; p_FreeArea->back=NULL; } } voidmenu() { intselect; printf("\n\n\n****************MENU***************\n"); printf("Youcanselectoneofthefollowing: \n"); printf(" (1)Requiretobeallocate.\n"); printf(" (2)Requiretocollectethesize.\n"); printf("(3)Checkthememory.\n"); printf("(4)Quit.\n"); printf("***********************************\n"); scanf("%d",&select); switch(select) { case1: if(CurrentJobCnt>=MAXJOBCNT) { printf("Thejobistoomany"); } else { printf("Enteryourjobname: "); } scanf("%s",JobName); printf("Enteryourjoblength: "); scanf("%d",&JobSize); ffallocation(); switch(JobAddress) { case-1: printf("thememoryisfull"); break; case0: coalesce(); ffallocation(); break; default: break; } break; case2: printf("Enterthenameofjob: "); scanf("%s",JobName); ffcollection(); break; case3: break; case4: exit (1); break; default: printf("Youinputawrongnumber! \n"); break; } } voidmain() { initiation(); while (1) { menu(); showyou(); } } ****************MENU*************** Youcanselectoneofthefollowing: (1)Requiretobeallocate. (2)Requiretocollectethesize. (3)Checkthememory. (4)Quit. *********************************** 1 Enteryourjobname: A Enteryourjoblength: 1000 namelength(b)addre A10002000 Thetotalleftis4000bytes ****************MENU*************** Youcanselectoneofthefollowing: (1)Requiretobeallocate. (2)Requiretocollectethesize. (3)Checkthememory. (4)Quit. *********************************** 1 Enteryourjobname: B Enteryourjoblength: 3000 namelength(b)addre B30003000 A10002000 Thetotalleftis1000bytes ****************MENU*************** Youcanselectoneofthefollowing: (1)Requiretobeallocate. (2)Requiretocollectethesize. (3)Checkthememory. (4)Quit. *********************************** 1 Enteryourjobname: C Enteryourjoblength: 500 namelength(b)addre C5006000 B30003000 A10002000 Thetotalleftis500bytes ****************MENU*************** Youcanselectoneofthefollowing: (1)Requiretobeallocate. (2)Requiretocollectethesize. (3)Checkthememory. (4)Quit. *********************************** 2 Enterthenameofjob: A namelength(b)addre C5006000 B30003000 Thetotalleftis1500bytes ****************MENU*************** Youcanselectoneofthefollowing: (1)Requiretobeallocate. (2)Requiretocollectethesize. (3)Checkthememory. (4)Quit. *********************************** 1 Enteryourjobname: D Enteryourjoblength: 1300 namelength(b)addre C5006500 B30003500 D13002000 Thetotalleftis200bytes ****************MENU*************** Youcanselectoneofthefollowing: (1)Requiretobeallocate. (2)Requiretocollectethesize. (3)Checkthememory. (4)Quit. *********************************** 实验总结: 分配3个内存A1000,B3000,C500地址分别为A2000、B3000、C6000(定义初始地址为2000,总内存5000)之后删除内存A此时剩余未分配空间为2000-30006500-7000,此时分配一个内存D大小1300,系统会将两块剩余内存压缩分配给D,BC地址改变,D初始地址为2000,B初始地址3500,C初始地址6500
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验四 存储器管理 实验 存储器 管理
![提示](https://static.bdocx.com/images/bang_tan.gif)