/** * A Process object represents a single process. */ class Process implements Heapable { // Variables that describe the actual process protected int _id; // ID number protected int _workingSet; // Size of current working set protected int _goalShare; // What share of CPU quanta the process // should get protected double _actualShare; // What share of CPU quanta the process // has actually gotten - exponentially // averaged over time protected double _shareFrom1; // What share of CPU quanta the process // should get, on a scale of 0 to 1 protected int _runTime; // How long it takes the process to // complete // Variables for keeping track of the process's performance public boolean _active; // Whether the process is rolled in and // receiving quanta public int _counter; // A multi-purpose variable used by // several scheduling algorithms protected int _quantaReceived; // How many CPU quanta the process // has been given protected int[] _window; // Keeps track of the last group of CPU // quanta the process has received protected int _riReceived; // How many memory quanta the process // has rolled in for protected int _roReceived; // How many memory quanta the process // has rolled out for protected int _windowSize; // How large a window the process uses // to determine its actual share so far; // if _windowSize = -1, look over the // entire lifetime of the process protected static int _totalQuanta; // How many CPU quanta all processes // have been given so far protected static int _totalShares; // The sum of all processes' goal shares // Constructor public Process( int id, int workingSet, int goalShare, int runTime ) { _id = id; _workingSet = workingSet; _goalShare = goalShare; _runTime = runTime; _quantaReceived = 0; _windowSize = -1; _shareFrom1 = -1; _totalQuanta = 0; _totalShares += goalShare; _active = false; _counter = 0; _actualShare = 0; // _window = new int[ _windowSize ]; } // Methods for altering the Process's variables // Resets all records the Process has kept public void reset( ) { _quantaReceived = 0; _riReceived = 0; _roReceived = 0; _totalQuanta = 0; _counter = 0; _active = false; _actualShare = 0; // _window = new int[ _windowSize ]; } // Changes the Process's working set size public void setWorkingSet( int workingSet ) { _workingSet = workingSet; } // Changes the Process's share public void setGoalShare( int goalShare ) { _goalShare = goalShare; } // Sets the Process's share on a scale from 0 to 1 public void setShareFrom1( double shareFrom1 ) { _shareFrom1 = shareFrom1; } // Methods for running and rolling in/out the Process // Run the Process for one CPU quanta // (The Process must be fully rolled in) public void giveQuanta( ) { // Note: if this.finished(), then this quanta is useless assert(allIn()); _quantaReceived++; _totalQuanta++; } // Roll in the Process for one unit of time public void rollIn( ) { _roReceived = 0; _riReceived++; } // Roll out the Process for one unit of time public void rollOut( ) { _riReceived = 0; _roReceived++; } // Update the Process's actual share using exponential averaging. // If running = true, the process has run for one time unit; if false, // the process has sat idle for one time unit. public void updateShare( boolean running ) { if( running ) { _actualShare = ( _actualShare * ( 1 - 0.004 ) ) + 0.004; } else { _actualShare = _actualShare * ( 1 - 0.004 ); } } // Whether or not the Process is fully rolled in public boolean allIn( ) { if ( _riReceived >= riTime() ) return true; return false; } // Whether or not the Process is fully rolled out public boolean allOut( ) { if ( _roReceived >= roTime() ) return true; return false; } // Whether or not the Process is finished running public boolean finished( ) { if ( _quantaReceived >= _runTime ) { return true; } return false; } // How many memory quanta needed to roll in the Process. // This method can be expanded later. public int riTime( ) { return _workingSet; } // How many memory quanta needed to roll out the Process. // This method can be expanded later. public int roTime( ) { return _workingSet; } // Methods for implementing Heapable public boolean greaterThan( Object other) { Process otherP = (Process)other; return ( ( actualShare() / getShareFrom1() ) > ( ((Process)other).actualShare() / ((Process)other).getShareFrom1() ) ); } public boolean lessThan( Object other ) { return ( ( actualShare() / getShareFrom1() ) < ( ((Process)other).actualShare() / ((Process)other).getShareFrom1() ) ); } public boolean equalTo( Object other ) { return ( ( actualShare() / getShareFrom1() ) == ( ((Process)other).actualShare() / ((Process)other).getShareFrom1() ) ); } // Access methods public double actualShare() { return (double)_quantaReceived / _totalQuanta; } public double averagedShare() { return _actualShare; } public int getID() { return _id; } public int getWorkingSet() { return _workingSet; } public int getGoalShare() { return _goalShare; } public int getShare() { return _goalShare; } public double getShareFrom1() { _shareFrom1 = (double)_goalShare / _totalShares; return _shareFrom1; } public int getQuanta() { return _quantaReceived; } public int getRunTime() { return _runTime; } // Prints out the Process's data public void report() { System.out.println ("ID " + _id + ": Share " + _goalShare + ", CPU quanta " + _quantaReceived); } public String toString() { // return ("P" + _id + ": goalShare=" + getShareFrom1() + ", actualShare=" + actualShare() + ", quantaReceived=" + _quantaReceived); return ("P" + _id); } }