程序管理優先排程演算法(Java版)
阿新 • • 發佈:2018-12-17
package PCB_Final; import java.util.Scanner; class pcb_Structure { public String name; public int runTime; public int priority; public char state; } class Pcb_main { public int pcb_initialize() { int N; Scanner in = new Scanner(System.in); System.out.println("Tell me How many Pcb You guys want:"); N = in.nextInt(); return N; } public void pcb_Inter(pcb_Structure[] pcbs, int N) { Scanner in = new Scanner(System.in); System.out.println("Please input the PCB name , priority , runtime (1,2,10):"); System.out.println(); for (int i = 0; i < N; i++) { System.out.println("Please input the NUmber " + (i + 1) + "\t PCb"); pcbs[i].name = in.next(); pcbs[i].priority = in.nextInt(); pcbs[i].runTime = in.nextInt(); pcbs[i].state = 'R'; } in.close(); } public int pcb_Priority_confirm(pcb_Structure[] pcbs, int N) { int key_value = 0, key, min = 10; for (int i = 0; i < N; i++) { if (pcbs[i].state == 'r') return -1; else { if (pcbs[i].state == 'R' && pcbs[i].priority < min) { min = pcbs[i].priority; key_value = i; } } } key = key_value; if (pcbs[key].state == 'F') return -1; else return key; } public void pcb_print(pcb_Structure[] pcbs, int N) { System.out.println("\npcb_name priority runtime status"); System.out.println("*********************************************************"); for (int i = 0; i < N; i++) { System.out.println(pcbs[i].name + "\t\t\t" + pcbs[i].priority + "\t\t\t" + pcbs[i].runTime + "\t\t\t" + pcbs[i].state); } } public void pcb_run(pcb_Structure[] pcbs, int N) { int t = 0; for (int i = 0; i < N; i++) { t += pcbs[i].runTime; } pcb_print(pcbs, N); for (int i = 0; i < t; i++) { while (pcb_Priority_confirm(pcbs, N) != -1) { pcbs[pcb_Priority_confirm(pcbs, N)].state = 'r'; } for (int j = 0; j < N; j++) { if (pcbs[j].state == 'r') { pcbs[j].priority += 1; pcbs[j].runTime--; pcb_print(pcbs, N); System.out.println("Process:" + pcbs[j].name + "\tRuntime:" + pcbs[j].runTime + "\tPriority:" + pcbs[j].priority); } { if (pcbs[j].runTime == 0) pcbs[j].state = 'F'; else pcbs[j].state = 'R'; } } } pcb_print(pcbs, N); } public static void main(String[] args) { Pcb_main pcb_main = new Pcb_main(); int N = pcb_main.pcb_initialize(); pcb_Structure[] pcbs = new pcb_Structure[N]; for (int i = 0; i < N; i++) { pcbs[i] = new pcb_Structure(); } pcb_main.pcb_Inter(pcbs, N); pcb_main.pcb_run(pcbs, N); } }