Menu

[Solved]Write Code Program Import Javaiofile Import Javaiofilenotfoundexception Import Javaioprint Q37059897

write the code in between the program import java.io.File;import java.io.FileNotFoundException; import java.io.PrintStream;import java.util.LinkedList; //KNOWN BUG: // sometimes a concurrentmodification exception is thrown public class Kernel { privatePrintStream log; //how we log messages. private final intCYCLE_LENGTH = 10; private static final boolean debug = false;private LinkedList readyQueue; //the ready queue. privateLinkedList waitQueue; //the wait queue for the keyboard. //NOTE:should be refactored; //would add a layer of complexity to labs torefactor now though… private LinkedList hardDriveWaitQueue; //thewait queue for the HD. private LinkedList interruptQueue; //a queuefor interrupts. private int nextPID; //the next pid (starts at 1).private PCB current; //the pcb of the process that’s currently onthe cpu. private int scheduler = RRP; //the scheduler (see OSParameters). //Hardware devices that could cause interrupts. //theyneed to be polled every so often to see if they have requested aninterrupt. private CPU cpu; private Clock clock; private Keyboardkeys; private HardDrive hardDrive; private static final int CPU_IRQ= 1; private static final int CLOCK_IRQ = 2; private static finalint KEYBOARD_IRQ = 3; private static final int HARDDRIVE_IRQ = 4;//OS Parameters //the alpha parameter for burst length prediction//private static final double ALPHA = 0.5; private static final intSJF = 1; private static final int RRP = 2; /** * Initializes thekernel. */ public Kernel() { readyQueue = new LinkedList();waitQueue = new LinkedList(); hardDriveWaitQueue = newLinkedList(); interruptQueue = new LinkedList(); nextPID = 1; cpu =new CPU(); clock = new Clock(); keys = new Keyboard(); hardDrive =new HardDrive(this); if(debug) { //sometimes we really do need tosee the output! log = System.out; } else { try { log = newPrintStream(new File(“kernel.txt”)); } catch (FileNotFoundExceptione) { log = System.out; } } } /** * Creates a process. * @parampriority * @param duration */ public void createProcess(intpriority, int duration, String burstPattern) { PCB p = newPCB(nextPID++, priority, duration, burstPattern); //create the pcb.p.set(p.tick(), p.priority(), State.READY); //set the state toready. readyQueue.add(p); //put it in the ready queue. } /** * Runsthe operating system. */ public void run() { boolean print = true;//determines if we print the time (for scheduler). //only print thetime when the last call to schedule set current to something //andnow current is null. (e.g., all processes waiting or complete).boolean checkPrint = true; //part of the mechanism above.keys.start(); hardDrive.start(); while(true) { if(checkPrint&& current == null && readyQueue.isEmpty()&& waitQueue.isEmpty() &&hardDriveWaitQueue.isEmpty()) { print = true; } if(print == false&& !readyQueue.isEmpty()) checkPrint = true; try {Thread.sleep(CYCLE_LENGTH); } catch(Exception e) {} // slow downthe emulation to a manageable rate. cycle(); //the basic thinghappening in here is handling a single cycle. if(print) { print =false; checkPrint = false; double cpuUtilization = 0; doublethroughput = 0; double waitTime = 0; System.out.println(“Noprocesses in OS. Current time is: ” + clock.time()); //TODO: Printout the throughput, CPU utilization, and waiting time. //throughput= # processes that have completed / total timeSystem.out.printf(“CPU Utilization: %.3fn”, cpuUtilization); //CPUutilization = time the CPU was not idle / total timeSystem.out.printf(“Throughput: %.3fn”, throughput); //waiting time= the amount of time a process spends on the waiting queue (thisshould be averaged). System.out.printf(“Waiting Time: %.3fn”,waitTime); //***IMPORTANT*** total time is the number of times thecycle method of this class is called. } } } /** * Does process(re)scheduling. */ public void schedule() { switch(scheduler) {case RRP: rrp_schedule(); break; case SJF: sjf_schedule(); break;default: kernelTrapError(“Unrecognized scheduler.”); }//sjf_schedule(); } /** * Does Shortest job first scheduling. */public void sjf_schedule() { } /** * Schedule using round robingwith priority sensitive quanta. */ public void rrp_schedule() {log.println(“SCHED: Before CS: ” + current + ” | ” + readyQueue + “KEYBOARD: (” + waitQueue + “) | HARDDRIVE: (” + hardDriveWaitQueue+ “)”); if(cpu.idle() && !readyQueue.isEmpty()) {//scheduling when cpu is idle current = readyQueue.remove();current.set(current.tick(), current.priority(), State.RUNNING);cpu.load(current); clock.setAlarm(clock.time() +current.priority()); } else if(!readyQueue.isEmpty()){ //otherwise,schedule the next process. if(current != null) {cpu.store(current); current.set(current.tick(), current.priority(),State.READY); readyQueue.add(current); } current =readyQueue.remove(); current.set(current.tick(),current.priority(), State.RUNNING); cpu.load(current);clock.setAlarm(clock.time() + current.priority()); }log.println(“SCHED: After CS: ” + current + ” | ” + readyQueue + “KEYBOARD: (” + waitQueue + “) | HARDDRIVE: (” + hardDriveWaitQueue+ “)”); } /** * Executes one cycle of the machine. */ public voidcycle() { cpu.cycle(); clock.tick(); pollForInterrupts();handleInterrupts(); } /** deals with all the interrupts currentlyon the interrupt queue*/ private void handleInterrupts() { while(!interruptQueue.isEmpty()) { Interrupt i = interruptQueue.remove();log.println(“INFO: ” + i.message); switch(i.irq) { case CPU_IRQ:cpu_interrupt(i); break; case CLOCK_IRQ: clock_interrupt(i); break;case KEYBOARD_IRQ: keyboard_interrupt(i); break; caseHARDDRIVE_IRQ: hardDrive_interrupt(i); break; default:kernelTrapError(“Unknown IRQ: ” + i.irq); } } } private voidhardDrive_interrupt(Interrupt i) { if(!hardDriveWaitQueue.isEmpty()) { PCB p =hardDriveWaitQueue.remove(); p.set(p.tick(), p.priority(),State.READY); readyQueue.add(p); } } private voidkeyboard_interrupt(Interrupt i) { if(! waitQueue.isEmpty()) { PCB p= waitQueue.remove(); p.set(p.tick(), p.priority(), State.READY);readyQueue.add(p); } } /** * handles fatal errors. */ private voidkernelTrapError(String message) { System.out.println(“ERROR: ” +message); System.out.println(“Shutting down…”);log.println(“ERROR: ” + message); log.println(“Shutting down…”);keys.stop(); //stop devices. hardDrive.stop(); System.exit(0); }/** * Handles interrupts from the clock. */ private voidclock_interrupt(Interrupt i) {if(i.message.get(“message”).equals(“alarm”)) { //the alarm is onlyused for rescheduling. schedule(); } } /** * Handles interruptsfrom the cpu. */ private void cpu_interrupt(Interrupt i) {if(i.message.get(“message”).equals(“reschedule”)) {if(i.message.get(“device”).equals(“cpu”)) { //the system is idle.schedule(); } } elseif(i.message.get(“message”).equals(“terminate”)) { int pid =Integer.parseInt(i.message.get(“pid”)); if(current != null&& current.pid() == pid) { current.set(current.tick(),current.priority(), State.TERMINATED); current = null; schedule();} else if(!readyQueue.isEmpty()){ //for whatever reason process tobe terminated is not currently running. for(int k = 0; k <readyQueue.size(); k++) { if(readyQueue.get(k).pid() == pid) { PCBp = readyQueue.remove(k); p.set(p.tick(), p.priority(),State.TERMINATED); } } } System.out.println(“Terminated ” + pid); }else if(i.message.get(“message”).equals(“read”)) {read(Integer.parseInt(i.message.get(“pid”))); } elseif(i.message.get(“message”).equals(“hdio”)) {hardDriveIO(Integer.parseInt(i.message.get(“pid”))); } } /** *Reads some information from the Hard drive. */ private voidhardDriveIO(int pid) { if(readyQueue.isEmpty() && current== null) { kernelTrapError(“Read with no processes on readyqueue.”); } else if(current != null && current.pid() ==pid) { cpu.store(current); current.set(current.tick(),current.priority(), State.WAITING);hardDriveWaitQueue.add(current); current = null; schedule(); } else{ for(int i = 0; i < readyQueue.size(); i++) {if(readyQueue.get(i).pid() == pid) { PCB p = readyQueue.remove(i);p.set(p.tick(), p.priority(), State.WAITING);hardDriveWaitQueue.add(p); } } } } /** * Returns true if there areprocesses waiting for input from the hard drive. */ public booleanprocessesWaitingForHD() { return !hardDriveWaitQueue.isEmpty(); }private void read(int pid) { if(current==null &&readyQueue.isEmpty()) { // trap error and shut down machine (noprocess could have initiated this call. kernelTrapError(“processshould not have been initiated”); } else { // else, if thecurrent’s pid is the pid of this method, if(current.pid()==pid) {cpu.store(current); // store cpu state into currentcurrent.set(current.tick(), current.priority(), State.WAITING);//set state of current to waiting waitQueue.add(current); // addcurrent to the wait queue current = null;// set current to nullschedule();// call schedule } else { // otherwise (the process thatmade the call is on the ready queue // use a for(int i…) loop tofind the process with the right pid for(int k = 0; k <readyQueue.size(); k++) { if(readyQueue.get(k).pid() == pid) { PCBp = readyQueue.remove(k);// remove the correct process from theready queue p.set(p.tick(), p.priority(), State.WAITING); // setits state to waiting waitQueue.add(p); // add it to the wait queue.} } } } } /** * Checks all hardware devices to see if an interruptis generated. * If it is, enqueue an interrupt on the interruptqueue. */ private void pollForInterrupts() {if(cpu.interruptRequested()) { //check the cpu for interrupts.interruptQueue.add(new Interrupt(CPU_IRQ, cpu.getMessage()));cpu.resetInterruptRequest(); } if(clock.interruptRequested()) {//check the clock for interrupts. interruptQueue.add(newInterrupt(CLOCK_IRQ, clock.getMessage()));clock.resetInterruptRequest(); } if(keys.interruptRequested()) {interruptQueue.add(new Interrupt(KEYBOARD_IRQ, keys.getMessage()));keys.resetInterruptRequest(); } if(hardDrive.interruptRequested()){ interruptQueue.add(new Interrupt(HARDDRIVE_IRQ,hardDrive.getMessage())); hardDrive.resetInterruptRequest(); } }}

Expert Answer


Answer to write the code in between the program import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStr… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *