package one;
import java.util.LinkedList;
import java.util.Queue;
class PCB {
String processName;
PCB next; //指针
int requiredTime;
int priority;
String status;
public PCB(String processName, int requiredTime, int priority) {
this.processName = processName; //进程名
this.requiredTime = requiredTime; //要求运行时间
this.priority = priority; //优先数
this.status = "R"; //状态
}
public void decreasePriority() {
this.priority--;
}
public void decreaseRequiredTime() {
this.requiredTime--;
}
public void setStatus(String status) {
this.status = status;
}
}
public class Scheduler {
public static void main(String[] args) {
PCB p1 = new PCB("P1", 4, 1);
PCB p2 = new PCB("P2", 2, 3);
PCB p3 = new PCB("P3", 6, 5);
PCB p4 = new PCB("P4", 3, 2);
PCB p5 = new PCB("P5", 5, 4);
Queue<PCB> readyQueue = new LinkedList<>();
readyQueue.add(p1);
readyQueue.add(p2); readyQueue.add(p3);
readyQueue.add(p4);
readyQueue.add(p5);
PCB currentProcess = readyQueue.peek();
while (!readyQueue.isEmpty()) {
currentProcess = readyQueue.poll();
currentProcess.decreasePriority();
currentProcess.decreaseRequiredTime();
System.out.println("Running process: " + currentProcess.processName);
System.out.println("Updated process control block:");
System.out.println("Process Name: " +currentProcess.processName);
System.out.println("Pointer: " + (currentProcess.next != null ? currentProcess.next.processName : "0"));
System.out.println("Required Time: " + currentProcess.requiredTime);
System.out.println("Priority: " + currentProcess.priority);
System.out.println("Status: " + currentProcess.status);
System.out.println();
if (currentProcess.requiredTime != 0) {
readyQueue.add(currentProcess); } else {
currentProcess.setStatus("E");
}
}
}
}
运行结果:
Running process: P1
Updated process control block:
Process Name: P1
Pointer: 0
Required Time: 3
Priority: 0
Status: R
Running process: P2
Updated process control block:
Process Name: P2
Pointer: 0
Required Time: 1
Priority: 2
Status: R
Running process: P3
Updated process control block:
Process Name: P3
Pointer: 0
Required Time: 5
Priority: 4
Status: R
Running process: P4
Updated process control block:
Process Name: P4
Pointer: 0
Required Time: 2
Priority: 1
Status: R
Running process: P5
Updated process control block:
Process Name: P5
Pointer: 0
Required Time: 4
Priority: 3
Status: R
Running process: P1
Updated process control block:
Process Name: P1
Pointer: 0
Required Time: 2
Priority: -1
Status: R
Running process: P2
Updated process control block:
Process Name: P2
Pointer: 0
Required Time: 0
Priority: 1
Status: R
Running process: P3
Updated process control block:
Process Name: P3
Pointer: 0
Required Time: 4
Priority: 3
Status: R
Running process: P4
Updated process control block:
Process Name: P4
Pointer: 0
Required Time: 1
Priority: 0
Status: R
Running process: P5
Updated process control block:
Process Name: P5
Pointer: 0
Required Time: 3
Priority: 2
Status: R
Running process: P1
Updated process control block:
Process Name: P1
Pointer: 0
Required Time: 1
Priority: -2
Status: R
Running process: P3
Updated process control block:
Process Name: P3
Pointer: 0
Required Time: 3
Priority: 2
Status: R
Running process: P4
Updated process control block:
Process Name: P4
Pointer: 0
Required Time: 0
Priority: -1
Status: R
Running process: P5
Updated process control block:
Process Name: P5
Pointer: 0
Required Time: 2
Priority: 1
Status: R
Running process: P1
Updated process control block:
Process Name: P1
Pointer: 0
Required Time: 0
Priority: -3
Status: R
Running process: P3
Updated process control block:
Process Name: P3
Pointer: 0
Required Time: 2
Priority: 1
Status: R
Running process: P5
Updated process control block:
Process Name: P5
Pointer: 0
Required Time: 1
Priority: 0
Status: R
Running process: P3
Updated process control block:
Process Name: P3
Pointer: 0
Required Time: 1
Priority: 0
Status: R
Running process: P5
Updated process control block:
Process Name: P5
Pointer: 0
Required Time: 0
Priority: -1
Status: R
Running process: P3
Updated process control block:
Process Name: P3
Pointer: 0
Required Time: 0
Priority: -1
Status: R