【操作系统】实验四 主存空间的分配和回收

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 512
struct partition {
    char    pn[10];//作业名
    int    begin;
    int    size;//大小
    int    end;
    char    status;//状态
};
struct partition part[MAX];
int p = 0; /*上次扫描结束处 */
void Init()
{
    int i;
    for ( i = 0; i < MAX; i++ )
        part[i].status = '-';
	
    strcpy( part[0].pn, "SYSTEM" );
    part[0].begin    = 0;
    part[0].size    = 100;
    part[0].status    = 'u';
	
    strcpy( part[1].pn, "-----" );
    part[1].begin    = 100;
    part[1].size    = 412;
    part[1].status    = 'f';
	
    for ( i = 0; i < MAX; i++ )
        part[i].end = part[i].begin + part[i].size;
}

void menu(){
	printf( "\n" );
	printf( "\n1、显示分区" );
	printf( "\n2、分配作业" );
	printf( "\n3、回收分区" );
	printf( "\n请输入选项:" );
}
void Output( int i ) /* 输出行 */
{
    printf( "\t%s", part[i].pn );
    printf( "\t%d", part[i].begin );
    printf( "\t%d", part[i].size );
    printf( "\t%d", part[i].end );
    printf( "\t%c", part[i].status );
}
void menu1(){
}

void ShowData() /* 显示分区 */
{
    int    i;
    int    n;
	printf( "\n================================================================" );
    printf( "\n空闲分区表Free:" );
    printf( "\n\tNo.\tproname\tbegin\tsize\tend\tstatus" );
    printf( "\n\t------------------------------------------------" );
    n = 1;
    for ( i = 0; i < MAX; i++ )
    {
        if ( part[i].status == '-' )
            break;
        if ( part[i].status == 'f' )
        {
            printf( "\n\tNo.%d", n );
            Output( i );
            n++;
        }
    }
    printf( "\n" );
    printf( "\n================================================================" );
    printf( "\n已分配分区表Used:" );
    printf( "\n\tNo.\tproname\tbegin\tsize\tend\tstatus" );
    printf( "\n\t------------------------------------------------" );
    n = 1;
    for ( i = 0; i < MAX; i++ )
    {
        if ( part[i].status == '-' )
            break;
        if ( part[i].status == 'u' )
        {
            printf( "\n\tNo.%d", n );
            Output( i );
            n++;
        }
    }
    printf( "\n" );
    printf( "\n" );
    printf( "\n================================================================" );
    printf( "\n内存使用情况,按起始址增长的排:" );
    printf( "\nprintf sorted by address:" );
    printf( "\n\tNo.\tproname\tbegin\tsize\tend\tstatus" );
    printf( "\n\t------------------------------------------------" );
    n = 1;
    for ( i = 0; i < MAX; i++ )
    {
        if ( part[i].status == '-' )
            break;
        printf( "\n\tNo.%d", n );
        Output( i );
        n++;
    }
    getch();
}


void Fittion( int a, char workName[], int workSize ) /* 分配成'u'+'f' */
{
    int i;
    for ( i = MAX; i > a + 1; i-- )
    {
        if ( part[i - 1].status == '-' )
            continue;
        strcpy( part[i].pn, part[i - 1].pn );
        part[i].begin    = part[i - 1].begin;
        part[i].size    = part[i - 1].size;
        part[i].end    = part[i - 1].end;
        part[i].status    = part[i - 1].status;
    }
    strcpy( part[a + 1].pn, "-----" );
    part[a + 1].begin    = part[a].begin + workSize;
    part[a + 1].size    = part[a].size - workSize;
    part[a + 1].end        = part[a].end;
    part[a + 1].status    = 'f';
    strcpy( part[a].pn, workName );
    part[a].size    = workSize;
    part[a].end    = part[a].begin + part[a].size;
    part[a].status    = 'u';
}
//先到先服务
void Thefirst(int i,int workSize,char workName[10]){
	for ( i = 0; i < MAX; i++ )
		if ( part[i].status == 'f' && part[i].size >= workSize )
			break;
        Fittion( i, workName, workSize );
}
//下次适应算法
void TheNextFittion(int p,int workSize,char workName[10]){
	for ( p; p < MAX; p++ )
	{
		if ( p == MAX )
			p = 0;
		if ( part[p].status == 'f' && part[p].size >= workSize )
			break;
	}
	Fittion( p, workName, workSize );
}
//最优适应算法
void TheBest(int i,int workSize,char workName[10],int pFree){
	for ( i = 0; i < MAX; i++ )
		if ( part[i].status == 'f' && part[i].size >= workSize )
			if ( part[pFree].size > part[i].size )
				pFree = i;
			Fittion( pFree, workName, workSize );
}

//最坏适应算法
void TheWrong(int i,int workSize,char workName[10],int pFree){
	for ( i = 0; i < MAX; i++ )
		if ( part[i].status == 'f' && part[i].size >= workSize )
			if ( part[pFree].size < part[i].size )
				pFree = i;
			Fittion( pFree, workName, workSize );
}
/* 分配 */
void Allocation() 
{
    int i;
    int a;
    int workSize;
    char workName[10];
    int pFree;
    printf( "\n请输入作业名称:" );
    while(1)
    {
        scanf( "%s", &workName );
        for(i=0;i<MAX;i++)
        {
            if(part[i].status=='u' && strcmp(workName,part[i].pn)==0)
            {
                printf("该作业已存在,请重新输入作业名:");
                break;
            }
        }
        if(i==MAX)
            break;
    }        
    printf( "请输入作业大小(k):" );
    while(1)
    {
        scanf( "%d", &workSize );    
        for ( i = 0; i < MAX; i++ )
        {
            if ( part[i].status == 'f' && part[i].size >= workSize )
            {
				pFree = i;
				break;
            }
        }
        if ( i == MAX )
        {
			printf( "\n该作业大小超出最大可分配空间,请重新输入:" );    
        }
        else break;
    }
    printf( "\n请选择分配算法:" );
    printf( "\n1、最先适应" );
    printf( "\n2、下次适应" );
    printf( "\n3、最优适应" );
    printf( "\n4、最坏适应" );
    printf( "\n请输入选项:" );
    while ( 1 )
    {
        scanf( "%d", &a );
        if ( a == 1 || a == 2 || a == 3 || a == 4 )
            break;
        else
			printf( "输入错误,请重新输入:" );
    }
    switch ( a )
    {
    case 1:
		Thefirst(i,workSize,workName);//先
        break;
    case 2:
		TheNextFittion(p,workSize,workName);//下
        break;
    case 3:
		TheBest(i,workSize,workName,pFree);//最优
        break;
    case 4:
		TheWrong(i,workSize,workName,pFree);//最坏
        break;
    default:
        break;
    }
    printf( "\n分配成功!" );
    getch();
}
void Merge() /* 合并连续的'f',回收 */
{
    int i = 0;
    while ( i != MAX - 1 )
    {
        for ( i = 0; i < MAX - 1; i++ )
        {
            if ( part[i].status == 'f' )
                if ( part[i + 1].status == 'f' )
                {
                    part[i].size    = part[i].size + part[i + 1].size;
                    part[i].end    = part[i].begin + part[i].size;
                    i++;
                    for ( i; i < MAX - 1; i++ )
                    {
                        if ( part[i + 1].status == '-' )
                        {
                            part[i].status = '-';
                            break;
                        }
                        strcpy( part[i].pn, part[i + 1].pn );
                        part[i].begin    = part[i + 1].begin;
                        part[i].size    = part[i + 1].size;
                        part[i].end    = part[i + 1].end;
                        part[i].status    = part[i + 1].status;
                    }
                    part[MAX - 1].status = '-';
                    break;
                }
        }
    }
}
void Recovery() /* 回收分区 */
{
    int    i;
    char  workName[10];
    printf( "\n请输入回收的分区名称:" );
    scanf( "%s", &workName );
    if ( strcmp( workName, "SYSTEM" ) == 0 )
    {
        printf( "\n该系统分区无法回收" );
        return;
    }
	
    for ( i = 0; i < MAX; i++ )
    {
        if ( strcmp( workName, part[i].pn ) == 0 )
        {
            strcpy( part[i].pn, "-----" );
            part[i].status = 'f';
            Merge();
            printf( "\n回收成功!" );
            getch();
            return;
			
        }
    }
    if ( i == MAX )
    {
        printf( "\n找不到该分区" );
        return;
    }
}


void main()
{
    int a;
    Init();
    printf( "\n初始化,设内存容量%dk", MAX );
    printf( "\n系统从低地址部分开始使用,占用%dk", part[0].size );
    printf( "\n" );
    while ( 1 )
    {
		menu();
        while ( 1 )
        {
            scanf( "%d", &a );
            if ( a == 1 || a == 2 || a == 3 )
                break;
            printf( "输入错误,请重新输入:" );
        }
        switch ( a )
        {
        case 1:
            ShowData();
            break;
        case 2:
            Allocation();
            break;
        case 3:
            Recovery();
            break;
        default:
            break;
        }
    }
}

  结果:

1.显示分区

2、作业分配

 

 

 3、回收

总结:

每一次实验都有着一定的难度,但是都需要去一步一步去解决。在解决问题在去完善自己,在操作中不断进步。

 

posted @ 2016-06-24 17:01  49陈焕恳  阅读(201)  评论(0编辑  收藏  举报