Rev 412 | Rev 414 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/******************************************************************************** File: Scheduler.java** Holds the Scheduler class, which has a generic scheduler class, from which* all schedulers should be derived.** Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)** Permission is hereby granted, free of charge, to any person obtaining a copy* of this software and associated documentation files (the "Software"), to deal* in the Software without restriction, including without limitation the rights* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell* copies of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in* all copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS* IN THE SOFTWARE.******************************************************************************/import java.util.Vector;abstract class Scheduler{protected Vector<Process> run_queue;protected Vector<LogEntry> log;protected int cur_time = 0;protected Process cur_proc;protected abstract boolean step ();/*** Add a new process to the run queue.** If you need to support time-delayed processes, then you must override* this method, and handle pulling them into the run_queue yourself.*/public void addProcess (Process proc, int add_time){run_queue.add (new Process(proc));}/*** Expire the currently running process.** This removes the current process from the cpu and re-adds it to the* run_queue.*/protected void expireCurrent (){assert (cur_proc.time_left > 0);cur_proc.finished_at = cur_time;run_queue.add (cur_proc);log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.EXPIRE));cur_proc = null;}protected void completeCurrent (){assert (cur_proc.time_left == 0);cur_proc.finished_at = cur_time;log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.COMPLETE));cur_proc = null;}protected void startProcess (Process proc){cur_proc = proc;cur_proc.started_at = cur_time;run_queue.remove (cur_proc);log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.START));}protected void scheduleCurrent (){cur_proc.time_left--;cur_time++;}public Scheduler (){this.run_queue = new Vector<Process> ();this.log = new Vector<LogEntry> ();}public void run (){/* Keep step()ing until we're done */while (step ()); // do nothingfor (LogEntry e : log)System.out.println (e);/* Print time taken */System.out.println ("\nSimulation took " + cur_time + " time units to complete!");}}/* vim: set ts=4 sts=4 sw=4 expandtab: */