时间片轮转:每个进程至多持续运行一定时间。
抢占:进程进入就绪队列,进行优先级比较,优先级大的可抢占正在运行的进程。
动态优先级:进程进入就绪队列一定时间未获取cpu,则提升优先级。
进程类
package process;
/**
* 进程类
*/
public class Process implements Comparable<Process>{
// 进程标识管理器
private static int processIdManage = 1;
// 进程大小
private int size;
// 进程控制块
private Pcb pcb;
// 进程优先级更新时间条件
private long priorityUpdateCondition;
public Process(int size){
this.size = size;
this.pcb = new Pcb();
}
public int getSize() {
return size;
}
public long getPriorityUpdateCondition() {
return priorityUpdateCondition;
}
public void setPriorityUpdateCondition(long priorityUpdateCondition) {
this.priorityUpdateCondition = priorityUpdateCondition;
}
@Override
public int compareTo(Process o) {
// 通过优先级实现进程比较
return -(this.getPcb().getPriority() - o.getPcb().getPriority());
}
/**
* 进程控制块类
*/
public static class Pcb{
// 进程标识
private int id;
// 进程状态
private String status;
// 进程优先级
private int priority;
// 进程运行至结束需要的时间
private long needRunTime;
// 进程等待时间
private long waitRunTotalTime;
public Pcb(){
this.id = processIdManage++;
this.status = "创建";
this.priority = 0;
this.needRunTime = 0;
this.waitRunTotalTime = 0;
}
public int getId(){
return id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getPriority() {
return priority;
}
public long getNeedRunTime() {
return needRunTime;
}
public void setNeedRunTime(long needRunTime) {
this.needRunTime = needRunTime;
}
public void setPriority(int priority){
this.priority = priority;
}
public long getWaitRunTotalTime() {
return waitRunTotalTime;
}
public void setWaitRunTotalTime(long waitRunTotalTime) {
this.waitRunTotalTime = waitRunTotalTime;
}
}
public Pcb getPcb() {
return pcb;
}
}
调度类
package process;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 进程调度类
*/
public class ProcessDispatch {
// 就绪队列
private final static List<Process> PROCESS_QUEUE = new ArrayList<Process>();
// 优先级更新条件:50秒不被调度则更新
private static long condition = 50;
/**
* 添加进程进入就绪队列,更改进程状态为:就绪
* @param process 进程
* @return boolean 添加成功返回true,否则false
*/
public synchronized static boolean add(Process process){
if(process == null)
return false;
process.getPcb().setStatus("就绪");
PROCESS_QUEUE.add(process);
showStatus(process);
// 更新队列顺序
Collections.sort(PROCESS_QUEUE);
return true;
}
/**
* 抢占cpu,更改进程状态为:运行
* @return Process 抢占的进程
*/
private synchronized static Process removeTop(){
if(PROCESS_QUEUE.isEmpty()){
return null;
}
Process process = PROCESS_QUEUE.remove(0);
process.getPcb().setStatus("运行");
process.setPriorityUpdateCondition(0);
showStatus(process);
return process;
}
/**
* 显示进程状态
* @param process 进程
*/
private synchronized static void showStatus(Process process){
if(process == null)
return;
Process.Pcb pcb = process.getPcb();
System.out.printf("进程Id:%5d 进程优先级:%5d 进程状态:%6s 剩余需运行时间:%5d 等待时间:%5d\n",pcb.getId(),pcb.getPriority()
,pcb.getStatus(),pcb.getNeedRunTime(),pcb.getWaitRunTotalTime());
}
/**
* 获得就绪队列第一个进程
* @return Process 进程
*/
private synchronized static Process getTop(){
if(PROCESS_QUEUE.isEmpty()){
return null;
}
return PROCESS_QUEUE.get(0);
}
/**
* 更新进程等待时间,并判断是否更新优先级
*/
private synchronized static void addWaitTimeAndUpdatePriority(){
boolean isUpdate = false;
for(Process p:PROCESS_QUEUE){
// 更新进程优先级更新时间条件
p.setPriorityUpdateCondition(p.getPriorityUpdateCondition()+1);
Process.Pcb pcb = p.getPcb();
// 更新总的等待时间
pcb.setWaitRunTotalTime(pcb.getWaitRunTotalTime()+1);
// 增加进程优先级
if(p.getPriorityUpdateCondition() == condition){
isUpdate = true;
pcb.setPriority(pcb.getPriority()+1);
}
}
// 优先级更新,进行重新排序队列
if(isUpdate)
Collections.sort(PROCESS_QUEUE);
}
/**
* 时间片管理类
*/
protected static class TimeSliceManage implements Runnable{
// 时间片大小为5
private static long size = 5-1;
// 开始时间
private static long start;
// 当前时间
private static long stop;
// cpu进程
private static Process cpuProcess;
public void setSize(long size){
TimeSliceManage.size = size;
}
@Override
public void run(){
start = stop = 0;
while(true){
stop++;
// 如果没有进程运行
if(cpuProcess == null){
start = stop = 0;
cpuProcess = removeTop();
continue;
}
long intervalSize = stop - start;
Process.Pcb pcb = cpuProcess.getPcb();
// 时间片用完
if(intervalSize == size){
// 进程未运行结束
if(pcb.getNeedRunTime() > 1){
// 更新运行时间
pcb.setNeedRunTime(pcb.getNeedRunTime()-1);
showStatus(cpuProcess);
// 更换时间片,将cpu进程添加到就绪队列
add(cpuProcess);
// 就绪进程抢占cpu
cpuProcess = removeTop();
// 进程运行结束
}else{
pcb.setStatus("结束");
pcb.setNeedRunTime(0);
showStatus(cpuProcess);
// 更换时间片
cpuProcess = removeTop();
}
start = stop = 0;
// 时间片未用完
}else{
// 进程未运行结束
if(pcb.getNeedRunTime() > 1){
// 更新运行时间
pcb.setNeedRunTime(pcb.getNeedRunTime()-1);
showStatus(cpuProcess);
// 就绪队列最高优先级进程 可抢占 当前cpu进程
Process topProcess = getTop();
if(topProcess != null && topProcess.getPcb().getPriority() > pcb.getPriority()){
// 更换时间片,将cpu进程添加到就绪队列
add(cpuProcess);
// 就绪进程抢占cpu
cpuProcess = removeTop();
start = stop = 0;
}
// 进程运行结束
}else{
pcb.setStatus("结束");
pcb.setNeedRunTime(0);
showStatus(cpuProcess);
// 更换时间片
cpuProcess = removeTop();
start = stop = 0;
}
}
// 就绪队列所有进程等待时间+1
addWaitTimeAndUpdatePriority();
}
}
}
// 时间片线程对象
private static Thread thread = new Thread(new TimeSliceManage());
// 启动时间片
public static void startTimeSlice(){
thread.start();
}
// 停止时间片
public static void stopTimeSlice(){
thread.hashCode();
}
}
Test类
package process;
public class Test {
public static void main(String[] args) {
ProcessDispatch.startTimeSlice();
Process p1 = new Process(2000);
p1.getPcb().setNeedRunTime(20);
Process p2 = new Process(150);
p2.getPcb().setNeedRunTime(10);
p2.getPcb().setPriority(1);
Process p3 = new Process(300);
p3.getPcb().setNeedRunTime(30);
p3.getPcb().setPriority(1);
Process p4 = new Process(1000);
p4.getPcb().setNeedRunTime(20);
p4.getPcb().setPriority(1);
ProcessDispatch.add(p1);
ProcessDispatch.add(p2);
ProcessDispatch.add(p3);
ProcessDispatch.add(p4);
while (true);
}
}