四种方法就不多介绍了,别的可以看,只是学生作品记录,做的地方还有许多不足
1.比如说当剩余空间Space的start = end相等时没有删除该剩余空间,反正是不想想了
2.还有的就是main方法只能理解过后才明白它在干什么,不能很清晰的设计程序步骤
3.ps:应付作业,我真可恶啊
package OS.Memory.T2;
import java.util.*;
public class Memory {
public static void main(String[] args) {
Memory memory = new Memory(20);
memory.addZone(4, 7);
memory.addZone(12, 17);
memory.calculateRest();
memory.showSpaces();
memory.allocation(3);
memory.allocation(5);
}
private int size;
//仅在领近算法时有用到
private int pointer;
private LinkedList<Zone> zones;
private List<Space> rest;
//占用空间类
class Zone {
//分区始址
private int start;
//分区终址
private int end;
//空闲状态
private boolean isFree;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public Zone(int start, int end) {
this.start = start;
this.end = end;
}
}
// 剩余空间类
class Space {
private int sstart;
private int send;
public Space(int sstart, int send) {
this.sstart = sstart;
this.send = send;
}
public int getSstart() {
return sstart;
}
public void setSstart(int sstart) {
this.sstart = sstart;
}
public int getSend() {
return send;
}
public void setSend(int send) {
this.send = send;
}
public Space() {
}
@Override
public String toString() {
return "Space{" +
"sstart=" + sstart +
", send=" + send +
'}';
}
}
// 判断zones已经占用的空间后 计算剩余空间
public void calculateRest() {
sortZones();
int length = zones.size();
rest = new ArrayList<>(length + 1);
int restI = 0;
int restJ = 0;
for (int i = 0; i < length; i++) {
Zone tmp = zones.get(i);
if (tmp.getStart() == 0) {
continue;
} else {
restJ = tmp.getStart();
}
rest.add(new Space(restI, restJ));
if (tmp.getEnd() == size) {
break;
} else {
restI = tmp.getEnd();
}
}
Space space = new Space(restI, size);
rest.add(space);
}
//最佳适应算法调用 其余不调用
public void sortSpacesDesc() {
Collections.sort(rest, new Comparator<Space>() {
@Override
public int compare(Space o1, Space o2) {
return (o1.getSend() - o1.getSstart()) > (o2.getSend() - o2.getSstart()) ? 1 : -1;
}
});
}
//最坏适应算法调用 其余不调用
public void sortSpacesASC() {
Collections.sort(rest, new Comparator<Space>() {
@Override
public int compare(Space o1, Space o2) {
return (o1.getSend() - o1.getSstart()) > (o2.getSend() - o2.getSstart()) ? -1 : 1;
}
});
}
public void sortZones() {
Collections.sort(zones, new Comparator<Zone>() {
@Override
public int compare(Zone o1, Zone o2) {
int start1 = o1.getStart();
int start2 = o2.getStart();
return start1 > start2 ? start1 : (start1 == start2) ? 1 : -1;
}
});
}
public void addZone(int start, int end) {
int startI = 0;
int endJ = 0;
for (int i = 0; i < zones.size(); i++) {
Zone tmp = zones.get(i);
startI = tmp.getStart();
endJ = tmp.getEnd();
if ((endJ < start) || (startI > end)) {
continue;
} else {
System.out.println("插入失败");
break;
}
}
Zone zone = new Zone(start, end);
zones.add(zone);
}
public void showSpaces() {
System.out.println("剩余空间始地址|剩余空间终址");
for (int i = 0; i < rest.size(); i++) {
System.out.println("起点:" + rest.get(i).getSstart() + "\t\t终点:" + rest.get(i).getSend());
}
}
public Memory() {
this.size = 100;
this.pointer = 0;
this.zones = new LinkedList<>();
}
public Memory(int size) {
this.size = size;
this.pointer = 0;
this.zones = new LinkedList<>();
}
public void allocation(int size) {
System.out.println("即将占用" + size + "大小内存");
System.out.println("1.FirstFit(首次适应) 2.NextFit(邻近适应) 3.BestFit(最佳适应) 4.WorstFit(最坏适应)");
System.out.print("请选择分配算法:");
Scanner in = new Scanner(System.in);
int algorithm = in.nextInt();
switch (algorithm) {
case 1:
fristFit(size);
break;
case 2:
nextFit(size);
break;
case 3:
bestFit(size);
break;
case 4:
worstFit(size);
break;
default:
System.out.println("请重新选择!");
}
}
//首次适应算法
private void fristFit(int size) {
for (int i = 0; i < rest.size(); i++) {
Space tmp = rest.get(i);
if (tmp.getSend() - tmp.getSstart() >= size) {
tmp.setSstart(tmp.getSstart() + size);
showSpaces();
break;
}
}
}
// 循环首次适应算法
private void nextFit(int size) {
for (int i = pointer; i < rest.size(); i++) {
Space tmp = rest.get(i);
if (tmp.getSend() - tmp.getSstart() >= size) {
tmp.setSstart(tmp.getSstart() + size);
pointer = i;
showSpaces();
break;
}
}
}
//最佳适应算法
private void bestFit(int size) {
sortSpacesDesc();
for (int i = 0; i < rest.size(); i++) {
Space tmp = rest.get(i);
if (tmp.getSend() - tmp.getSstart() >= size) {
tmp.setSstart(tmp.getSstart() + size);
showSpaces();
break;
}
}
}
// 最坏适应算法
private void worstFit(int size) {
sortSpacesASC();
for (int i = 0; i < rest.size(); i++) {
Space tmp = rest.get(i);
if (tmp.getSend() - tmp.getSstart() >= size) {
tmp.setSstart(tmp.getSstart() + size);
showSpaces();
break;
}
}
}
}