• 姓名 陈悦凯
  • 学号 201821121011
  • 班级 计算1811

1. 记录内存空间使用情况

//显示当前内存的使用情况,包括空闲区的情况和已经分配的情况
int display_mem_usage()
{
    struct free_block_type *fbt= free_block;
    struct allocated_block *ab= allocated_block_head;
    printf("-------------------------------------------------------------\n");
    printf("Free Memory:\n");
    printf("%20s %20s\n","     start_addr","     size");
    while(fbt!= NULL)
    {
        printf("%20d %20d\n",fbt->start_addr,fbt->size);
        fbt= fbt->next;
    }
    printf("\nUsed Memory:\n");
    printf("%10s %20s %10s %10s\n","PID","ProcessName","start_addr","size");
    while(ab!= NULL)
    {
        printf("%10d %20s %10d %10d\n",ab->pid,ab->process_name,ab->start_addr,ab->size);
        ab= ab->next;
    }
    printf("-------------------------------------------------------------\n");
    return 1;
}

  

//创建一个新进程
int
new_process(int x) { struct allocated_block *ab; int size; int ret; ab= (struct allocated_block*)malloc(sizeof(struct allocated_block)); if(!ab) exit(-5); ab->next= NULL; pid++; sprintf(ab->process_name,"PROCESS-%02d",pid);//将格式化的数据写入某字符串中 ab->pid= pid; ab->size=x; ret= allocate_mem(ab); if((ret== 1)&&(allocated_block_head== NULL)) //如果此时未赋值,则赋值 { allocated_block_head= ab; return 1; } else if(ret== 1) //分配成功,将该已分配块的描述插入已分配链表 { ab->next= allocated_block_head; allocated_block_head= ab; return 2; } else if(ret== -1) //分配不成功 { printf("Allocation fail.\n"); free(ab); return -1; } return 3; }

 

2. 记录空闲分区

//描述每一个空闲块的数据结构
struct free_block_type
{
    int size;        //空闲块大小
    int start_addr;  //空闲块起始位置
    struct free_block_type *next;  //指向下一个空闲块
};
//指向内存中空闲块链表的首地址
struct free_block_type *free_block= NULL;
//按FF算法重新整理内存空闲块链表,按空闲块首地址排序
int rearrange_FF()
{
    struct free_block_type *head= free_block;
    struct free_block_type *forehand,*pre,*rear;
    int i;
    if(head== NULL)
        return -1;
    for(i= 0;i< free_block_count-1;i++)
    {
        forehand= head;
        pre= forehand->next;
        rear= pre->next;
        while(pre->next!= NULL)
        {
            if(forehand== head&&forehand->start_addr>= pre->start_addr)
            {
                //比较空闲链表中第一个空闲块与第二个空闲块的开始地址的大小
                head->next= pre->next;
                pre->next= head;
                head= pre;
                forehand= head->next;
                pre= forehand->next;
                rear= pre->next;
            }
            else if(pre->start_addr>= rear->start_addr)
            {
                //比较链表中其它相邻两个结点的开始地址的大小
                pre->next= rear->next;
                forehand->next= rear;
                rear->next= pre;
                forehand= rear;
                rear= pre->next;
            }
            else
            {
                forehand= pre;
                pre= rear;
                rear= rear->next;
            }
        }
    }
    return 0;
}

3. 内存分配算法

最坏适应算法:要扫描整个空闲分区或链表,总是挑选一个最大的空闲分区分割给作业使用。

最佳适应算法:从全部空闲区中找出能满足作业要求且大小最小的空闲分区的一种计算方法,这种方法能使碎片尽量小。

首次适应算法:从空闲分区表的第一个表目起查找该表,把最先能够满足要求的空闲区分配给作业,这种方法目的在于减少查找时间。为适应这种算法,空闲分区表(空闲区链)中的空闲分区要按地址由低到高进行排序。该算法优先使用低址部分空闲区,在低址空间造成许多小的空闲区,在高地址空间保留大的空闲区。

//按照最坏适应算法给新进程分配内存空间
int allocate_WF(struct allocated_block *ab)
{
    int ret;
    struct free_block_type *wf= free_block;
    if(wf== NULL)
        return -1;
    if(wf->size>= ab->size)
        allocate(NULL,wf,ab);
    else if(current_free_mem_size>= ab->size)
        ret= mem_retrench(ab);
    else
        ret= -2;
    rearrange_WF();
    return ret;
}

//按照最佳适应算法给新进程分配内存空间
int allocate_BF(struct allocated_block *ab)
{
    int ret;
    struct free_block_type *pre= NULL,*bf= free_block;
    if(bf== NULL)
        return -1;
    while(bf!= NULL)
    {
        if(bf->size>= ab->size)
        {
            ret= allocate(pre,bf,ab);
            break;
        }
        pre= bf;
        pre= pre->next;
    }
    if(bf== NULL&&current_free_mem_size> ab->size)
        ret= mem_retrench(ab);
    else
        ret= -2;
    rearrange_BF();
    return ret;
}

//按照首次适应算法给新进程分配内存空间
int allocate_FF(struct allocated_block *ab)
{
    int ret;
    struct free_block_type *pre= NULL,*ff= free_block;
    if(ff== NULL)
        return -1;
    while(ff!= NULL)
    {
        if(ff->size>= ab->size)
        {
            ret= allocate(pre,ff,ab);
            break;
        }
        pre= ff;
        pre= pre->next;
    }
    if(ff== NULL&&current_free_mem_size> ab->size)
        ret= mem_retrench(ab);
    else
        ret= -2;
    rearrange_FF();
    return ret;
}

 

4. 内存释放算法

//释放ab数据结构结点
int dispose(struct allocated_block *free_ab)
{
    struct allocated_block *pre,*ab;
    if(free_block== NULL)
        return -1;
    if(free_ab== allocated_block_head)   //如果要释放第一个结点
    {
        allocated_block_head= allocated_block_head->next;
        free(free_ab);
    }
    else
    {
        pre= allocated_block_head;
        ab= allocated_block_head->next; 
        //找到free_ab
        while(ab!= free_ab)
        {
            pre= ab;
            ab= ab->next;
        }
        pre->next= ab->next;
        free(ab);
    }
    return 1;
}

//将ab所表示的已分配区归还,并进行可能的合并
int free_mem(struct allocated_block *ab)
{
    int algorithm= ma_algorithm;
    struct free_block_type *fbt,*pre,*work;
    fbt= (struct free_block_type*)malloc(sizeof(struct free_block_type));
    if(!fbt)
        return -1;
    pre= free_block;
    fbt->start_addr= ab->start_addr;
    fbt->size= ab->size;
    fbt->next= NULL;
    if(pre!= NULL)
    {
        while(pre->next!= NULL)
            pre= pre->next;
        pre->next= fbt;
    }
    else
    {
        free_block= fbt;
    }
    rearrange_FF();
    pre= free_block;
    work= pre->next;
    while(work!= NULL)
    {
        if(pre->start_addr+ pre->size== work->start_addr)
        {
            pre->size+= work->size;
            free(work);
            work= pre->next;
        }
        else
        {
            pre= work;
            work= work->next;
        }
    }
    current_free_mem_size+= ab->size;
    return 1;
}

//删除进程,归还分配的存储空间,并删除描述该进程内存分配的结点
void kill_process(int x)
{
    struct allocated_block *ab;
    int pid;
    
    pid=x;
    
    ab= find_process(pid);
    if(ab!= NULL)
    {
        free_mem(ab);  //释放ab所表示的分配区
        dispose(ab);   //释放ab数据结构结点
    }
}

 

5. 运行结果

内存分配

 

 内存释放

 

posted on 2020-05-17 16:56  chenyuekai  阅读(234)  评论(0编辑  收藏  举报