java按时间片轮转进程调度算法实现

时间:2021-01-27 19:51:12


public class OSProcess{
private String name; //进程名
private OSProcess next;//指向下一个进程
private int run_time; //要求运行时间
private int allo_time;//已运行时间
private int req_time;//还需要运行时间
private char state;//状态
private int pri;//优先数

public OSProcess(){}
//构造方法1  优先权方法
public OSProcess(String name, int run_time, int pri, char state) {

this.name = name;
this.run_time = run_time;
this.pri=pri;
this.state = state;
}

//构造方法2  时间片轮转方法
public OSProcess(String name, int run_time, char state, int allo_time) {
this.name = name;
this.run_time = run_time;
this.allo_time = allo_time;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public OSProcess getNext() {
return next;
}
public void setNext(OSProcess next) {
this.next = next;
}
public int getRun_time() {
return run_time;
}
public void setRun_time(int run_time) {
this.run_time = run_time;
}
public int getAllo_time() {
return allo_time;
}
public void setAllo_time(int allo_time) {
this.allo_time = allo_time;
}

public int getReq_time() {
return req_time;
}
public void setReq_time(int req_time) {
this.req_time = req_time;
}
public char getState() {
return state;
}
public void setState(char state) {
this.state = state;
}
public int getPri() {
return pri;
}
public void setPri(int pri) {
this.pri = pri;
}


}




public class OSTimeturn {
List<OSProcess> list = new ArrayList<OSProcess>();

OSProcess op1,op2,op3,op4,op5;
public void init(){
//初始化进程OSProcess(String name, int run_time, char state, int allo_time) 
op1 = new OSProcess("q1",2,'R',1);
op2 = new OSProcess("q2",3,'R',0);
op3 = new OSProcess("q3",1,'R',0);
op4 = new OSProcess("q4",2,'R',0);
op5 = new OSProcess("q5",4,'R',0);

op1.setNext(op2);
op2.setNext(op3);
op3.setNext(op4);
op4.setNext(op5);
op5.setNext(op1);


//添加到进程队列
OSProcess temp = op2;
while(!list.contains(temp)){
list.add(temp);
temp=temp.getNext();
}
}
public int getNext(OSProcess current,List<OSProcess> list){
OSProcess next = current.getNext();
int t=0;;
while(!list.contains(next)){
next=next.getNext();
t=list.indexOf(next);
}
return t;
}
public void run(int curPosition){
OSProcess current = list.get(curPosition);

current.setAllo_time(current.getAllo_time()+1);
System.out.println("当前:"+current.getName()+"进程执行");
OSProcess temp = list.remove(curPosition);//获得当前运行的进程
if(temp.getAllo_time()!=temp.getRun_time()) //判断已运行时间是否等于要求运行时间
list.add(temp);
else { //已运行时间==要求运行时间
temp.setState('E');
System.out.println("进程"+temp.getName()+"出队!");
}
if(list.size()>0){
System.out.print("此时list的值:");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i).getName()+"");
}
System.out.println();
}else{
System.out.println("进程队列为空!");
}
}

public void runProcess(){
OSProcess cur = list.get(0);
this.run(0);
while(list.size()>0){
int a = this.getNext(cur, list);
cur = this.list.get(a);
this.run(a);
}
}
public static void main(String[] args) {
OSTimeturn ost = new OSTimeturn();
ost.init();
ost.runProcess();
}
}