实验目的
模拟存储管理中内存空间的管理和分配
存储管理的主要功能之一是合理地分配空间。内存空间的管理分为固定分区管理方式,可变分区管理方式,页式存储管理,段式存储管理。可以对这几种管理方式进行分配和回收的实验。
一、实验内容
以分区管理为例,程序功能为
1、完成分配内存的工作;
2、完成回收内存的工作;
3、显示主存的情况(包括已分配区和空闲区)。二、程序流程图
主程序流程图:
主要函数模块流程图:
三、代码实现:
#include<stdio.h>
#include<stdlib.h> /*因为下面用到内存动态分配函数*/
#define N 10
typedef struct memory
{
int order;
int volume;
int start_address;
int alloc;
int free;
int state;
}MEM;
MEM * Create_memory( );
MEM * Alloction_memory( );
MEM * Recover_memory( );
void Output_memory( );
main( )
{
char temp;
int flag=0; /*用来标志最后一次分配的进程的记号,此变量存储该进程的序号order.*/
MEM *M=NULL;
M=(MEM *)calloc(N,sizeof(MEM));
M=Create_memory(M);
Output_memory(M);
printf("Please input one char:'a'(Alloction_memory) or 'r'(Recover_memory)\nWhen you input 'z',then this program is over!\n");
//temp=getchar();
//getchar();
while(1)
{
temp=getchar();
//getchar();
if(temp=='z')
{
break;
}
if(temp=='a'||temp=='A')
{
M=Alloction_memory(M,&flag);
}
if(temp=='r'||temp=='R')
{
M=Recover_memory(M,&flag);
//Output_memory(M);
}
printf("Please input one char:'a'(Alloction_memory) or 'r'(Recover_memory)\nWhen you input 'z',then this program is over!\n");
//temp=getchar();
//getchar();/*接受回车符!*/
}
/*Alloction_memory(M);
Recover_memory(M);
printf("\n");
Output_memory(M);*/
}
MEM * Create_memory(MEM *Create_M)
{
int i;
for(i=0;i<N;i++)
{
Create_M[i].order=i+1;
Create_M[i].volume=50+rand()%50;
if(i==0)
{
Create_M[0].start_address=0;
}
else
{
Create_M[i].start_address=Create_M[i-1].start_address+Create_M[i-1].volume;/*首地址从0开始*/
}
Create_M[i].alloc=0;
Create_M[i].free=Create_M[i].volume;
Create_M[i].state=0;
}
return Create_M;
}
MEM * Alloction_memory(MEM *Alloction_M,int *Alloction_flag)
{
int i,/*k=0,*/ask_volume;
printf("This is Alloction_memory function!\nPlease input you will ask for the memory of volume!\n");
printf("Please input a integer,not other word!\nIf input is '-1',then Alloction_memory over!\n");
while(1)
{
scanf("%d",&ask_volume);
if(ask_volume==-1)
{
getchar();/*输入的若是-1,用来接收回车,防止影响main()中的temp=getchar()语句。*/
break;
}
for(i=0;i<N;i++)
{
if(ask_volume<=Alloction_M[i].free)
{
Alloction_M[i].alloc=ask_volume;
Alloction_M[i].free=Alloction_M[i].free-Alloction_M[i].alloc;
Alloction_M[i].start_address=Alloction_M[i].start_address+ask_volume;
Alloction_M[i].state=1;
*Alloction_flag=Alloction_M[i].order;
//k=1;
//break;
Output_memory(Alloction_M);
Alloction_M[i].state=0;
printf("You can alloc memory again!\nPlease input a integer,not other word!\nIf input is '-1',then Alloction_memory over!\n");
break;
}
}
/*if(k==1)
{
break;
}*/
if(i==N)
{
printf("\nThis alloc is fail!\nYou ask for the memory of volume,is above the max_memory!\nPlesase input the right volume again:\n");
}
}
return Alloction_M;
}
MEM * Recover_memory(MEM *Recover_M,int *Recover_flag)
{
int i;
getchar(); //接受回车符。
for(i=0;i<N;i++)
{
if(/*(Recover_M[i].state==1)&&(Recover_M[i].free!=Recover_M[i].volume)*/*Recover_flag==Recover_M[i].order)
{
Recover_M[i].state=0;
}
else
{
if((Recover_M[i].state==0)&&(Recover_M[i].free!=Recover_M[i].volume))
{
if(Recover_M[i].order==1)
{
Recover_M[i].start_address=0;
}
else
{
Recover_M[i].start_address=Recover_M[i-1].start_address+Recover_M[i-1].volume;
}
Recover_M[i].alloc=0;
Recover_M[i].free=Recover_M[i].volume;
Recover_M[i].state=1;
}
}
}
Output_memory(Recover_M);
for(i=0;i<N;i++)
{
/*if(Recover_M[i].free!=Recover_M[i].volume)
{
Recover_M[i].state=1;
}
else
{*/
Recover_M[i].state=0;
//}
}
//free(Recover_M);
return Recover_M;
}
void Output_memory(MEM *Output_M)
{
int i;
printf("Order\tVolume\tstart_address\talloc\tfree\tstate\n");
for(i=0;i<N;i++)
{
printf("%d\t%dkb\t%dkb\t\t%dkb\t%dkb\t%d\n",Output_M[i].order,Output_M[i].volume,Output_M[i].start_address,Output_M[i].alloc,Output_M[i].free,Output_M[i].state);
}
}
本文是原创,转载请注明出处!