实验四 主存空间的分配和回收模拟

1. 实验目的

为了合理地分配和使用这些存储空间,当用户提出申请主存储器空间时,存储管理必须根据申请者的要求,按一定的策略分析主存空间和使用情况,找出足够的空闲区域给申请者。当作业撤离归还主存资源时,则存储管理要收回占用的主存空间。主存的分配和回收的实现是与主存储器的管理方式有关的,通过本实验帮助我们理解在不同的存储管理方式下应怎样实现主存空间的分配和回收。

用高级语言完成一个主存空间的分配和回收模拟程序,以加深对内存分配方式及其算法的理解。

 

2. 实验内容

2.1 模拟包括3部分:

1) 实现特定的内存分配算法

2) 实现内存回收模拟

3) 每种内存分配策略对应的碎片数统计

 

2.2 固定分区存储管理

假设内存容量为120KB,并且分别划分成23,24,25,26KB大小的块各一块。

一个进程所需要的内存为0100KB。同时假设一个进程在运行过程中所需内存的大小不变。

模拟五个进程到达请求分配与运行完回收情况,输出主存分配表.

 

 

2.3 动态分区分配存储管理

采用连续分配方式之动态分区分配存储管理,使用首次适应算法、下次适应算法、最佳适应算法和最坏适应算法4种算法完成设计(任选两种算法)。

(1)在程序运行过程,由用户指定申请与释放。

(2)设计一个已占用分区表,以保存某时刻主存空间占用情况。

(3)设计一个空闲分区表,以保存某时刻主存空间剩余情况。

(4)用两个表的变化情况,反应各进程所需内存的申请与释放情况。

 

3. 实验环境

根据指定的实验课题,完成设计、编码和调试工作,完成实验报告。

可以选用Turbo C作为开发环境。也可以选用Windows下的VBCB或其他可视化环境,利用各种控件较为方便。自主选择实验环境。

 

 

4. 实验环境

 

    1.源程序名: allocate.c

 

        可执行程序名:allocate.exe

 

    2.原理分析及流程图

 

       输入全部进程,程序自动遵循简单轮转法的要求开始运行,最后输出结果。

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
void sd();

typedef struct T{
    int capacity;
    int signal;
    int fragment;
    int time;
    int occu;
    char fillername[10];
}tony;     //static stuff

typedef struct O{
    int capacity;
    int signal;
    int time;
    char name[10];
    struct O *next;
}nina;    //chain~~

typedef struct H{
    char name[10];
    int occupation;
    int time;
    int get;
}hao;
int total=120;
void sdistribution();

void dpartitionfirst();

void dpartitionoptimum();

main()
{
    int swi;
    int uu;

    printf("enter 0(static distribution) or 1(dynamic partition) :   ");
    scanf("%d",&swi);
    switch(swi)
    {
    case 0:
        sdistribution();
        break;
    case 1:
        printf("1(首次适应) 2(最佳适应)  :   ");
        scanf("%d",&uu);
        switch(uu)
        {
        case 1:
        dpartitionfirst();
        break;
        case 2:
        dpartitionoptimum();
        break;
        }
        break;
    default :
        puts("
fucking wrong

");
        break;
    }    
}
void dpartitionfirst()
{
    nina *chan,*head,*uu,*temp,*te1;
    int a,k,j=0,f;
    hao hao;
    head=(nina *)malloc(sizeof(nina));
    
    printf(" now, we got 120KBstorage
");
    chan=(nina *)malloc(sizeof(nina));
    chan->capacity=total;                    ////
    chan->signal=0;
    uu=(nina *)malloc(sizeof(nina));
    te1=(nina *)malloc(sizeof(nina));
    chan->next=NULL;
    temp=(nina *)malloc(sizeof(nina));
    temp=chan;
    te1=chan;
    head->next=temp;
    do{
        printf("spared table :
 capacity 
");
        while(temp!=NULL)
        {
        if(temp->signal==0)
        {
            printf(" %d
",temp->capacity);
        }
        temp=temp->next;
        }
    temp=head->next; //has head->next's essence
    printf("occupyed table :
 capacity       time	name
");
    while(temp!=NULL)
    {
        if(temp->signal==1)
        {
            printf(" %d	%d	%s
",temp->capacity,temp->time,temp->name);
        }
            temp=temp->next;
    }
        temp=head->next;
    printf("

wanna enter a shit? 1:yeap 2:nope
");
    scanf("%d",&a);
    if(a==1)
    {    
        printf("

please enter a fucking process
space,time,name
");
        scanf("%d",&hao.occupation);
        if(hao.occupation<=total)
        {
            total=total-hao.occupation;
            chan->capacity=total;
        scanf("%d",&hao.time);
        getchar();
        scanf("%s",&hao.name);
        uu->capacity=hao.occupation;
        strcpy(uu->name,hao.name);
        uu->signal=1;
        uu->time=hao.time;
        head->next=uu;           ////
        uu->next=te1;            ///       huge problem!!!!!      
        te1=uu;//
        }
        else
            printf("outweight our storage,so that stuff cannot enter storage

");
    }
    }while(a==1);
}

void dpartitionoptimum()  ////hhhh
{
    int all=120;
    nina *chan,*head,*uu,*temp,*te1;
    int a,k,j=0,f;
    hao hao;
    head=(nina *)malloc(sizeof(nina));
    
    printf(" now, we got 120KBstorage
");
    chan=(nina *)malloc(sizeof(nina));
    chan->capacity=total;                    ////
    chan->signal=0;
    uu=(nina *)malloc(sizeof(nina));
    te1=(nina *)malloc(sizeof(nina));
    chan->next=NULL;
    temp=(nina *)malloc(sizeof(nina));
    temp=chan;
    te1=chan;
    head->next=temp;
    do{
        printf("spared table :
 capacity 
");
        while(temp!=NULL)
        {
        if(temp->signal==0)
        {
            printf(" %d
",temp->capacity);
        }
        temp=temp->next;
        }
    temp=head->next; //has head->next's essence
    printf("occupyed table :
 capacity       time	name
");
    while(temp!=NULL)
    {
        {
            temp->time--;
            if(temp->time!=0)
            {
            printf(" %d	%d	%s
",temp->capacity,temp->time,temp->name);
            all=all-temp->capacity;
            }
            else
                temp->signal=0;
        }
            temp=temp->next;
    }
        temp=head->next;
    printf("

wanna enter a shit? 1:yeap 2:nope
");
    scanf("%d",&a);
    if(a==1)
    {    
        printf("

please enter a fucking process
space,time,name
");
        scanf("%d",&hao.occupation);
        if(hao.occupation<=all)
        {
            total=total-hao.occupation;
            chan->capacity=total;
        scanf("%d",&hao.time);
        getchar();
        scanf("%s",&hao.name);
        uu->capacity=hao.occupation;
        strcpy(uu->name,hao.name);
        uu->signal=1;
        uu->time=hao.time;
        head->next=uu;           ////
        uu->next=te1;            ///       huge problem!!!!!      
        te1=uu;                  //
        }
        else
            printf("outweight our storage,so that stuff cannot enter storage

");
    }
    }while(a==1);

}

void sdistribution()   //completion
{
    tony chan[4];
    int a,k,j=0,f;
    hao hao[5];
    printf(" now, we got 120KBstorage
 we will distribute them into 8,16,32,64kb at the moment
");
    printf("
******now,the table before inputting any process is below*****
         partition      occupyed
");
    for(a=0;a<4;a++)
    {
        chan[a].capacity=8*pow(2,a);
        chan[a].signal=0;
        chan[a].occu=0;
        chan[a].time=0;
    }
    for(a=0;a<4;a++)
    {
        printf("         %d             %d 
",chan[a].capacity,chan[a].signal);
    }
    printf("

 now,please input five processes(order is crucial) into this storage
");
    printf("occupation ,time and name,OvO
");
    for(a=0;a<5;a++)
    {
        hao[a].get=65536;
        scanf("%d",&hao[a].occupation);
        scanf("%d",&hao[a].time);
        getchar();
        scanf("%s",&hao[a].name);
    }
    printf("existed processes show below
");

    printf("occupation	time	name
");
    for(a=0;a<5;a++)
    {
        printf("%d            %d            %s
",hao[a].occupation,hao[a].time,hao[a].name);
    }
    printf("*********processing*********");
    for(a=0;a<5;a++)
    {
            for(k=0;k<4;k++)
            {            
                       if(chan[k].signal==1)
                            {
                                chan[k].time--;
                            if(chan[k].time==0)
                            {
                                chan[k].signal=0;
                            }
                            }
                        if((hao[a].get>chan[k].capacity)&&(hao[a].occupation<=chan[k].capacity)&&(chan[k].signal==0))
                        {
                        strcpy(chan[k].fillername,hao[a].name);
                           chan[k].signal=1;
                        chan[k].fragment=chan[k].capacity-hao[a].occupation;
                        chan[k].occu=hao[a].occupation;
                        chan[k].time=hao[a].time;
                        hao[a].get=chan[k].capacity;
                        }

            }

            printf("**********
大小  进程名字  碎片  占有信号  占有大小  运行时间
");
                for(k=0;k<4;k++)
                {
                    if(chan[k].signal==1)
                    printf("%d	%s	%d	%d	%d		%d
",chan[k].capacity,chan[k].fillername,chan[k].fragment,chan[k].signal,chan[k].occu,chan[k].time);
                    if(chan[k].signal==0)
                        printf("%d	%d	
",chan[k].capacity,chan[k].signal);
                }    
                printf("press 'enter'button to next step
");
                getchar();
    }
}

 

 5实验总结

   这次实验整体难度不大,和之前几次实验不同,这次我完全没有参考网上的例子,完全通过自己完成,可能是我这次掌握的好的原因。固定分区大体没什么问题但是可变分区我个人认为需要用到链表,而链表一向是我的难点,因此有点难度。

  不过通过这次 实验我对操作系统有了更深的理解,很不错的实验。

原文地址:https://www.cnblogs.com/TonyWinner/p/5089043.html