固定分区存储空间的分配和回收Java代码实现

目录

一、实验要求

二、实验目的

三、源代码

四、实验结果

五、实验总结 

一、实验要求

  1. 实现固定分区存储管理方式下存储空间的分配和回收。
  2. 已知当前内存分配表如下:

分区号

起始地址

长度

状态

110KB30KBJob5
240KB7KB0
347KB50KBJob2

3.有若干个作业申请或释放内存空间,请求如下:

  1. 作业Job6请求资源,申请20KB大小的内存空间;
  2. 作业Job7请求资源,申请5KB大小的内存空间;
  3. 作业Job2执行完毕,释放空间。

4.编写程序实现相应存储空间的分配和回收,若请求成功,修改主分配表,并输出该表,若请求不能满足,输出“分配失败”。

二、实验目的

     通过编写固定分区存储管理的模拟程序,加深对操作系统存储管理功能中固定分区管理方式、主存分配表等相应知识的理解。

三、源代码

import java.util.Scanner;
class Store{
    //分区号
    public  String distinct;
    //起始地址
    public  String beginAddress;
    //长度
    public  int lengthAddress;
    //状态
    public  String stausWork;
    public Store(String distinct, String beginAddress, int lengthAddress, String stausWork) {
        super();
        this.distinct = distinct;
        this.beginAddress =beginAddress;
        this.lengthAddress =lengthAddress;
        this.stausWork = stausWork;
    }
}
public class Mainclass {
    public static void main(String[] args) {
        Store[] st= new Store[3];
        st[0]=new Store("1","10KB",30,"job5");
        st[1]=new Store("2","40KB",7,"0");
        st[2]=new Store("3","47KB",50,"job2");
        System.out.println("分区号 "+"起始地址  "+"长度  "+"状态");
        for(int i=0;i<st.length;i++){
            if(st[i].lengthAddress<=10){
                System.out.println(st[i].distinct+"    "+st[i].beginAddress+"    "+st[i].lengthAddress +"     "+st[i].stausWork);

            }else{
                System.out.println(st[i].distinct+"    "+st[i].beginAddress+"    "+st[i].lengthAddress +"    "+st[i].stausWork);
            }
        }
        while(true){
            System.out.println("--------------------");
            System.out.println("   0:退出");
            System.out.println("   1:申请资源");
            System.out.println("   2:释放资源");
            System.out.println("--------------------");
            System.out.println("请输入对应的菜单:");
            Scanner scan = new Scanner(System.in);
            int number = scan.nextInt();
            //申请资源
            if(number==1){
                System.out.println("请输入你想要申请资源的内存大小:");
                int storeSize = scan.nextInt();
                System.out.println("请输入工作名字:");
                scan.nextLine();
                int flag=0;
                int a =10;
                String workname = scan.nextLine();
                for(int i=0;i<st.length;i++){
                    if(st[i].lengthAddress >=storeSize){
                        a=5;
                        flag=5;
                        if(st[i].stausWork.equals("0")) {
                            st[i].stausWork=workname;
                            flag++;
                            a--;
                            break;
                        }
                    }

                }
                if(a==10) {
                    System.out.println("内存过大");
                }

                if(flag==5&&a==5){
                    System.out.println("无可分配分区");
                }

                for(int i=0;i<st.length;i++){
                    if(st[i].lengthAddress <=10){
                        System.out.println(st[i].distinct+"    "+st[i].beginAddress+"    "+st[i].lengthAddress+"     "+st[i].stausWork);

                    }else{
                        System.out.println(st[i].distinct+"    "+st[i].beginAddress+"    "+st[i].lengthAddress+"    "+st[i].stausWork);
                    }
                }
               //释放资源
            }else if(number==2){
                System.out.println("请输入工作名字:");
                scan.nextLine();
                String workname = scan.nextLine();
                for(int i=0;i<st.length;i++){
                    if(st[i].stausWork.equals(workname)){
                        st[i].stausWork="0";
                        break;
                    }
                }
                for(int i=0;i<st.length;i++){
                    if(st[i].lengthAddress<=10){
                        System.out.println(st[i].distinct+"    "+st[i].beginAddress+"    "+st[i].lengthAddress+"     "+st[i].stausWork);

                    }else{
                        System.out.println(st[i].distinct+"    "+st[i].beginAddress+"    "+st[i].lengthAddress+"    "+st[i].stausWork);
                    }
                }

            }else if(number==0){
                System.exit(0);
            }else{
                System.out.println("请输入正确序号!");
            }
        }

    }
}

四、实验结果

 

五、实验总结 

固定分区

原理:又称定长分区或静态分区模式,是满足多道程序设计需要的最简单的存储管理技术。基本思想:给进入主存的用户作业划分一块连续存储区域,把作业装入该连续存储区域,若有多个作业装入主存,则它们可并发执行。

使用大小相等的固定分区有两个难点:程序可能太大而不能放到一个分区中,内存的,利用率很低。由于被装入的数据块小于分区大小,从而导致分区内部有浪费现象,成为“内部碎片”。对与大小不等的分区策略,最简单的方法就是把每个进程分配到能够容纳它的最小分区中。

目前已经基本上没有什么场合使用固定分区。

优势:实现简单,只需要极少的操作系统开销

缺点:有内部碎片,对内存的使用不充分,活动进程的最大数目是固定的。

:一个固定长度的数据块,存储在二级存储器中(如磁盘)。数据页可以临时复制入内存的页框中。

段:一个变长的数据块,存储在二级存储中,整个段可以临时复制到内存的可用区域(分段),或者将段分成许多页,将页单独复制到内存中(分段分页相结合)。

重定位:就是把程序的逻辑地址空间变换成内存中的实际物理地址空间的过程

​ 操作系统存储至少要分成两级:内存和外存。内存提供快速的访问,成本相对较高,是易失性的,不能永久存储。外存比内存慢,但是便宜,非易失性,可以长期存储程序和数据。而较小的内存用于保存当前使用的程序和数据。

​ 内存管理最基本的操作时由处理器把程序装入内存中执行。

可变分区:可变分区存储管理不是预先把内存中的用户区域划分成若干固定
分区,而是在作业要求装入内存时,根据用户作业的大小和当时内存空间使用情况决定是否为该作业分配一个分区。因此分区大小不是预先固定的,而是按作业需求量来划分的;分区的个数和位置也不是预先确定的。它有效地克服了固定分区方式中,由于分区内部剩余内存空置造成浪费的问题。

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值