1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| //RAM内存类 import java.util.ArrayList; import java.util.Random; import java.util.Scanner;
public class RAM { Scanner input = new Scanner(System.in); static IdlePartition idle = new IdlePartition(); private Integer size; //总内存大小 private Integer initialAddress; //起址 private Integer endAddress; //末址 private Integer blockNum ; //块数 private ArrayList blockSize; //块大小 private ArrayList blockAddress; //每块的起址 private ArrayList blockStatus; //每块的状态
public RAM(Integer size, Integer initialAddress) { this.size = size; this.initialAddress = initialAddress; this.endAddress = this.initialAddress + this.size; this.blockNum = 10; Random random = new Random(); Integer sum = 0; this.blockSize = new ArrayList(); this.blockAddress = new ArrayList(); this.blockStatus = new ArrayList(); for (int i = 0; i < 9; i++) { this.blockAddress.add(sum); this.blockSize.add(random.nextInt(100)+30); sum += (Integer)(this.blockSize.get(i)); this.blockStatus.add("未分配"); } this.blockSize.add(this.size - sum); this.blockAddress.add(sum); this.blockStatus.add("未分配"); Printer(); } public IdlePartition Changing(){ ArrayList initialAddress1 = new ArrayList(); //起始地址 ArrayList lenth = new ArrayList(); //长度 ArrayList status = new ArrayList(); //状态 for (int i = 0; i < this.blockNum; i++) { if (this.blockStatus.get(i) == "未分配"){ initialAddress1.add(this.blockAddress.get(i)); lenth.add(this.blockSize.get(i)); status.add(this.blockStatus.get(i)); } } idle.setInitialAddress(initialAddress1); idle.setLenth(lenth); idle.setStatus(status); return idle; } public void Printer(){ System.out.println("***********内存分块说明表*************"); System.out.printf("%-7s%-10s%-10s%-10s\n","编号","起址","长度","状态"); for (int i = 0; i < this.blockNum; i++) { System.out.printf( "%2d%10d%s%10d%s%10s\n",i+1,this.blockAddress.get(i),"K" ,this.blockSize.get(i) ,"K",this.blockStatus.get(i) ); } } public void Printer1(IdlePartition idlePartition){ System.out.println("***********空闲说明表*************"); System.out.printf("%-7s%-10s%-10s%-10s\n","编号","起址","长度","状态"); for (int i = 0; i < idlePartition.getStatus().size(); i++) { System.out.printf( "%2d%10d%s%10d%s%10s\n",i+1,idlePartition.getInitialAddress().get(i),"K" ,idlePartition.getLenth().get(i) ,"K",idlePartition.getStatus().get(i) ); } }
/** * 查找空闲分区 */ public void FindingIdlePartition(IdlePartition idlePartition,Integer size,Integer a){ for (int i = 0; i < idlePartition.getStatus().size(); i++) { if (size == 0) { System.out.println("申请无效!"); return; } if ((Integer)(this.blockSize.get(i)) >= size && this.blockStatus.get(i).equals("未分配")) { this.blockStatus.set(i,"作业" + a + "正在使用中"); if ((Integer)(idlePartition.getLenth().get(i)) == size){ this.blockSize.set(i,size); return; }else { //使用空间,并建立新的空闲分区 this.blockAddress.add(i+1,(Integer)(this.blockAddress.get(i)) + size); this.blockStatus.add(i+1,"未分配"); this.blockSize.add(i+1,((Integer)this.blockSize.get(i)) - size); this.blockSize.set(i,size); this.blockNum++; return; } } } System.out.println("未找到适当的内存,请重新申请!!!"); } /** * 作业i申请空间 */ public void Applying(Integer i){ System.out.print("请输入要申请的作业" + i + "占用内存:"); FindingIdlePartition(idle,input.nextInt(),i); } /** * 作业i释放空间 */ public void Freeing(Integer i){ for (int j = 0; j < this.blockNum; j++) { if (this.blockStatus.get(j).equals("作业" + i + "正在使用中")){ this.blockStatus.set(j,"未分配"); this.blockSize.set(j,(Integer)this.blockSize.get(j)+(Integer)this.blockSize.get(j+1)); this.blockSize.remove(j+1); this.blockAddress.remove(j+1); this.blockStatus.remove(j+1); this.blockNum--; return; } } System.out.println("未找到作业" + i + "正在使用中,请重新输入!!!"); }
public ArrayList getBlockAddress() { return blockAddress; }
public ArrayList getBlockStatus() { return blockStatus; }
public Integer getBlockNum() { return blockNum; }
public ArrayList getBlockSize() { return blockSize; }
public Integer getSize() { return size; }
public void setSize(Integer size) { this.size = size; }
public Integer getInitialAddress() { return initialAddress; }
public void setInitialAddress(Integer initialAddress) { this.initialAddress = initialAddress; }
public Integer getEndAddress() { return endAddress; }
}
|