📄 processmanager.java
字号:
/* * ProcessManager.java * * Created on 2006年3月18日, 上午9:06 * *ProcessModule Module the control process *and communicate with CPU */package os.process;/** * * @author Vernkin */import os.cpu.*;import os.iodevice.IOGate;import os.memory.MainMemory;import java.util.*;/** * Module the control process * Aslpo communicate with CPU and IODevice */public class ProcessManager implements Runnable{ /** * Short Queue */ private Heaps shortQ = new Heaps("Short Queue", 16, false); /** * Long Queue */ private java.util.LinkedList longQ = new java.util.LinkedList(); /** *middle Queue has higher priority than long Queue */ private java.util.LinkedList middleQ = new java.util.LinkedList(); /** * Printer Queue */ private Heaps printerQ = new Heaps("Printer Queue", 16,true); /** * Console Queue */ private Heaps consoleQ = new Heaps("Console Queue", 16,true); /** * Printer IOGate */ private IOGate printer; /** * Console IOGate */ private IOGate console; /** * The only object of ProcessManager */ private static ProcessManager pm = new ProcessManager(); /** * The only Object of class Interrupt */ Interrupt itp; /** * MainMemory Module */ MainMemory mm = MainMemory.getInstance(); //method run() temp value MyProcess p; int data,num; /** Creates a new instance of ProcessManager */ private ProcessManager() { itp = Interrupt.getInstance(); printer = IOGate.getInstance("Printer"); console = IOGate.getInstance("Console"); } /** * Get the only object of ProcessManager * @return the only object of ProcessManager */ public static ProcessManager getInstance(){ return pm; } /** * Implementation of interface Runnable */ public void run(){ while(true) { if(itp.isCPURequest()){ dealtWithInterrupt(itp.managerGetProcess(), itp.getInterruptNum()); rest(); } checkForIOGate(printer); checkForIOGate(console); /**execute printer queue**/ if(printer.managerReadyToUse() && printerQ.getFirstProcess() != null){ p = printerQ.getFirstProcess(); data = mm.getContextValueAt(p, 7); num = mm.getContextValueAt(p, 11); printer.managerOutput(p, data,num); } /**execute console queue**/ if(console.managerReadyToUse() && consoleQ.getFirstProcess() != null){ p = consoleQ.getFirstProcess(); data = mm.getContextValueAt(p, 7); num = mm.getContextValueAt(p, 11); System.out.println("Console opetate in PM "+p+" "+data+" "+num); console.managerOutput(p,data,num); } adjustment(); itp.rest(500); } } private void rest(){ try{ Thread.sleep(100); }catch(InterruptedException e){ System.out.println(e.getMessage()); } } /** * interact with cpu(by Interrupt) * @param p source Process * @param interrupt_num interrupt number */ private void dealtWithInterrupt(MyProcess p,int interrupt_num){ setNextProcess(); if(interrupt_num == Interrupt.HALT_INTERRUPT && p != null){ p.setStatus("Dead"); /**process halt , release allocated **/ mm.releaseMemory(p); } else if(interrupt_num == Interrupt.CPUFREE_INTERRUPT){ if(shortQ.getFirstProcess() == null) itp.rest(100); else setNextProcess(); } else if(interrupt_num == Interrupt.TIMEOUT_INTERRUPT){ middleQ.add(p); } else if(interrupt_num == Interrupt.PrintOut_INTERRUPT){ System.out.println("Add "+p+" into printer queue"); addIntoQueue(p,printerQ); checkForIOGate(printer); } else if(interrupt_num == Interrupt.ConsoleOut_INTERRUPT || interrupt_num == Interrupt.ConsoleIn_INTERRUPT){ System.out.println("Add "+p+" into console queue"); addIntoQueue(p,consoleQ); checkForIOGate(console); } } /** * Check for Certain IOGate and Heaps * @param ig associate IOGate */ private void checkForIOGate(IOGate ig){ if(ig.managerRemainWork()) dealtRemainWork(ig); } /** * Pass new Process to CPU */ public void setNextProcess(){ if(itp.managerShouldToSet() && shortQ.getFirstProcess() != null){ itp.managerSetMessage(shortQ.findMax()); }else{ /**ProcessManager can not have availble process at that time**/ itp.managerNotAvailbleProcess(); } } /** * Add A Process to a Heaps(q); * @param p * @param q */ private void addIntoQueue(MyProcess p,Heaps q){ q.add(p); } /** * If the IOGate doesn't finish the former wrok * Response first * @param g The associate IOGate */ private void dealtRemainWork(IOGate g){ if(g.getIOGateName().equalsIgnoreCase("Printer")){ addIntoShortQueue(printerQ.findMax()); g.managerResponseOK(); } else if(g.getIOGateName().equalsIgnoreCase("Console")){ MyProcess tmp = g.getProcess(); int inter = mm.getContextValueAt(tmp, 11);//interrupt number if(inter == Interrupt.ConsoleOut_INTERRUPT){ addIntoShortQueue(consoleQ.findMax()); g.managerResponseOK(); }else if(inter == Interrupt.ConsoleIn_INTERRUPT){ mm.setContextValueAt(tmp, 7, g.getData());//change R7 addIntoShortQueue(consoleQ.findMax()); g.managerResponseOK(); } } } /**don't consider the process that didn't load in the main memory**/ public void arrageNewProcess(MyProcess p){ p.setStatus("Wait"); longQ.add(p); } /**Adjustment Algorithm**/ private void adjustment(){ if(shortQ.getMaxSize() > shortQ.getSize()-1){ //middle Queue has higher priority than long Queue if(!middleQ.isEmpty() && middleQ.getFirst() != null) shortQ.add((MyProcess)middleQ.removeFirst()); else if(!longQ.isEmpty() && longQ.getFirst() != null) shortQ.add((MyProcess)longQ.removeFirst()); } } private void addIntoShortQueue(MyProcess p){ //If short Queue is full, store in middle queue first; if(shortQ.isFull()){ p.setStatus("MiddleQueue"); middleQ.addLast(p); } else shortQ.add(p); } public static MyProcess[] getLinkedList(LinkedList in){ if(in.isEmpty()) return null; MyProcess[] ret = new MyProcess[in.size()]; for(int i=0;i<ret.length;i++) ret[i] = (MyProcess)in.get(i); return ret; } public MyProcess[] getShortQueue(){ return shortQ.getProcessGroup(); } public MyProcess[] getPrinterQueue(){ return printerQ.getProcessGroup(); } public MyProcess[] getConsoleQueue(){ return consoleQ.getProcessGroup(); } public MyProcess[] getMiddleQueue(){ return getLinkedList(middleQ); } public MyProcess[] getLongQueue(){ return getLinkedList(longQ); } public MyProcess[] getMainMemoryProcess(){ return mm.getMainMemoryProcess(); } public int getTotalMemory(){ return mm.getTotalMemory(); } public int getUsedMemory(){ return mm.getUsedMemory(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -