操作系统——存储器管理

  1. 设在初始状态下,可用的内存容量为320KB。
  2. 根据动态分区分配的原理,主要设置两个数据结构:空闲分区表(链)和已分配分区表,均包含分区的起始地址、长度等信息。
  3. 用户可通过控制台或文件输入控制命令,如New:新建作业,分配作业所需的内存大小;Done:某作业执行完毕,回收作业占用的内存;Show:显示当前所有作业所占用的分区和空闲分区。
  4. 当有新作业请求装入主存时,需查找空闲分区表,分别按照首次适应算法、最佳适应算法、最坏适应算法的策略从中找到一个合适的空闲分区,将其分配给作业,按照作业请求的内存大小进行分配,其余的部分仍为空闲分区;将作业占用的部分记录到已分配分区表中,分区的剩余部分仍记录在空闲分区表中。
  5. 作业执行完毕后,应回收作业占用的分区,删除已分配分区表中的表项,然后修改空闲分区表,并根据情况增加或合并空闲分区。
#include<bits/stdc++.h>
using namespace std;
class Memory{
public:
    explicit Memory(int size){
        free.emplace_back(0, size);
    }
    void New()
    {
        string fun;
        cout<<"输入分配方式(first_fit/best_fit/worst_fit):";
        cin>>fun;
        cout<<"作业大小:";
        int size;
        cin>>size;
        if(fun=="first_fit")
        {
            sort(free.begin(),free.end(),first_fit);
            for(auto p=free.begin();p!=free.end();p++)
            {
                if(p->second>size)
                {
                    all.push_back({p->first,size});
                    p->first+=size;
                    p->second-=size;
                    return;
                }else if(p->second==size)
                {
                    all.push_back({p->first,size});
                    free.erase(p);
                    return;
                }
            }
        }else if(fun=="best_fit")
        {
            sort(free.begin(),free.end(),best_fit);
            for(auto p=free.begin();p!=free.end();p++)
            {
                if(p->second>size)
                {
                    all.emplace_back(p->first,size);
                    p->first+=size;
                    p->second-=size;
                    return;
                }else if(p->second==size)
                {
                    all.emplace_back(p->first,size);
                    free.erase(p);
                    return;
                }
            }
        }else if(fun=="worst_fit")
        {
            sort(free.begin(),free.end(),worst_fit);
            auto p=free.begin();
            if(p->second>size)
            {
                all.emplace_back(p->first,size);
                p->first+=size;
                p->second-=size;
                return;
            }else if(p->second==size)
            {
                all.emplace_back(p->first,size);
                free.erase(p);
                return;
            }
        }else
        {
            New();
        }
        cout<<"内存不足\n";

    }
    void Done()
    {
        cout<<"输入要回收地址的起始位置:";
        int start_address;
        cin>>start_address;
        for(auto p=all.begin();p!=all.end();p++)
        {
            if(p->first==start_address)
            {
                free.emplace_back(p->first,p->second);
                all.erase(p);
                break;
            }
        }
        sort(free.begin(),free.end());
        for(auto p=free.begin();p+1!=free.end();p++)
        {
            auto p2=p+1;
            if(p->first+p->second==p2->first)
            {
                p->second+=p2->second;
                free.erase(p2);
            }
        }
    }
    void Show()
    {
    	
        cout<<"-------------------------------\n";
        cout<<"空闲:\n    起始地址 |  长度\n";
        for(auto & p : free)
        {
            cout<<"\t"<<p.first<<"\t"<<p.second<<endl;
        }
        cout<<"已分配:\n    起始地址 |  长度\n";
        for(auto &p:all)
        {
            cout<<"\t"<<p.first<<"\t"<<p.second<<endl;
        }
        cout<<"-------------------------------\n";
    }
private:
    vector<pair<int,int>> free;
    vector<pair<int,int>> all;
    function<bool(pair<int,int>&,pair<int,int>&)> first_fit=[&](pair<int,int>& x,pair<int,int>& y)->bool {
        return x.first<y.first;
    };
    function<bool(pair<int,int>&,pair<int,int>&)> best_fit=[&](pair<int,int>& x,pair<int,int>& y)->bool {
        if(x.second==y.second)return x.first<y.first;
        return x.second<y.second;
    };
    function<bool(pair<int,int>&,pair<int,int>&)> worst_fit=[&](pair<int,int>& x,pair<int,int>& y)->bool {
        if(x.second==y.second)return x.first<y.first;
        return x.second>y.second;
    };

};
int main()
{
    Memory memory(320*1024);
    while(true)
    {
        cout<<"输入操作(New/Done/Show/Exit):";
        string fun;
        cin>>fun;
        if(fun=="New")
        {
            memory.New();
        }else if(fun=="Done")
        {
            memory.Done();
        }else if(fun=="Show")
        {
            memory.Show();
        }else if(fun=="Exit")
        {
            break;
        }
    }
}
/*
New
first_fit
50
New
first_fit
60
Show
Done
0
Show 
*/

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值