import java.io.*; class InOrderScheduler extends HeuristicScheduler { public InOrderScheduler( Process[] processes, int availMem ) { super( processes, availMem ); _currentProc = 0; } public void runAndReport( ) { int prevProc = -1, prevPrevProc = -1; // Roll in the first process. System.out.println("Rolling in P" + _processes[ _currentProc ].getID() + " for " + _processes[ _currentProc ].riTime() ); for( int loop = 0; loop < _processes[ _currentProc ].riTime(); loop++ ) { _processes[ _currentProc ].rollIn(); _clock++; } System.out.println("Adding " + _processes[ _currentProc ].riTime() + " to clock: clock = " + _clock); // Run through the processes over and over, giving each its share. while( !finished( ) ) { // Roll out the previous process. if( prevProc != -1 ) { System.out.println("Rolling out P" + _processes[ prevProc ].getID() + " for " + _processes[ prevProc ].roTime() ); for( int loop = 0; loop < _processes[ prevProc ].roTime(); loop++ ) { _processes[ prevProc ].rollOut(); } } // Run the current process. System.out.println("Running P" + _processes[ _currentProc ].getID() + " for " + _processes[ _currentProc ].getShare() ); for( int loop = 0; loop < _processes[ _currentProc ].getShare(); loop++ ) { _processes[ _currentProc ].giveQuanta(); _cpuCount++; } // Make _currentProc the next process. prevPrevProc = prevProc; prevProc = _currentProc; _currentProc++; if( _currentProc >= _pCount ) { _currentProc = 0; } // Roll in the next process. System.out.println("Rolling in P" + _processes[ _currentProc ].getID() + " for " + _processes[ _currentProc ].riTime() ); for( int loop = 0; loop < _processes[ _currentProc ].riTime(); loop++ ) { _processes[ _currentProc ].rollIn(); } // Advance the clock the appropriate number of steps. int roTime = 0, riTime = 0, runTime = 0; if ( prevPrevProc != -1 ) { roTime = _processes[ prevPrevProc ].roTime(); } if ( prevProc != -1 ) { runTime = _processes[ prevProc ].getShare(); } riTime = _processes[ _currentProc ].riTime(); _clock += Math.max( runTime, roTime + riTime ); System.out.println("Adding " + Math.max( runTime, roTime + riTime ) + " to clock: clock = " + _clock); } System.out.println("Finished!"); finalReport(); } public void runAndReport( int time ) { System.out.println("Report not yet implemented."); run( time ); } public void finalReport( ) { System.out.println("Number of processes: " + _pCount ); for( int loop = 0; loop < _pCount; loop++ ){ _processes[loop].report(); } } public void run( int time ) { System.out.println("Partial run not yet implemented."); } public void run( ) { int prevProc = -1, prevPrevProc = -1; // Roll in the first process. for( int loop = 0; loop < _processes[ _currentProc ].riTime(); loop++ ) { _processes[ _currentProc ].rollIn(); _clock++; } // Run through the processes over and over, giving each its share. while( !finished( ) ) { // Roll out the previous process. if( prevProc != -1 ) { for( int loop = 0; loop < _processes[ prevProc ].roTime(); loop++ ) { _processes[ prevProc ].rollOut(); } } // Run the current process. for( int loop = 0; loop < _processes[ _currentProc ].getShare(); loop++ ) { _processes[ _currentProc ].giveQuanta(); _cpuCount++; } // Make _currentProc the next process. prevPrevProc = prevProc; prevProc = _currentProc; _currentProc++; if( _currentProc >= _pCount ) { _currentProc = 0; } // Roll in the next process. for( int loop = 0; loop < _processes[ _currentProc ].riTime(); loop++ ) { _processes[ _currentProc ].rollIn(); } // Advance the clock the appropriate number of steps. int roTime = 0, riTime = 0, runTime = 0; if ( prevPrevProc != -1 ) { roTime = _processes[ prevPrevProc ].roTime(); } if ( prevProc != -1 ) { runTime = _processes[ prevProc ].getShare(); } riTime = _processes[ _currentProc ].riTime(); _clock += Math.max( runTime, roTime + riTime ); } } public void run( String name ) throws IOException { // Create names for the files to be generated String cpuName, shareName, integralName; StringBuffer buffer = new StringBuffer(); buffer.append( "../data/" ).append( name ) .append( "-" ).append( getType() ).append( "cpu.dat" ); cpuName = buffer.toString(); buffer = buffer.delete( cpuName.lastIndexOf("c"),buffer.length() ); buffer.append( "share.dat" ); shareName = buffer.toString(); buffer = buffer.delete( shareName.lastIndexOf("s"),buffer.length() ); buffer.append( "integral.dat" ); integralName = buffer.toString(); // Create PrintStreams to output to the three files PrintStream cpuOut = new PrintStream ( new BufferedOutputStream ( new FileOutputStream ( cpuName ))); PrintStream shareOut = new PrintStream ( new BufferedOutputStream ( new FileOutputStream ( shareName ))); PrintStream integralOut = new PrintStream ( new BufferedOutputStream ( new FileOutputStream ( integralName ))); int prevProc = -1, prevPrevProc = -1; // Roll in the first process. for( int loop = 0; loop < _processes[ _currentProc ].riTime(); loop++ ) { _processes[ _currentProc ].rollIn(); _clock++; _idleTime++; cpusToFile( cpuOut ); sharesToFile( shareOut, integralOut ); } // Run through the processes over and over, giving each its share. while( !finished( ) ) { // Roll out the previous process. if( prevProc != -1 ) { for( int loop = 0; loop < _processes[ prevProc ].roTime(); loop++ ) { _processes[ prevProc ].rollOut(); } } // Run the current process. for( int loop = 0; loop < _processes[ _currentProc ].getShare(); loop++ ) { _processes[ _currentProc ].giveQuanta(); _cpuCount++; for( int ploop = 0; ploop < _pCount; ploop++ ){ if( ploop == _currentProc ) { _processes[ploop].updateShare( true ); } else { _processes[ploop].updateShare( false ); } } cpusToFile( cpuOut ); sharesToFile( shareOut, integralOut ); } // Make _currentProc the next process. prevPrevProc = prevProc; prevProc = _currentProc; _currentProc++; if( _currentProc >= _pCount ) { _currentProc = 0; } // Roll in the next process. for( int loop = 0; loop < _processes[ _currentProc ].riTime(); loop++ ) { _processes[ _currentProc ].rollIn(); } // Advance the clock the appropriate number of steps. int roTime = 0, riTime = 0, runTime = 0; if ( prevPrevProc != -1 ) { roTime = _processes[ prevPrevProc ].roTime(); } if ( prevProc != -1 ) { runTime = _processes[ prevProc ].getShare(); } riTime = _processes[ _currentProc ].riTime(); _clock += Math.max( runTime, roTime + riTime ); // Figure out whether the CPU sat idle during RI and RO; and if // so, update the data files accordingly int extraTime = ( roTime + riTime ) - runTime; if( extraTime > 0 ) { for( int loop = 0; loop < extraTime; loop++ ) { cpusToFile( cpuOut ); sharesToFile( shareOut, integralOut ); _idleTime++; } } } // Add the total run and idle times to the bottom of the share file shareOut.println( _clock ); shareOut.println( _idleTime ); cpuOut.close(); shareOut.close(); integralOut.close(); } public String getType() { return "IO"; } protected int _currentProc; }