当前位置:文档之家› 可变分区存储管理方案中的内存分配

可变分区存储管理方案中的内存分配

可变分区存储管理方案中的内存分配用户提出内存空间的申请;系统根据申请者的要求,按照一定的分配策略分析内存空间的使用情况,找出能满足请求的空闲区,分给申请者;当程序执行完毕或主动归还内存资源时,系统要收回它所占用的内存空间或它归还的部分内存空间。

1.程序运行时首先接收输入:空闲区数据文件,包括若干行,每行有两个数据项:起始地址、长度(均为整数),各数据项以逗号隔开。

2.建立空闲区表并在屏幕上显示输出空闲区表内容,空闲区表中记录了内存中可供分配的空闲区的始址和长度,用标志位指出该分区是否是未分配的空闲区。

3.从用户界面根据用户提示接收一个内存申请,格式为:作业名、申请空间的大小。

4.按照最差(最坏)适配算法选择一个空闲区,分割并分配,修改相应的数据结构(空闲区表),填写内存已分配区表(起始地址、长度、标志位),其中标志位的一个作用是指出该区域分配给哪个作业。

5.重复3、4,直到输入为特殊字符(0)。

6.在屏幕上显示输出新的空闲区表和已分配区表的内容#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX 10struct data1 /*空闲区表*/{int address;int length;int flag;};struct data2 /*已分配区表*/{int address;int length;char name[20];};struct data1 empty[MAX];struct data2 busy[MAX];void initialize( );int read_data( ); /*从文件中读如数据*/void display_empty_table(int); /*显示空闲区表*/void display_busy_table(int); /*显示已分配区表*/void badest_fit( int *,int *,char *name,int s );/*最坏适应算法*/void first_fit( int *,int *,char *name,int s ); /*最先适应算法*/void best_fit( int *,int *,char *name,int s ); /*最佳适应算法*/void main( ){int num1,num2,size; /*num1用于统计空闲表的,num2用于统计分配区表*/ char name[20];num2=0;initialize( ); /*初始花空闲区表和分配区表*/num1=read_data( );if( num1==0 ) /*表示文件中没有数据*/printf("there has no data in empty table\n");printf("the initialial empty table is:\n");display_empty_table( num1 ); /*显示空闲区表*/while(1){printf("please input job's name and job's size\n");puts("input exit to exit");scanf("%s",name);if( strcmp(name,"exit")==0 ){getch( );break;}scanf("%d",&size);badest_fit( &num1,&num2,name,size );/*这里可以改为最佳适应和最先适应*/ }puts("the empty table after assigning");display_empty_table( num1 );puts("the busy table:");display_busy_table( num2 );}void initialize( ){int i;for( i=0;i<MAX;i++ ){empty[i].address=0;empty[i].length=0;empty[i].flag=0;busy[i].address=0;busy[i].length=0;strcpy(busy[i].name,"");}}int read_data( ){FILE *fp;int n=0;fp=fopen("A.txt","rb");if( fp==NULL ){puts("can't open A.txt");getch( );exit(1);}while( !feof(fp) ){fscanf(fp,"%d,%d",&empty[n].address,&empty[n].length);if( feof(fp) )break;n++;}fclose(fp);return n;}void display_empty_table( int num ){int i;printf("address\tlength\tflag\n");for( i=0;i<num;i++ )printf("%d\t%d\t%d\n",empty[i].address,empty[i].length,empty[i].flag); printf("\n");}void display_busy_table( int num ){int i;printf("address\tlength\tname\n");for( i=0;i<num;i++ )printf("%d\t%d\t%s\n",busy[i].address,busy[i].length,busy[i].name); printf("\n");}void badest_fit( int *n1,int *n2,char *name,int s ){int i,temp;temp=0;for( i=0;i<*n1;i++ ) /*寻找最大的空闲区*/if( empty[i].length>empty[temp].length)temp=i;if( s>empty[temp].length) /*申请的空间比最大的空闲区还大*/{printf("the size of memory is not enough\n");return;}busy[*n2].address=empty[temp].address;/*修改分配区表*/busy[*n2].length=s;strcpy( busy[*n2].name,name );(*n2)++;if( s==empty[temp].length ) /*若申请的空间与空闲区恰好相等*/ {for( i=temp+1;i<*n1;i++ )empty[i-1]=empty[i];(*n1)--;}else{empty[temp].address+=s;empty[temp].length-=s;}}/*最先适应算法*/void first_fit( int *n1,int *n2,char *name,int s ){int i,temp;temp=0;for( i=0;i<*n1;i++ ) /*寻找第一块空闲区*/if( empty[i].length>=s ){temp=i;break;}if( i>=*n1){printf("the size of memory is not enough\n");return;}busy[*n2].address=empty[temp].address;busy[*n2].length=s;strcpy( busy[*n2].name,name );(*n2)++;if( s==empty[temp].length ){for( i=temp+1;i<*n1;i++ )empty[i-1]=empty[i];(*n1)--;}else{empty[temp].address+=s;empty[temp].length-=s;}}/*最佳适应算法*/void best_fit( int *n1,int *n2,char *name,int s ){int i,temp;temp=0;for( i=0;i<*n1;i++ ) /*寻找最佳的空闲区*/if( empty[i].length>=s ){temp=i;break;}if( i>=*n1){printf("the size of memory is not enough\n");return;}for(i=temp+1;i<*n1;i++ ){if(empty[i].length>s&&empty[i].length<empty[i].length ) {temp=i;}}busy[*n2].address=empty[temp].address;busy[*n2].length=s;strcpy( busy[*n2].name,name );(*n2)++;if( s==empty[temp].length ){for( i=temp+1;i<*n1;i++ )empty[i-1]=empty[i];(*n1)--;}else{empty[temp].address+=s;empty[temp].length-=s; }}测试文件0,200200, 600800, 8001700, 1001800, 500。

相关主题