可变分区存储管理方案模拟
{
for( i=temp+1;i<*n1;i++ )
empty[i-1]=empty[i];
(*n1)--;
}
else //如果空闲分区仍有盈余
{
empty[temp].address+=s;
empty[temp].length-=s;
//如果没找到,显示提示信息
/*寻找第一块空闲区*/
if( empty[i].length>=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");
其它两种算法*/
}
puts("-the empty table after assigning:");
display_empty_table( num1 ); //显示空闲分区信息
puts("-the busy table:");
display_busy_table( num2 ); //显示已分配分区信息
empty[i-1]=empty[i];
(*n1)--;
}
else
{
empty[temp].address+=s;
empty[temp].length-=s;
}
puts("-the empty table after assigning:");
display_empty_table( *n1 ); //显示空闲分区信息
getchar( );
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 )
{
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++ )
puts("input exit to exit");
scanf("%s",name);
if( strcmp(name,"exit")==0 )
{
getchar( );
break;
}
scanf("%d",&size);
best_fit( &num1,&num2,name,size );/*每输入一个进程便为其分配内存。这里可以改为
return;
}
//当找到时,准备分配该空闲分区给进程,修改相应数据
busy[*n2].address=empty[temp].address;
busy[*n2].length=s;
strcpy( busy[*n2].name,name );
(*n2)++;
if( s==empty[temp].length ) //如果空闲分区大小与进程大小一致
{
/*lu显示empty中空闲分区的信息,参数为空闲分区的个数*/
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);
empty[i-1]=empty[i];
(*n1)--;
}
else{
empty[temp].address+=s;
empty[temp].length-=s;
}
}
}
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,"");
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++ )
}
}
int read_data( )
{
/*
从a.txt中读出内存空闲分区状态
其中的每一行,分别代表了一个空闲分区的始址和大小
返回空闲分区的个数
*/
FILE *fp;
int n=0;
fp=fopen("a.txt","rb");
if( fp==NULL )
{
puts("can't open a.txt");
puts("-the busy table:");
display_busy_table( *n2 ); //显示已分配分区信息
}
//最坏适应算法,不要求实现
void badest_fit( int *n1,int *n2,char *name,int s )
{
printf("\n---- Badest Fit Arithmetic Allocation ----\n");
printf("\n");
}
/*最先适应算法*/
void first_fit( int *n1,int *n2,char *name,int s )
{
/*
传入参数:n1为空闲分区的个数;n2为已分配分区的个数;name为新进程的名字;s为新
进程的长度
n1,n2采用地址方式传入,可直接在子函数中修改其值
void best_fit( int *n1,int *n2,char *name,int s )
{
printf("Best Fit Arithmetic Allocation\n");
int i,temp;
temp=0;
for( i=0;i<*n1;i++ )/*寻找最佳空闲分区*/
if(empty[i].length>=s)
printf("the initialial empty table is:\n");
display_empty_table( num1 ); /*显示空闲区表*/
while(1)
{
printf("\n---------------------------------------");
printf("\nplease input job's name and job's size\n");
char name[20];
num2=0;
initialize( );
if( num1==0 )
/*初始花空闲区表和分配区表*/
/*表示文件中没有数据*/
num1=read_data( ); /*将空闲分区信息读入empty数组,并返回空闲分区个数*/
printf("there has no data in empty table\n");
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);
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用于统计分配区表*/