Java简单实现——操作系统基于顺序搜索的动态区间分配

实现功能

  1. 首次适应算法(FirstFit)
  2. 循环首次适应算法(NextFit)
  3. 最佳适应算法(BestFit)
  4. 最差适应算法(WorstFit)

源代码

  1. MemoryBlock class 内存块
package com.company.dynamicinterval;

/**
 * @author hudongsheng
 * @date 2020/12/15 - 21:47
 * 划分节点
 */
public class MemoryBlock {
    private int head;       //小内存块的起始地址
    private int size;       //小内存块的大小
    private boolean isFree; //小内存块的空闲状态
    public MemoryBlock next;

    public MemoryBlock(int head, int size) {
        this.head = head;
        this.size = size;
        this.isFree = true;
    }

    public int getHead() {
        return head;
    }

    public void setHead(int head) {
        this.head = head;
    }

    public int getSize() {
        return size;
    }

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

    public boolean isFree() {
        return isFree;
    }

    public void setFree(boolean free) {
        isFree = free;
    }
}

  1. Manage class 内存块管理
package com.company.dynamicinterval;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author hudongsheng
 * @date 2020/12/15 - 21:47
 * 内存链
 */
public class Manage {
    //空闲分区链
    private LinkedList<MemoryBlock> linkedList = new LinkedList();
    //初始内存链
    public void initRam(int number,int size){
        int headpos = 0;
        for (int i = 0; i < number; i++) {
            MemoryBlock memoryBlock = new MemoryBlock(headpos,size);
            linkedList.addLast(memoryBlock);
            headpos += size;
        }
    }

    //申请内存
    public boolean setRam(int pos,int size){
        //遍历分区找要申请的内存
        MemoryBlock node;
        for (int i = 0; i < linkedList.size(); i++) {
            node = linkedList.get(i);
            //判断是否是要找的分区
            if (node.getHead() == pos){
                //分类别 申请
                if(size == 100){
                    node.setFree(false);
                }else {
                    MemoryBlock newNode = new MemoryBlock(node.getHead(),size);
                    newNode.setFree(false);
                    linkedList.add(i,newNode);
                    node.setSize(node.getSize()-size);
                    node.setHead(node.getHead()+size);
                }
                return true;
            }
        }
        return false;
    }

    //内存空间
    public void show(){
        MemoryBlock node;
        for (int i = 0; i < linkedList.size(); i++) {
            node = linkedList.get(i);
            System.out.println( "第"+i+"个内存块: "+
                                "起始地址"+node.getHead()+
                                "结束地址"+(node.getHead()+node.getSize()-1)+
                                "内存大小"+node.getSize()+
                                "是否可用"+node.isFree());
        }
    }

    public void start(){
        int result = -1;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内存块个数和大小");
        initRam(scanner.nextInt(),scanner.nextInt());
        show();
        while (true){
            System.out.println("请输入请求的内存块大小: ");
            int size = scanner.nextInt();
            System.out.println("----------------");
            System.out.println("1.首次适应算法");
            System.out.println("2.循环首次适应算法");
            System.out.println("3.最佳适应算法");
            System.out.println("4.最差适应算法");
            System.out.println("----------------");
            System.out.println("请输入要选择的算法: ");
            int chioce = scanner.nextInt();
            switch (chioce){
                case 1:
                    result =  FirstFit.firstFit(size,linkedList);
                    break;
                case 2:
                    result = NextFit.nextFit(size,linkedList);
                    break;
                case 3:
                    result = BestFit.bestFit(size,linkedList);
                    break;
                case 4:
                    result = WorstFit.worstFit(size,linkedList);
                    break;
            }
            if(result != -1){
                if(setRam(result,size)){
                    System.out.println("分配成功");
                }else {
                    System.out.println("分配失败");
                }
            }else {
                System.out.println("无可用内存空间申请失败");
            }
            show();
            System.out.println();
        }
    }

}

3.Test class 测试类

package com.company.dynamicinterval;

/**
 * @author hudongsheng
 * @date 2020/12/9 - 21:04
 */
public class Test {
    public static void main(String[] args) {
        Manage manage = new Manage();
        manage.start();
    }
}

  1. FirstFit class 首次适应
package com.company.dynamicinterval;

import java.util.LinkedList;

/**
 * @author hudongsheng
 * @date 2020/12/15 - 21:23
 * 首次适应
 */
public class FirstFit {

    public static int firstFit(int size, LinkedList<MemoryBlock> linkedList){
        MemoryBlock node = linkedList.getFirst();
        for (int i = 1;node != null; i++) {
            if(node.getSize() >= size && node.isFree()){
                return node.getHead();
            }
            node = linkedList.get(i);
        }
        return -1;
    }
}

  1. NextFit class 循环首次适应
package com.company.dynamicinterval;

import java.util.LinkedList;

/**
 * @author hudongsheng
 * @date 2020/12/15 - 21:24
 * 循环首次适应
 */
public class NextFit {
    //记录遍历位置
    private static int index = 0;


    public static int nextFit(int size, LinkedList<MemoryBlock> linkedList){
        //遍历次数
        int count = 0;
        MemoryBlock node = linkedList.getFirst();
        for (int i = index;count < linkedList.size(); i = (i+1)%linkedList.size()) {
            if(node.getSize() >= size && node.isFree()){
                index = i;
                return node.getHead();
            }
            node = linkedList.get(i);
            count++;
            //循环
            if(index == linkedList.size()){
                node = linkedList.getFirst();
            }
        }
        return -1;
    }
}

  1. BestFit class 最佳适应
package com.company.dynamicinterval;

import java.util.LinkedList;

/**
 * @author hudongsheng
 * @date 2020/12/15 - 21:24
 * 最佳适应
 */
public class BestFit {

    public static int bestFit(int size, LinkedList<MemoryBlock> linkedList){
        MemoryBlock node = linkedList.getFirst();
        //合适块大小
        int blockSize = -1;
        //最佳块大小
        int bestSize = -1;
        //最佳块位置
        int best = -1;
        for (int i = 1;i<linkedList.size(); i++) {
            if(node.getSize() >= size && node.isFree()){
                blockSize = node.getSize()-size;
                if(bestSize == -1){
                    bestSize = blockSize;
                    best = node.getHead();
                }
                if(blockSize < bestSize){
                    bestSize = blockSize;
                    best = node.getHead();
                }
            }
            node = linkedList.get(i);
        }
        return best;
    }
}

7.WorstFit class 最差适应

package com.company.dynamicinterval;

import java.util.LinkedList;

/**
 * @author hudongsheng
 * @date 2020/12/15 - 21:24
 * 最坏适应
 */
public class WorstFit {

    public static int worstFit(int size, LinkedList<MemoryBlock> linkedList){
        MemoryBlock node = linkedList.getFirst();
        //合适块大小
        int blockSize = -1;
        //最差块大小
        int worstSize = -1;
        //最差块位置
        int worst = -1;
        for (int i = 1;i<linkedList.size(); i++) {
            if(node.getSize() >= size && node.isFree()){
                blockSize = node.getSize()-size;
                if(worstSize == -1){
                    worstSize = blockSize;
                    worst = node.getHead();
                }
                if(blockSize > worstSize){
                    worstSize = blockSize;
                    worst = node.getHead();
                }
            }
            node = linkedList.get(i);
        }
        return worst;
    }
}

总结

学校的操作系统实验,只是简单模拟,只实现最基本功能,对于交互功能,异常处理,非法处理未实现,欢迎各位大佬指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值