存储管理-分区分配与回收算法(Java简单实现)

来源:互联网 发布:京华科讯软件 编辑:程序博客网 时间:2024/06/05 06:56

主要目的:针对不同的放置策略建立相应队列结构

  • 编写分区分配算法
    • 最佳适应法
    • 首次适应法
  • 编写分区回收算法


需要注意的特殊情况:
  • 无空闲区存在时要求分配
  • 回收的空间end与空闲空间start相连
  • 回收的空间start与空闲空间end相连
  • 回收的空间与空闲空间两边都相连
  • 回收的空间与空闲空间两边都不相连
  • 回收的空间与空闲空间存在重复
  • 回收的空间超出最大空间限制(本例中最大地址为32766)
  • 回收时的首地址出现负值(本例中最小地址为0)
  • 要求分配的空间大于所有空闲空间

实现效果如图:
  • 最佳适应法

  • 首次适应法

  • 实现代码
    • FreeBlock.java
    • package nn_MemeryManagement;public class FreeBlock {private int adr;private int size;public FreeBlock(int p_adr, int p_size) {adr = p_adr;size = p_size;}public void printME() {System.out.println(this.getAdr() + "\t" + this.getEnd() + "\t"+ this.getSize());System.out.println("----------------------------");}public int getEnd() {if (size > 0) {return adr + size - 1;} else {return 0;}}public int getAdr() {return adr;}public void setAdr(int adr) {this.adr = adr;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}}


    • Main.java
    • package nn_MemeryManagement;import java.util.ArrayList;import java.util.Scanner;public class Main {static Scanner scanner = new Scanner(System.in);static ArrayList<FreeBlock> blockList = new ArrayList<FreeBlock>();static int application;static int adr;static int size;public static void main(String[] args) {initalize();}public static void initalize() {// 将整个存储区作为freeBlock初始化并显示信息FreeBlock freeBlock = new FreeBlock(0, 32767);blockList.add(freeBlock);printAll();print("Please input the way (1-best,2-first):");int way = scanner.nextInt();if (way == 1) {bestClass();} else if (way == 2) {firstClass();} else {print("Error!\n");}}public static void bestClass() {int type = getRequest();if (type == 1) {assign(1, application);} else if (type == 2) {accept(adr, size);} else {print("Error!\n");}bestClass();}public static void firstClass() {int type = getRequest();if (type == 1) {assign(2, application);} else if (type == 2) {accept(adr, size);} else {print("Error!\n");}firstClass();}public static void printAll() {print("adr\tend\tsize\n");print("----------------------------\n");for (FreeBlock block : blockList) {block.printME();}}public static int getRequest() {print("Assign or Accept (1-Assign,2-Accept):");int type = scanner.nextInt();if (type == 1) {print("input Application:");application = scanner.nextInt();} else if (type == 2) {print("input adr and size:");adr = scanner.nextInt();size = scanner.nextInt();} else {print("Error!\n");}return type;}public static boolean assign(int p_way, int p_application) {// 判断是否有空闲区if (blockList.isEmpty()) {print("没有任何空闲区域可供分配!\n");return false;}// 按各自的原则查找空闲区if (p_way == 1) {// bestint minSize = 32767;int minIndex = -1;for (FreeBlock block : blockList) {if (block.getSize() <= minSize&& block.getSize() >= p_application) {minSize = block.getSize();minIndex = blockList.indexOf(block);}}if (minIndex == -1) {print("没有符合要求的空闲区域!\n");return false;} else {FreeBlock tempBlock1 = blockList.get(minIndex);if (tempBlock1.getSize() == p_application) {blockList.remove(tempBlock1);printAll();return true;}FreeBlock tempBlock2 = new FreeBlock(tempBlock1.getAdr(),tempBlock1.getSize() - p_application);blockList.set(minIndex, tempBlock2);printAll();return true;}} else if (p_way == 2) {// firstint minAdr = 32766;int minIndex = -1;for (FreeBlock block : blockList) {if (block.getAdr() <= minAdr&& block.getSize() >= p_application) {minAdr = block.getSize();minIndex = blockList.indexOf(block);}}if (minIndex == -1) {print("没有符合要求的空闲区域!\n");return false;} else {FreeBlock tempBlock1 = blockList.get(minIndex);if (tempBlock1.getSize() == p_application) {blockList.remove(tempBlock1);printAll();return true;}FreeBlock tempBlock2 = new FreeBlock(tempBlock1.getAdr(),tempBlock1.getSize() - p_application);blockList.set(minIndex, tempBlock2);printAll();return true;}} else {print("Error!\n");return false;}}public static boolean accept(int p_adr, int p_size) {int p_end = adr + size - 1;// 检查:首地址小于最小地址(0)if (p_adr < 0) {print("错误:首地址小于最小地址(0)!\n");return false;}// 检查:回收空间大于最大空间(32766)if (p_end > 32766) {print("错误:回收空间大于最大空间(32766)!\n");return false;}// 检查:回收空间和空闲空间重叠for (FreeBlock block : blockList) {if (p_adr >= block.getAdr() && p_adr <= block.getEnd()) {print("错误:回收空间和空闲空间重叠!\n");return false;}if (p_end >= block.getAdr() && p_end <= block.getEnd()) {print("错误:回收空间和空闲空间重叠!\n");return false;}}// 检查:前有接续for (FreeBlock block : blockList) {if (p_adr - 1 == block.getEnd()) {block.setSize(block.getSize() + p_size);// 在前有接续的基础上,检查:后有接续for (FreeBlock block2 : blockList) {if (block.getEnd() + 1 == block2.getAdr()) {block.setSize(block.getSize() + block2.getSize());blockList.remove(block2);printAll();return true;}}printAll();return true;}}// 检查:后有接续(前肯定没有接续)for (FreeBlock block : blockList) {if (p_end + 1 == block.getAdr()) {block.setAdr(p_adr);block.setSize(block.getSize() + p_size);printAll();return true;}}// 前后均无接续FreeBlock freeBlock = new FreeBlock(p_adr, p_size);blockList.add(freeBlock);printAll();return true;}public static void print(String printString) {System.out.print(printString);}}


0 0
原创粉丝点击