import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; class PCB { String name; int priority, needtime, cputime; PCB(String name,int priority,int needtime,int cputime){ this.name = name; this.priority = priority; this.needtime = needtime; this.cputime = cputime; } } public class Scheduling { private boolean cpu_state = true; //true 空闲 , false 繁忙 private int CPUTIME = 0; private LinkedList<PCB> initBox = new LinkedList<>();//记录初始进程 private LinkedList<PCB> readyBox = new LinkedList<>();//就绪队列 private LinkedList<PCB> cpuBox = new LinkedList<>();//cpu private LinkedList<PCB> completedBox = new LinkedList<>();//已完成队列 private HashMap<String, Integer> RunTime = new HashMap<>();//创建的进程完成时的CPUTIME private HashMap<String, Integer> WaitingTime = new HashMap<>();//创建的进程的空等待时间(RunTime - needtime) //创建一个进程 public boolean creat(int num) { if (num <= 0) return false; boolean num_state = true; while (num_state) { Scanner input = new Scanner(System.in); System.out.println("输入进程名(一个字符串),优先级(一个整数),所需时间(一个整数):"); String name = input.nextLine(); int priority = Integer.parseInt(input.nextLine()); int needtime = Integer.parseInt(input.nextLine()); readyBox.offer(new PCB(name, priority, needtime, 0)); initBox.offer(new PCB(name, priority, needtime, 0)); num--; if (num == 0) num_state = false; } System.out.println("NAME NEEDTIME PRIORITY"); for (int i = 0; i < readyBox.size(); i++) { System.out.println(readyBox.get(i).name + " " + " " + readyBox.get(i).needtime + " " + readyBox.get(i).priority); } while (readyBox.size() > 0) { dispatch(); System.out.println("CPUTIME:" + CPUTIME); System.out.println("NAME CPUTIME NEEDTIME PRIORITY STATE"); System.out.println(cpuBox.get(0).name + " " + cpuBox.get(0).cputime + " " + cpuBox.get(0).needtime + " " + cpuBox.get(0).priority + " working"); for (int i = 0; i < readyBox.size(); i++) { if (readyBox.get(i).cputime != 0 && readyBox.get(i).needtime != 0) { readyBox.get(i).cputime++; } System.out.println(readyBox.get(i).name + " " + readyBox.get(i).cputime + " " + readyBox.get(i).needtime + " " + readyBox.get(i).priority + " ready"); } if (completedBox.size() > 0) { for (int i = 0; i < completedBox.size(); i++) { // System.out.println(completedBox.get(i).name + " " + completedBox.get(i).cputime + " " + completedBox.get(i).needtime + " " + completedBox.get(i).priority + " finish"); } } timeout(); } System.out.println("CPUTIME:" + CPUTIME); System.out.println("NAME CPUTIME NEEDTIME PRIORITY STATE"); for (int i = 0; i < completedBox.size(); i++) { System.out.println(completedBox.get(i).name + " " + completedBox.get(i).cputime + " " + completedBox.get(i).needtime + " " + completedBox.get(i).priority + " finish"); } System.out.println("NAME RunTime WaitingTime"); //RunTime为进程运行完成时的CPUTIME for (int i = 0; i < initBox.size(); i++) { PCB t = initBox.get(i); System.out.println(t.name + " " + RunTime.get(t.name) + " " + WaitingTime.get(t.name)); } return true; } //执行 private boolean dispatch() { if (cpu_state) { if (!readyBox.isEmpty()) { sort(); PCB ready = readyBox.poll(); cpuBox.offer(new PCB(ready.name, ready.priority, ready.needtime, ready.cputime)); cpu_state = false; cpuBox.peek().priority--; cpuBox.peek().needtime--; cpuBox.peek().cputime++; CPUTIME++; return true; } } return false; } //就绪队列将优先级最高的替换到队首 private boolean sort() { PCB t = null; for (int j = 1; j < readyBox.size(); j++) { if (readyBox.get(0).priority < readyBox.get(j).priority) { t = readyBox.get(0); readyBox.set(0, readyBox.get(j)); readyBox.set(j, t); } } return true; } //时间片完 private boolean timeout() { if (!cpu_state) {//cpu繁忙 PCB cpu = cpuBox.poll(); if (cpu.needtime == 0) { completedBox.offer(new PCB(cpu.name, cpu.priority, cpu.needtime, cpu.cputime)); RunTime.put(cpu.name, CPUTIME - 1); int t = 0; for (int i = 0; i < initBox.size(); i++) { if (initBox.get(i).name.equals(cpu.name)) { t = initBox.get(i).needtime; } } WaitingTime.put(cpu.name, CPUTIME - 1 - t); } else { readyBox.offer(new PCB(cpu.name, cpu.priority, cpu.needtime, cpu.cputime)); } cpu_state = true; } return true; } public static void main(String[] args) { Scheduling demo = new Scheduling(); int num = 5;//创建的进程数 demo.creat(num); } }
计算机操作系统实验1
最新推荐文章于 2024-06-20 20:20:21 发布