时间片轮转法:
PCB.ava
public class PCB {
private String name;
private int pri;
private int runTime;// 已运行的时间
private int needTime;
private String state;// R表示运行 ,F表示完成,W表示等待
private int counter;// 计数器
public PCB(String name, int counter, int runTime, int needTime, String state) {
this.name = name;
this.counter = counter;
this.runTime = runTime;
this.needTime = needTime;
this.state = state;
}
public void info() {/*
System.out.println("进程名 " + " " + "runTime" + " " + "needTime" + " "
+ "计数器" + " " + " " + "状态 ");*/
System.out.println(this.name + " " + this.runTime + " " + this.needTime
+ " " + this.counter + " " + this.state);
}
public void run() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPri() {
return pri;
}
public void setPri(int pri) {
this.pri = pri;
}
public int getNeedTime() {
return needTime;
}
public void setNeedTime(int needTime) {
this.needTime = needTime;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getRunTime() {
return runTime;
}
public void setRunTime(int runTime) {
this.runTime = runTime;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
}
TimePieces.java
import java.util.LinkedList;
public class TimePieces {
static LinkedList<PCB> readyPcbs = new LinkedList<PCB>();
static LinkedList<PCB> finishPcbs = new LinkedList<PCB>();
static int pcbcount = (int) (Math.random() * 10);
static int cuptime = (int) (Math.random() * 5 + 1);
public static void main(String[] args) {
creatPCBs();
System.out.println("时间片是:" + cuptime);
System.out.println("一共产生了" + pcbcount + "进程");
showList(readyPcbs);
runPCBs();
}
public static void creatPCBs() {
for (int i = 0; i < pcbcount; i++) {
int needTime = (int) (Math.random() * 10 + 1);
String name = i + "号";
PCB pcb = new PCB(name, 0, 0, needTime, "W");
readyPcbs.add(pcb);
}
}
public static void runPCBs() {
while (!readyPcbs.isEmpty()) {
readyPcbs.get(0).setState("R");
int runTime = readyPcbs.get(0).getRunTime();
int needTime = readyPcbs.get(0).getNeedTime();
int counter = readyPcbs.get(0).getCounter();
readyPcbs.get(0).setRunTime(runTime + 1);
readyPcbs.get(0).setNeedTime(needTime - 1);
readyPcbs.get(0).setCounter(counter + 1);
System.out.println("就绪队列:");
showList(readyPcbs);
/* 运行完成将其加入完成队列从,并从绪队列中移除 */
if (readyPcbs.get(0).getNeedTime() == 0) {
finishPcbs.add(readyPcbs.get(0));
readyPcbs.get(0).setState("F");
readyPcbs.remove(0);
} else if (readyPcbs.get(0).getCounter() == cuptime) {
PCB pCB = readyPcbs.get(0);
pCB.setCounter(0);
pCB.setState("W");
readyPcbs.addLast(pCB);
readyPcbs.remove(0);
}
System.out.println("已完成队列:");
showList(finishPcbs);
}
}
public static void showList(LinkedList<PCB> pcbs) {
System.out.println("进程名 " + " " + "runTime" + " " + "needTime" + " "
+ "计数器" + " " + " " + "状态 ");
for (int j = 0; j < pcbs.size(); j++) {
pcbs.get(j).info();
}
System.out.println(" ");
}
}
优先数法:优先数=50-运行时间
每运行一次优先数减3,重新竞争
PCB.java
public class PCB {
private String name;
private int pri;
private int runTime;// 已运行的时间
private int needTime;
private String state;// R表示运行 ,F表示完成,W表示等待
private int counter;// 计数器
public PCB(String name, int counter, int runTime, int needTime, int pri, String state) {
this.name = name;
this.counter = counter;
this.runTime = runTime;
this.needTime = needTime;
this.pri = pri;
this.state = state;
}
public void info() {/*
System.out.println("进程名 " + " " + "runTime" + " " + "needTime" + " "
+ "计数器" + " " + " " + "状态 ");*/
System.out.println(this.name + " " + this.runTime + " " + this.needTime
+ " " + this.counter + " "+this.pri+" "+ this.state);
}
public void run() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPri() {
return pri;
}
public void setPri(int pri) {
this.pri = pri;
}
public int getNeedTime() {
return needTime;
}
public void setNeedTime(int needTime) {
this.needTime = needTime;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getRunTime() {
return runTime;
}
public void setRunTime(int runTime) {
this.runTime = runTime;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
}
PriProcess.java
import java.util.LinkedList;
public class PriProcess {
static LinkedList<PCB> readyPcbs = new LinkedList<PCB>();
static LinkedList<PCB> finishPcbs = new LinkedList<PCB>();
static int pcbcount = (int) (Math.random() * 10);
public static void main(String[] args) {
System.out.println("一共产生了"+pcbcount+"个进程");
createPCBs();
showList(readyPcbs);
runPcbs();
}
public static void createPCBs() {
for (int i = 0; i < pcbcount; i++) {
int needTime = (int) (Math.random() * 10 + 1);
String name = i + "号";
PCB pcb = new PCB(name, 0, 0, needTime, 50-needTime, "W");
readyPcbs.add(pcb);
}
}
public static void runPcbs(){
while(!readyPcbs.isEmpty()){
System.out.println("就绪队列:");
sortList(readyPcbs);
int counter = readyPcbs.get(0).getCounter();
int pri = readyPcbs.get(0).getPri();
int needTime = readyPcbs.get(0).getNeedTime();
int runTime = readyPcbs.get(0).getRunTime();
readyPcbs.get(0).setState("R");
showList(readyPcbs);
readyPcbs.get(0).setCounter(counter+1);
readyPcbs.get(0).setNeedTime(needTime-1);
readyPcbs.get(0).setPri(pri-3);
readyPcbs.get(0).setRunTime(runTime+1);
/* 运行完成将其加入完成队列从,并从就绪队列中移除 */
if (readyPcbs.get(0).getNeedTime() == 0) {
readyPcbs.get(0).setState("F");
readyPcbs.get(0).setCounter(0);
finishPcbs.add(readyPcbs.get(0));
readyPcbs.remove(0);
}
sortList(readyPcbs);
System.out.println("已完成队列:");
showList(finishPcbs);
}
}
/*冒泡排序*/
public static void sortList(LinkedList<PCB> pcbs){
int i,j,k;
int lenth = pcbs.size();
for(i=0; i<lenth-1; i++){
k = i;
for(j=i+1; j<lenth; j++){
if(pcbs.get(j).getPri()>pcbs.get(k).getPri()){
PCB pcbj = pcbs.get(j);
PCB pcbk = pcbs.get(k);
pcbs.remove(k);
pcbs.add(k, pcbj);
pcbs.remove(j);
pcbs.add(j, pcbk);
}
}
}
}
public static void showList(LinkedList<PCB> pcbs) {
System.out.println("进程名 " + " " + "runTime" + " " + "needTime" + " "
+ "计数器" + " " +"优先数"+ " " + "状态 ");
for (int c = 0; c < pcbs.size(); c++) {
pcbs.get(c).info();
}
System.out.println(" ");
}
}