偶尔勤奋的肥猫——OS时间片轮转调度基本算法

来源:互联网 发布:apache如何加载php 编辑:程序博客网 时间:2024/06/02 22:52


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;//优先数

//构造方法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 {
ArrayList list = new ArrayList<OSprocess>();
int timepiece=2;
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',0);
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 ;
temp= op1;
while(!list.contains(temp)){
list.add(temp);
temp=temp.getNext();
}
}
//更新进程队列


public int getNext(OSprocess current,ArrayList<OSprocess> list){
OSprocess next = current.getNext();
//int t=(OSprocess)list.getposition(next);//指定元素的位置
int t=0;
while(!list.contains(next)){
next=next.getNext();
t=list.indexOf(next);
}
return t;  
}

public void run(int curPosition){
OSprocess current = (OSprocess) list.get(curPosition);
if((current.getAllo_time()+timepiece)>current.getRun_time()){
current.setAllo_time(current.getRun_time());
}
else {
current.setAllo_time(current.getAllo_time()+timepiece);
}
System.out.println("当前:"+current.getName()+"进程执行");
    OSprocess temp = (OSprocess) 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(((OSprocess)list.get(i)).getName()+"");
}
System.out.println();
}else{
System.out.println("进程队列为空!");
}
}


public void runProcess(){
OSprocess cur = (OSprocess) list.get(0);
this.run(0);
while(list.size()>0){
int a = this.getNext(cur,list);
cur = (OSprocess) this.list.get(a);
this.run(a);
}
}

public static void main(String[] args) {
OSTimeturn ost = new OSTimeturn();
ost.init();
ost.runProcess();
}
}




0 0