/** Version 2 * A Scheduler object keeps a set of processes which it schedules and runs. * It can run over a series of steps, stopping after each step to report * results so far and make changes to its processes, if desired. * * The Scheduler abstract class has two main abstract subclasses: * PreCompScheduler and HeuristicScheduler. Each subclass has several * non-abstract subclasses. */ import java.io.*; abstract class Scheduler { // Constructors ------------------------ public Scheduler( Process[] processes, int availMem ) { loadProcesses( processes ); _clock = 0; _stepClock = 0; _idleTime = 0; _availMem = availMem; } public Scheduler( ) { _processes = new Process[0]; _pCount = 0; _clock = 0; _stepClock = 0; _idleTime = 0; _availMem = 8; } // Public --------------------------------- // Methods for running processes and reporting results // Schedule and run P's for given time, then print results for this step public abstract void runAndReport( int time ); // Schedule and run P's until finished, then print results public abstract void runAndReport( ) throws IOException; // Schedule and run P's for given time public abstract void run( int time ); // Schedule and run P's until finished public abstract void run( ) throws IOException; // Print results from the sum of all steps public abstract void finalReport( ); // Write results to a series of data files public abstract void makeFiles( String procfile ) throws IOException; // Return the scheduler's type as a two-char abbreviation public abstract String getType( ); // Methods for dealing with the scheduler's progress // Whether or not all processes have been completed public boolean finished( ) { for (int loop = 0; loop < _pCount; loop++) { if ( _processes[loop].finished() == false) return false; } return true; } // Resets all record of the scheduler's previous runs public void reset() { for (int loop = 0; loop < _pCount; loop++) { _processes[loop].reset(); } _clock = 0; _stepClock = 0; _idleTime = 0; } // Methods for making changes to the scheduler's processes // Give the scheduler the given set of processes public void loadProcesses( Process[] processes ) { _processes = processes; _pCount = processes.length; } // Add process p to the scheduler's processes public void addProcess( Process p ) { if ( _processes.length <= _pCount + 1 ) { growProcesses(); } _processes[_pCount] = p; _pCount++; } // Remove the process with given id // Return -1 if process does not exist; else return 0 public int remProcess( int id ) { int slot = findProcess(id); if (slot == -1) return -1; _processes[ slot ] = _processes[ _pCount - 1 ]; _processes[ _pCount - 1 ] = null; _pCount--; return 0; } // Change the process with given id's working set // Return -1 if process does not exist; else return 0 public int editProcWS( int id, int ws ) { int slot = findProcess(id); if (slot == -1) return -1; _processes[ slot ].setWorkingSet( ws ); return 0; } // Change the process with given id's share // Return -1 if process does not exist; else return 0 public int editProcShare( int id, int share ) { int slot = findProcess(id); if (slot == -1) return -1; _processes[ slot ].setGoalShare( share ); return 0; } // Private -------------------------------- // The current set of processes protected Process[] _processes; // How many processes we currently have protected int _pCount; // How many quanta we've run so far, total protected int _clock; // How many quanta we've run so far during the current step protected int _stepClock; // How many quanta the CPU has sat idle for so far protected int _idleTime; // How much memory we have available in which to keep working sets protected int _availMem; // How many CPU quanta we've given out so far // Note: This should always be equal to _clock, since if the schedule // is computed correctly the CPU should have no idle time. The only // exception is when a process's data change midway through a schedule // run; this may make idle time necessary. protected int _cpuCount; // Doubles the size of the _processes array protected void growProcesses(){ Process[] bigger = new Process[ _processes.length * 2 ]; for (int loop = 0; loop < _pCount; loop++) { bigger[loop] = _processes[loop]; } _processes = bigger; } // Returns the slot in _processes that contains the process with given id // (or -1 if the process is not found) protected int findProcess( int id ){ for (int loop = 0; loop < _pCount; loop++) { if ( _processes[loop].getID() == id ) return loop; } return -1; } }