可变分区存储模拟系统-为课程设计用的。操作系统
源代码在线查看: visual.java
public class visual {
AllProcess ALLPCS;
FreeTable FRTB ;
Memory MMY ;
process PCS;
TableItem TBIM;
boolean flag;
visual(boolean f, int size) {
ALLPCS = AllProcess.getInstence();
Memory.setSize(size);
MMY = Memory.getInstence();
//FreeTable.setBanJia(f);//是否使用搬家算法
flag = f;
FRTB= FreeTable.getInstence();
FRTB.clear();
TBIM = new TableItem(0, size);
FRTB.add(TBIM);//初始化,将整个内存作为一个空闲分区存入表中
}
public void setFlag(boolean f){this.flag=f;}
public boolean fenpei(String Name, int SIZE) { //分配
int start, size;
process P;
int num = FRTB.getBySize(SIZE);
if (num != -1) {
if (FRTB.getItem(num).getSize() >SIZE) { //>
P = new process(Name, SIZE);
P.setStart(FRTB.getItem(num).getStart());
MMY.fenpei(FRTB.getItem(num).getStart(),SIZE);
FRTB.getItem(num).setSize(FRTB.getItem(num).getSize() - SIZE);
FRTB.getItem(num).setStart(FRTB.getItem(num).getStart() + SIZE);
ALLPCS.add(P);
return true;
} else if (FRTB.getItem(num).getSize() == SIZE) { //=.
P = new process(Name, SIZE);
P.setStart(FRTB.getItem(num).getStart());
MMY.fenpei(FRTB.getItem(num).getStart(),SIZE);
FRTB.removeByStart(FRTB.getItem(num));
ALLPCS.add(P);
System.out.println(FRTB.size());
return true;
}
} else if (flag == true && MMY.getFree() >= SIZE) { //进行搬家运算
process temp;
for (int g = 0; g < ALLPCS.size(); g++) { //进程按START排序
for (int h = g; h < ALLPCS.size(); h++) {
if (ALLPCS.getByorder(h).getStart() <
ALLPCS.getByorder(g).getStart()) {
temp = ALLPCS.getByorder(h);
ALLPCS.setByorder(h, (process) ALLPCS.getByorder(g));
ALLPCS.setByorder(g, temp);
}
}
}
if(ALLPCS.getByorder(0).getStart()!=0){
int start1 = ALLPCS.getByorder(0).getStart();
int size1 = ALLPCS.getByorder(0).getSize();
MMY.huishou(start1,size1);
MMY.fenpei(0,size1);
ALLPCS.getByorder(0).setStart(0);
}
for (int i = 1; i < ALLPCS.size(); i++) { //搬家过程之整理process与Memory
if (ALLPCS.getByorder(i - 1).getEnd() + 1 !=
ALLPCS.getByorder(i - 1).getStart()) {
start = ALLPCS.getByorder(i).getStart();
size = ALLPCS.getByorder(i).getSize();
MMY.huishou(start, size);
MMY.fenpei(ALLPCS.getByorder(i - 1).getEnd() + 1, size);
ALLPCS.getByorder(i).setStart(ALLPCS.getByorder(i - 1).
getEnd() + 1);
}
}
FRTB.clear();
FRTB.add(new TableItem(MMY.point(), MMY.getFree()));
num = FRTB.getBySize(SIZE);
if (num != -1) {
if (FRTB.getItem(num).getSize() >SIZE) { //>
P = new process(Name, SIZE);
P.setStart(FRTB.getItem(num).getStart());
FRTB.getItem(num).setSize(FRTB.getItem(num).getSize() - SIZE);
FRTB.getItem(num).setStart(FRTB.getItem(num).getStart() + SIZE);
MMY.fenpei(FRTB.getItem(num).getStart(),SIZE);
ALLPCS.add(P);
System.out.println(FRTB.size());
return true;
} else if (FRTB.getItem(num).getSize() == SIZE) { //=.
P = new process(Name, SIZE);
P.setStart(FRTB.getItem(num).getStart());
MMY.fenpei(FRTB.getItem(num).getStart(),SIZE);
FRTB.removeByStart(FRTB.getItem(num));
ALLPCS.add(P);
System.out.println(FRTB.size());
return true;
}
}
return true;
} else { //报告无法分配
return false;
}
return false;
}
public void huishou(process p) { //撤销进程时回收内存,更改空闲分区表.
FreeTable tempF = FreeTable.getInstence(); //空闲表对象引用
AllProcess tempP = AllProcess.getInstence(); //进程表对象引用
tempP.removeByStart(p.getStart()); //进程表中删除进程
TableItem top=tempF.getByEnd(p.getStart()-1),
bottom= tempF.getByStart(p.getEnd()+1);
if (top != null && bottom != null) { //相邻的上下区均为空闲分区,合并3个区域
System.out.println("qing 1");
int size = top.getSize() + p.getSize() + bottom.getSize();
int start = top.getStart();
tempF.removeByStart(bottom);
tempF.removeByStart(top);
tempF.add(new TableItem(start, size));
MMY.huishou(start,size);
} else if (top != null && bottom == null) { //仅相邻的上区为空闲分区,合并上中2个区域
System.out.println("qing 2");
int size = top.getSize() + p.getSize();
int start = top.getStart();
tempF.removeByStart(top);
tempF.add(new TableItem(start, size));
MMY.huishou(start,size);
} else if (top == null && bottom != null) { //仅相邻的下区为空闲分区,合并中下两个区域
System.out.println("qing 3");
int size = p.getSize() + bottom.getSize();
int start = p.getStart();
tempF.removeByStart(bottom);
tempF.add(new TableItem(start, size));
MMY.huishou(start,size);
} else { //相邻的上下区均不为空闲分区
System.out.println("qing 4");
tempF.add(new TableItem(p.getStart(), p.getSize()));
MMY.huishou(p.getStart(), p.getSize());
}
}
public void delProcess(){//撤销进程,根据鼠标所在有的位置
}
public void delAllProcess(){//撤销所有进程
ALLPCS.clear();
FRTB.clear();
MMY.clear();
FRTB.add(new TableItem(0,MMY.getFree()));
}
public void restart(){//重置
}
public int getProcessNumber(){return ALLPCS.size();}
public process get(int i){return ALLPCS.getByorder(i);}
public int getFreeNumber(){return FRTB.size();}
public TableItem getFreeByOrder(int i){return FRTB.getItem(i);}
public int[] getMemory(){return MMY.getMmy();}
public int getMemoryItem(int i){return MMY.get(i);}
public int getAllFree(){return MMY.getFree();}
}