java设计一个按时间片轮转法实现处理器调度的程序

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值