ccyy's coding zone
往前走,不要留恋路边的风景.
posts - 25,comments - 9,trackbacks - 0
纯粹一SB模拟....

编写程序完成动态分区存储管理方式的内存分配和回收的实现

, 实验具体包括:
首先确定内存空间分配表,然后采用最先适配算法,完成内存空间的分配和回收.

C++语言: 模拟内存分配
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 10
int free_p;
int used_p;
struct table
{
    int address;
    int len;
    int flag;
    int run_id;
};
table used[Max],Free[Max];
struct Process
{
    int id;
    int len;
}p[Max];
int cmp(const table &a,const table &b)
{
    return a.address<b.address;
}
void allocate(int id,int len)
{
        int i,j;
    for(i=0;i<free_p;i++)
    {
        if(Free[i].len>=len)
        {
            used[used_p].address=Free[i].address;
            used[used_p].len=len;
            used[used_p].run_id=id;
            used_p++;
            if(Free[i].len>len)
            {
                Free[i].address+=len;
                Free[i].len-=len;
            }
            else
            {
                free_p--;
                for(j=i;j<free_p;j++)
                {
                    Free[j]=Free[j+1];
    n            }
            }
            break;
        }
    }
   
}
void reclaim(int id)
{
    int i,j,k;
    for(i=0;i<used_p;i++)
    {
        if(used[i].run_id==id)
        {
            int add=used[i].address;
            int length=used[i].len;   
            used_p--;
            for(j=i;j<used_p;j++)
            {
                used[j]=used[j+1];
            }
       
            Free[free_p].address=add;
            Free[free_p].len=length;
            free_p++;
            sort(Free,Free+free_p,cmp);
            for(j=0;j<free_p;j++)
            {
               
                if(j + 1 < free_p&&Free[j].address+Free[j].len==Free[j + 1].address)
                {
                    Free[j].len+=Free[j + 1].len;
                    free_p--;
                    for(k =j + 1; k < free_p;k++)
                    {
                        Free[k]=Free[k+1];
                    }
                }
           
            }
       
        }
    }
   
}
void show()
{
     int i;
     sort(used,used+used_p,cmp);
     printf("***********used table:***********\n");
     for(i=0;i<used_p;i++)
     {
         printf("%d %7d %7d %7d\n",i,used[i].run_id,used[i].address,used[i].len);

     }
     printf("***********Free table:***********\n");
     for(i=0;i<free_p;i++)
     {
         printf("%d %7d %7d\n",i,Free[i].address,Free[i].len);
     }
     printf("end\n");
}
int main()
{
    int i,type,id;
    int xk;
    //空闲区表初始化
    Free[0].address=10240;
    Free[0].len=102400;
    Free[0].flag=1;
    for(i=1;i<Max;i++)
       Free[i].flag=0;
    //已分配1区表初始化
    for(i=0;i<Max;i++)
       used[i].flag=0;
    free_p=1;
    used_p=0;
    while(1)
    {
        printf("**************please chose the type of the functions:**************\n 0--exit   1--allocate   2--reclaim   3--show\n");
        //printf("please chose the function type\n");
        scanf("%d",&type);
        switch(type)
        {
            case 0: return 0;
            case 1:
                printf("*********please input processing name and lenth:**********\n");
                scanf("%d %d",&id,&xk);
                allocate(id,xk);
                break;
            case 2:
                printf("*********please intput the prossing name to be reclaim:**********\n");
                scanf("%d",&id);
                reclaim(id);
                break;
            case 3:
                show();
  
        }
    }    
    return 0;
}
阅读全文
类别:默认分类 查看评论
文章来源:http://hi.baidu.com/%D2%EC%B6%C8%BF%D5%BC%E4%5F%B5%DA%CB%C4%CE%AC/blog/item/ae9d4ea669eacefb9152eed4.html
posted on 2010-05-19 20:52 ccyy 阅读(356) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理