操作系统存储管理分区分配算法编程实现

一、实验要求

1. 要求

在可变分区管理模式下采用首次适应法和最佳适应法实现主存的分配和回收。

建立分区描述区(用空闲分区表或空闲分区链描述内存所有空闲区)

建立自由主存队列(针对不同的放置策略建立相应队列结构)

编写分区分配算法

2.具体步骤:

从未分配表中找到一个足以容纳该作业的可用空白区(未分配区);如果这个空白区比所要求的大,则将它分成两部分:一部部分成为已经分配的分区,剩余部分仍为空白区;修改数据结构的有关信息,并回送一个所分配分区的序号或该分区的始址。

编写分区回收算法。(检查回收的分区是否与空白区相邻接,如有则加以合并,使之成为一个连续的空白区)

二、程序数据结构设计介绍

Adr:分区地址 End:末地址  Size:分区大小  ap:application,申请的区域

c:选择算法 c1:选择动态分配和回收算法

三、代码

1.最佳适应算法C++

#include<bits/stdc++.h>
using namespace std;
struct index
{
    int Adr,End,Size;
}a[100];
int n=1;
bool cmp(index x,index y)
{
    if(x.Size==y.Size)
        return x.Adr<y.Adr;
    return x.Size<y.Size;
}

bool cmp1(index x,index y)
{
    return x.Adr<y.Adr;
}
void accept(int x,int y)
{
    int flag=0;
    sort(a,a+n,cmp1);
    for(int i=0;i<n;i++)
    {
        if(a[i].End+1==x&&x+y-1+1==a[i+1].Adr)
        {
            a[i].Size+=y+a[i+1].Size;
            a[i].End=a[i].Adr+a[i].Size-1;
            n--;
            for(int j=i+1;j<n;j++)
            {
                a[j]=a[j+1];
            }
            flag=1;
            break;
        }
        else if(a[i].End+1==x)
        {
            a[i].Size+=y;
            a[i].End=a[i].Adr+a[i].Size-1;
            flag=1;
            break;
        }
        else if(x+y-1+1==a[i].Adr)
        {
            a[i].Adr=x;
            a[i].Size+=y;
            a[i].End=a[i].Adr+a[i].Size-1;
            flag=1;
            break;
        }
        else if((a[i].Adr<=x&&a[i].End>=x)||(a[i].Adr<=(x+y-1)&&a[i].End>=(x+y-1)))
        {
            cout<<"回收区域与空闲区域重复!"<<endl;
            flag=1;
            break;
        }
    }
    if(!flag)
    {
        a[n].Adr=x;
        a[n].Size=y;
        a[n].End=x+y-1;
        n++;
    }
    sort(a,a+n,cmp);

}
int Assign(int x)
{
    sort(a,a+n,cmp);
    int temp=-1;
    for(int i=0;i<n;i++)
    {
        if(a[i].Size>=x)
        {
            a[i].Size-=x;
            if(a[i].Size==0)
            {
                n--;
                for(int j=i+1;j<n;j++)
                {
                a[j]=a[j+1];
                }
            }
            a[i].End-=x;
            temp=a[i].End+1;
            break;
        }
    }
    return temp;
}
int main()
{
    int adr,size;
    int t;
    int ap;
    string c,c1;
    n=1;
    a[0].Adr=0;
    a[0].Size=32767;
    a[0].End=32766;
    cout<<"input    the  way (best  or  first):";
    cin>>c;
    while(1)
    {
        cout<<"Index   *       adr    *      end      *       size   "<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<i+1<<setw(16)<<a[i].Adr<<setw(17)<<a[i].End<<setw(17)<<a[i].Size<<endl;
        }
        cout<<"Assign  or  Accept: "<<endl;
        cout<<"输入0返回"<<endl;
        cin>>c1;
        if(c1=="as")
        {
            cout<<"input APPLICATION: ";
            cin>>ap;
            if(n==0)
            {
                cout<<"List is EMPTY!"<<endl;
                continue;
            }
            t=Assign(ap);
            if(t==-1)
            {
                cout<<"Too   large  application!"<<endl;
            }
            else
            {
                cout<<"SUCCESS!!!  ADDRESS="<<t<<endl;
            }
            sort(a,a+n,cmp);
        }
        else if(c1=="ac")
        {
            cout<<"Input  adr  and  size:";
            cin>>adr>>size;
            if(adr<0)
            {
                cout<<"回收时首地址不能为负!!"<<endl;
                continue;
            }
            else if(size>32766||adr+size>32766)
            {
                cout<<"回收超出最大空间"<<endl;
                continue;
            }
            accept(adr,size);
        }
        else if(c1=="0")
        {
            cout<<"程序结束,自动退出..."<<endl;
            return 0;
        }
        cout<<endl;

    }
}

2.首次适应算法C++

#include<bits/stdc++.h>
using namespace std;
struct index
{
    int Adr,End,Size;
}a[100];
int n=1;
bool cmp(index x,index y)
{
    if(x.Size==y.Size)
        return x.Adr<y.Adr;
    return x.Size<y.Size;
}

bool cmp1(index x,index y)
{
    return x.Adr<y.Adr;
}
void accept(int x,int y)
{
    int flag=0;
    sort(a,a+n,cmp1);
    for(int i=0;i<n;i++)
    {
        if(a[i].End+1==x&&x+y-1+1==a[i+1].Adr)
        {
            a[i].Size+=y+a[i+1].Size;
            a[i].End=a[i].Adr+a[i].Size-1;
            n--;
            for(int j=i+1;j<n;j++)
            {
                a[j]=a[j+1];
            }
            flag=1;
            break;
        }
        else if(a[i].End+1==x)
        {
            a[i].Size+=y;
            a[i].End=a[i].Adr+a[i].Size-1;
            flag=1;
            break;
        }
        else if(x+y-1+1==a[i].Adr)
        {
            a[i].Adr=x;
            a[i].Size+=y;
            a[i].End=a[i].Adr+a[i].Size-1;
            flag=1;
            break;
        }
        else if((a[i].Adr<=x&&a[i].End>=x)||(a[i].Adr<=(x+y-1)&&a[i].End>=(x+y-1)))
        {
            cout<<"回收区域与空闲区域重复!"<<endl;
            flag=1;
            break;
        }
    }
    if(!flag)
    {
        a[n].Adr=x;
        a[n].Size=y;
        a[n].End=x+y-1;
        n++;
    }

}
int Assign(int x)
{
    int temp=-1;
    for(int i=0;i<n;i++)
    {
        if(a[i].Size>=x)
        {
            a[i].Size-=x;
            if(a[i].Size==0)
            {
                n--;
                for(int j=i+1;j<n;j++)
                {
                a[j]=a[j+1];
                }
            }
            a[i].End-=x;
            temp=a[i].End+1;
            break;
        }
    }
    return temp;
}
int main()
{
    int adr,size;
    int t;
    int ap;
    string c,c1;
    n=1;
    a[0].Adr=0;
    a[0].Size=32767;
    a[0].End=32766;
    cout<<"input    the  way (best  or  first):";
    cin>>c;
    while(1)
    {
        cout<<"Index   *       adr    *      end      *       size   "<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<i+1<<setw(16)<<a[i].Adr<<setw(17)<<a[i].End<<setw(17)<<a[i].Size<<endl;
        }
        cout<<"Assign  or  Accept: "<<endl;
        cout<<"输入0返回"<<endl;
        cin>>c1;
        if(c1=="as")
        {
            cout<<"input APPLICATION: ";
            cin>>ap;
            if(n==0)
            {
                cout<<"List is EMPTY!"<<endl;
                continue;
            }
            t=Assign(ap);
            if(t==-1)
            {
                cout<<"Too   large  application!"<<endl;
            }
            else
            {
                cout<<"SUCCESS!!!  ADDRESS="<<t<<endl;
            }
        }
        else if(c1=="ac")
        {
            cout<<"Input  adr  and  size:";
            cin>>adr>>size;
            if(adr<0)
            {
                cout<<"回收时首地址不能为负!!"<<endl;
                continue;
            }
            else if(size>32766||adr+size>32766)
            {
                cout<<"回收超出最大空间"<<endl;
                continue;
            }
            accept(adr,size);
        }
        else if(c1=="0")
        {
            cout<<"程序结束,自动退出..."<<endl;
            return 0;
        }
        cout<<endl;

    }
}

四、结果

1.最佳适应

2.首次适应

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deleteeee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值