操作系统 动态分区分配 Java 实现

操作系统 动态分区分配 Java 实现

1. 分配算法原理

操作系统 动态分区分配

2. 代码实现

在这里插入图片描述

package dynamicMemoAlloc;

class Block{
    private int id; // id == -1 表示空闲分区
    private int begin;
    private int end;
    private int size;
    private Block next;

    public Block(int id, int begin, int size) {
        this.id = id;
        this.begin = begin;
        this.end = begin + size - 1;
        this.size = size;
        this.next = null;
    }

    public int getId() {
        return id;
    }

    public int getBegin() {
        return begin;
    }

    public int getEnd() {
        return end;
    }

    public int getSize() {
        return size;
    }

    public Block getNext() {
        return next;
    }


    public void setId(int id) {
        this.id = id;
    }

    public void setBegin(int begin) {
        this.begin = begin;
        this.size = this.end - begin + 1;
    }

    public void setEnd(int end) {
        this.end = end;
        this.size = end - this.begin + 1;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setNext(Block next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Block{" +
                "id=" + (id+1) +
                ", begin=" + begin +
                ", end=" + end +
                ", size=" + size +
                '}';
    }
}

public class Memory {

    private int memoSize;
    private Block head;
    private int idCounter;

    public Memory(int memoSize) {
        this.memoSize = memoSize;
        this.head = new Block(-2, 0, 0);
        Block b = new Block(-1, 0, memoSize);
        this.head.setNext(b);
        this.idCounter = -1;
    }

    // First Fit Allocation
    public void allocByFF(int size){
        Block b = head.getNext();
        Block last = head;
        while(b != null ){
            // 当前内存块为空闲状态,并且大小 >= 申请的内存大小,那么就是可分配的
            if(b.getId() == -1 && b.getSize() >= size){
                Block all = new Block(++idCounter, b.getBegin(), size);
                b.setBegin(all.getEnd()+1);
                last.setNext(all);
                all.setNext(b);
                break;
            }
            last = b;
            b = b.getNext();
        }
        /**if( b!= null)
            showMemoInfo("FF");
        else
            System.out.println("Error: Out of memory.");
         */
    }

    // Best Fit Allocation
    public void allocByBF(int size){
        int min = memoSize;
        int counter = 0;
        Block tarBefore = null;

        // 定位
        Block b = head.getNext();
        Block last = head;
        while(b != null){
            if(b.getId() == -1 && b.getSize() >= size ) {
                if(min > size)
                    tarBefore = last;
            }
            last = b;
            b = b.getNext();
        }

        // 添加节点
        Block target = tarBefore.getNext();
        Block all = new Block(++idCounter, target.getBegin(), size);
        target.setBegin(all.getEnd()+1);
        tarBefore.setNext(all);
        all.setNext(target);

        // showMemoInfo("BF");
    }

    public void free(int size){
        Block b = head.getNext();
        Block last = head;
        while(b != null){
            // 1. 释放与传入的 size 大小相同的内存区
            if(b.getSize() == size) {
                b.setId(-1);
                // 2. 如果前后相邻的内存区为空闲状态,那么归并前后相邻的内存区
                Block next = b.getNext();
                if(last.getId() == -1 && next.getId() == -1){
                    last.setEnd(next.getEnd());
                    last.setNext(next.getNext());
                    next.setNext(null);
                }else{
                    if(last.getId() == -1){
                        last.setEnd(b.getEnd());
                        last.setNext(b.getNext());
                    }
                    if(next.getId() == -1){
                        next.setBegin(b.getBegin());
                        last.setNext(next);
                        b.setNext(null);
                    }
                }
                break;
            }
            last = b;
            b = b.getNext();
        }
        // showMemoInfo();
    }

    public void showMemoInfo(String info){
        System.out.println("-----------------------------------------");
        System.out.println(info + ": ");
        Block b = head.getNext();
        while(b != null){
            System.out.println(b);
            b = b.getNext();
        }
    }

    public static void main(String[] args){
        // 首次适应
        Memory memo = new Memory(640);
        memo.allocByFF(130);
        memo.allocByFF(60);
        memo.allocByFF(100);
        memo.free(60);
        memo.allocByFF(200);
        memo.free(100);
        memo.free(130);
        memo.allocByFF(140);
        memo.allocByFF(60);
        memo.allocByFF(50);
        memo.free(60);
        memo.showMemoInfo("FF");

        // 最佳适应
        Memory memo1 = new Memory(640);
        memo1.allocByBF(130);
        memo1.allocByBF(60);
        memo1.allocByBF(100);
        memo1.free(60);
        memo1.allocByBF(200);
        memo1.free(100);
        memo1.free(130);
        memo1.allocByBF(140);
        memo1.allocByBF(60);
        memo1.allocByBF(50);
        memo1.free(60);
        memo1.showMemoInfo("BF");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值