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

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 24
void firstfit(int num);
void bestfit(int num);
void badfit(int num);
struct partition{
    
    char pn[10];
    int begin;
    int size;
    int end;   
    char status;  
    };
typedef struct partition PART;

PART Total[MAX] ;
PART userjob ;
PART backups[MAX] ;

void init(){

strcpy(Total[0].pn,"No.1");
Total[0].begin=0;
Total[0].size=100;
Total[0].end=100;
Total[0].status='u';
}


void input(int num){

    int i=0;
    for(i=0;i<num;i++){
    //作业名
    printf("作业名:\n");
    scanf("%s" ,&Total[i+1].pn);
    //开始内存大小
    printf("内存大小:\n");
    scanf("%d",&Total[i+1].size);
    //作业状态
    printf("作业状态:\n");
    scanf("%s",&Total[i+1].status);
    //作业开始大小
    Total[i+1].begin=Total[i].end;
    printf("开始地址:%d\n",Total[i+1].begin);
    //作业结束大小
    Total[i+1].end=Total[i+1].begin+Total[i+1].size;
    printf("结束地址:%d\n",Total[i+1].end);

    }

}


void output(int num){
int i=0;
printf("内存名\t开始\t大小\t结束\t状态\n");    
    for(i=0;i<num+1;i++)
    {
        
        printf("%s \t %d \t %d \t %d \t %c \n" ,Total[i].pn,Total[i].begin,Total[i].size,Total[i].end,Total[i].status);
    
    }


}

void inputjob(){
printf("作业名:\n");
    scanf("%s" ,&userjob.pn);
    //开始内存大小
    printf("内存大小:\n");
    scanf("%d",&userjob.size);
    //作业状态
    userjob.status='u';
    //作业开始大小
    userjob.begin=0;
    //作业结束大小
    userjob.end=0;


}



void selecttypr(int select,int num){

    switch(select){
    case 1 : 
        printf("1首次适应算法\n");
        firstfit(num);
        output(num+1);
        break;
    case 2 : 
        printf("2循环首次适应算法");

        break;
    case 3 : 
        printf("3最佳适应算法\n");
         bestfit(num);
         output(num+1);
        break;
    case 4 : 
        printf("4最坏适应算法");
        badfit(num);
        output(num+1);
        break;
    default :
        printf("输入错误");
        break;

    }

}
void firstfit(int num){
   int i,sum;
   for(i=0;i<=num;i++){
       sum=Total[i+1].size;
       if(userjob.size>=sum && Total[i+1].status!='u')
       {
          Total[num+1].size=userjob.size;
          Total[num+1].begin=Total[i].end;
          Total[num+1].end=Total[i+1].begin;
          strcpy(Total[num+1].pn,userjob.pn);
          Total[num+1].status=userjob.status ;
          break;
       }
   } 
}

void bestfit(int num){ 
     int i,s,sum;
     for(i=0;i<=num;i++){
         
         if(userjob.size>=Total[i+1].size && Total[i+1].status!='u') 
         {
            s=i;
            sum=Total[i+1].size;
            for(i=i+1;i<sum;i++){
                if(Total[i].size>userjob.size&&Total[i].size<sum&&Total[i+1].status!='u'){
                    sum=Total[i].size;
                      s=i;
                }
         }
         }
         
     }
          Total[num+1].size=userjob.size;
          Total[num+1].begin=Total[s].end;
          Total[num+1].end=Total[s].begin;
          strcpy(Total[num+1].pn,userjob.pn);
          Total[num+1].status=userjob.status ;
     
}
void badfit(int num){
     int i,s,sum;
     for(i=0;i<=num;i++){
         
         if(userjob.size>=Total[i+1].size && Total[i+1].status!='u') 
         {
            s=i;
            sum=Total[i+1].size;
            for(i=i+1;i<sum;i++){
                if(Total[i].size>userjob.size&&Total[i].size>sum&&Total[i+1].status!='u'){
                    sum=Total[i].size;
                      s=i;
                }
         }
         }
         
     }
          Total[num+1].size=userjob.size;
          Total[num+1].begin=Total[s].end;
          Total[num+1].end=Total[s].begin;
          strcpy(Total[num+1].pn,userjob.pn);
          Total[num+1].status=userjob.status ;
}

void cz(int num){
     int i,n=0;
     char pn[10];
    printf("输入:");
    scanf("%s",&pn);
    for(i=0;i<=num;i++){
        if(strcmp(pn,Total[i+1].pn)==0){
            n=1;
            break;
        }
        else n=0;
    }
    if(n==0)
        printf("作业不存在。\n");
    else  
        printf("已终止。\n");
}
void main(){
int num;int select;
printf("初始化,设内存的总量为512k\n");
printf("系统从低地址开始分配,占用100k\n");
init();

printf("=================================");
printf("\n\n\n");
printf("作业的个数 \n");
scanf("%d",&num);
input(num);

printf("=================================");
printf("\n\n\n");
output(num);
//cz(num);
printf("=================================");
printf("\n\n\n");
printf("用户输入作业\n");
inputjob();

printf("=================================");
printf("\n\n\n");
printf("用户选择分配算法\n");
printf("\t\t\t 1首次适应算法\n");
printf("\t\t\t 2循环首次适应算法\n");
printf("\t\t\t 3最佳适应算法\n");
printf("\t\t\t 4最坏适应算法\n");
scanf("%d",&select);
selecttypr(select,num);

}

posted @ 2016-06-17 17:31  Newul  阅读(163)  评论(0编辑  收藏  举报