实验四存储器管理系统.doc
实验四 存储器管理1、目的与要求 本实验的目的是让学生熟悉存储器管理的方法,加深对所学各种存储器管理方案的了解;要求采用一些常用的存储器分配算法,设计一个存储器管理模拟系统,模拟内存空间的分配和释放。2、实验内容设计一个存放空闲块的自由链和一个内存作业分配表,存放内存中已经存在的作业。编制一个按照首次适应法分配内存的算法,进展内存分配。同时设计内存的回收以与内存清理如果要分配的作业块大于任何一个空闲块,但小于总的空闲分区,如此需要进展内存的清理,空出大块的空闲分区的算法。3实验环境PC兼容机Windows、DOS系统、Turbo c 2.0C语言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*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 4000 bytes6实验程序#include <stdio.h>#include <dos.h>#include <stdlib.h>#include <conio.h>#include <io.h>#include "string.h"#define TOTALSPACE 5000 /*起始空闲空间大小#define INITADDRESS 2000 /*起始地址#define MINSPACE 100#define MAXJOBT 10 /* 内存中作业数量最大值struct JobMat char name10; int address; int length; struct JobMat * next; struct JobMat * back;struct FreeArea int address; int size; struct FreeArea * next; struct FreeArea * back;struct JobMat *p_JobMat;struct FreeArea *p_FreeArea;int UnusedSpace; /*未分配的空闲空间大小int CurrentJobt; /*目前内存中的作业数量int JobSize; /*目前要分配的作业的大小char JobName10;int JobAddress; void initiation() p_FreeArea=(struct FreeArea *)malloc(sizeof(struct FreeArea); p_FreeArea->size=TOTALSPACE; p_FreeArea->address=INITADDRESS; p_FreeArea->next=NULL; p_FreeArea->back=NULL; UnusedSpace=TOTALSPACE; p_JobMat=NULL; CurrentJobt=0; JobSize=0; memset(JobName, 0, 10); JobAddress=0;void ffallocation()struct FreeArea * fp;struct JobMat *jp;struct JobMat *jp1;struct JobMat *jp2;JobAddress=-1;if (UnusedSpace<JobSize)/*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;elseCurrentJobt=CurrentJobt+1;UnusedSpace=UnusedSpace-JobSize;jp2=(struct JobMat *)malloc(sizeof(struct JobMat);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;elsejp=p_JobMat;while(jp!=NULL)&&(jp2->address<jp->address)jp1=jp;jp=jp->next;jp2->next=jp;if (jp=NULL)jp2->back=jp1;jp1->next=jp2; elsejp2->back=jp->back;if (jp->back!=NULL) jp1->next=jp2; else p_JobMat=jp2;jp->back=jp2; if (fp->size-JobSize<MINSPACE)if (fp->next!=NULL) fp->next->back=fp->back;if (fp->back!=NULL) fp->back->next=fp->next;else p_FreeArea=fp->next;elsefp->size=fp->size-JobSize;fp->address=fp->address+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(jp->name, JobName) != 0)jp=jp->next;if (jp!=NULL)CurrentJobt=CurrentJobt-1;UnusedSpace=UnusedSpace+jp->length;if (p_FreeArea=NULL) p_FreeArea=(struct FreeArea *)malloc(sizeof(struct FreeArea); p_FreeArea->address=jp->address; p_FreeArea->size=jp->length; p_FreeArea->next=NULL; p_FreeArea->back=NULL;elsefp=p_FreeArea;while(fp!=NULL)&&(fp->address<jp->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;else if (jp->address) = (fp1->address+(fp1->size) f=f+2;switch (f)case 0:fp2=(struct FreeArea *)malloc(sizeof(struct FreeArea);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;else p_FreeArea=fp2;fp->back=fp2;elsefp2->back=fp1;fp1->next=fp2;break;case 1:fp->size=fp->size+jp->length;fp->address=jp->address;break;case 2:fp1->size=fp1->size+jp->length;break;case 3: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);void showyou()struct JobMat *jp;/*clrscr();*/if (CurrentJobt<=0)printf("No job.n");elseprintf("name length(b) addren");jp=p_JobMat;while (jp!=NULL)printf("%s %d %dn", jp->name, jp->length,jp->address);jp=jp->next;printf("The total left is %d bytesn",UnusedSpace);void coalesce()struct FreeArea *fp;struct FreeArea *fp1;struct JobMat *jp;int bottom;if (CurrentJobt>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=(struct FreeArea *)malloc(sizeof(struct FreeArea);p_FreeArea->size=UnusedSpace;p_FreeArea->address=INITADDRESS;p_FreeArea->next=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.n");printf("*n");scanf("%d",&select);switch(select)case 1:if( CurrentJobt>=MAXJOBT)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.实验结果