我已经在这里查看了其他CPU调度程序,但似乎没有一个看起来像我现在正在使用的那个。
我获得了一个CPU调度程序并被告知写了一个" if语句"最短的完成时间(STCF或SRTF)。该程序有3个不同的文件。
代码就在下面,但如果您愿意,我还在评论中包含了一个指向Pastebin .java文件的链接。我们不允许在这里包含链接。
这是" Process.java "
/**
* Process class
*
* This class represents a process on our hypothetical system. Any information
* about a single process should be stored here.
*
*
*/
public class Process
{
// The name of the process. Can be set to something, but the current
// program assigns arbitrary names.
private String name;
// The starting time of the process - the time that the process arrives
// at the system to be run.
private int startTime;
// The ending time of the process - the time that the process completed
// running on the system.
private int endTime;
// The first run time of the process - the time that the process was
// first places on the CPU to be run.
private int firstRunTime;
// The amount of time left to complete the process - how much time until
// the process finished.
private int timeLeft;
// The length of the process - how much time the process originally
// required to complete.
private int length;
// A boolean indicating in the process is finished or not.
private boolean finished;
/**
* The constructor for the Process class.
*
* @param n The name of the process.
* @param s The start time of the process.
* @param len The length of the process.
*/
public Process( String n, int s, int len )
{
name = n;
startTime = s;
endTime = -1; // End time is calculated while running.
firstRunTime = -1; // First run time is calculated while running.
timeLeft = len; // Initial time left is length of process.
length = len;
finished = false;
}
/**
* This method is called when the time on the system advances one step.
* It should only be called on a process that is currently running on
* the CPU.
*
* @param time The current time in the system
*/
public void tick( int time )
{
// One second of running has occurred, so less time is left.
timeLeft--;
// If there is no time left on the process, then the process
// is finished, and the end time is the current time.
if ( timeLeft == 0 )
{
finished = true;
endTime = time;
}
}
// Sets the firstRunTime to the time passed as the parameter.
public void startRunning( int time )
{
// Only change it if it has not been changed before.
if ( firstRunTime == - 1 )
firstRunTime = time;
}
// Return the name of the process.
public String getName( )
{
return name;
}
// Return the time left of the process.
public int getTimeLeft( )
{
return timeLeft;
}
// Returns whether or not the process is finished.
public boolean isFinished( )
{
return finished;
}
// Returns the starting time of the process.
public int getStartTime( )
{
return startTime;
}
// Returns the end time of the process.
public int getEndTime( )
{
return endTime;
}
// Returns the first time that the process started running.
public int getFirstRunTime( )
{
return firstRunTime;
}
// Returns the original length of the process.
public int getLength( )
{
return length;
}
}
这是" CPU.java "我个人添加的唯一方法是" public int remainingTime()"
import java.util.ArrayList;
/**
* The CPU class.
* This class represents a CPU that is running on our system.
*
*
*/
public class CPU
{
// This process is the process that is currently running on the CPU.
private Process running;
// This variable is true if the CPU is empty (not running a process).
private boolean empty;
// This variable stores the names of the processes that were run on
// the CPU over the course of the simulation.
private ArrayList<String> chart;
/**
* The constructor for the CPU class. We assume that the CPU is not
* running anything initially.
*/
public CPU( )
{
running = null;
empty = true;
chart = new ArrayList<String>( );
}
/**
* This method is used to start a process running on the CPU. The process
* to start is called p, and the time it is started is time.
*
* @param p The process to start running on the system.
* @param time The current time.
*/
public void begin( Process p, int time )
{
// If the process p is null, then don't do anything.
if ( p != null )
{
// If the CPU is empty and there is time left to run on process p,
// then start p running.
if ( empty && ( p.getTimeLeft( ) > 0 ) )
{
running = p;
running.startRunning( time );
empty = false;
}
// Otherwise, print an appropriate error message.
else if ( !empty )
{
System.out.println( "CPU is currently running another process." );
}
else
{
System.out.println( "Process is already finished." );
}
}
}
/**
* The tick method advances the CPU one time tick. If the process that is
* currently running on the CPU finishes, it is returned from the method;
* otherwise it will return null.
*
* @param time
* @return The process that completed; null if no process completed
*/
public Process tick( int time )
{
// **Basically: return either process or nothing (if a process is running). Nothing on CPU, return NULL.
// If the CPU is currently running something, do this stuff.
if ( !empty )
{
// Advance the process running on the CPU one time tick.
running.tick( time );
// Add the name of the process running to the chart.
chart.add( running.getName( ) );
// If the process is finished now, remove it from the CPU and
// return it.
if ( running.isFinished( ) )
{
empty = true;
return running;
}
// If the process is not finished, return null.
else
{
return null;
}
}
// If the CPU is not running anything, it is empty, so add
// null to the chart, then return null.
chart.add( null );
return null;
}
/**
* Return the value of the empty variable.
* @return Whether or not the CPU is empty.
*/
public boolean isEmpty( )
{
return empty;
}
public int remainingTime()
{
return running.getTimeLeft();
}
/**
* This method prints the CPU usage chart, 20 time ticks per line. This
* code assumes the names of the processes are two characters - if they
* are not, the chart will not line up well.
*/
public void printChart( )
{
int i = 1;
for ( String s : chart )
{
// If no process was running, this will print XX on the chart.
if ( s != null )
System.out.print( s + " " );
else
System.out.print( "XX " );
// After 20 items, move to the next line.
if ( ( i % 20 ) == 0 )
System.out.println( );
i++;
}
System.out.println( );
}
}
这是&#34; Simulator.java &#34;,STCF应该去哪里。
import java.util.ArrayList;
import java.util.Random;
/**
* The Simulator class, which executes the simulation.
*
*
*/
public class Simulator
{
// cpu1 is the CPU of the system.
private CPU cpu1;
// ready stores the processes that are ready to run and are waiting for
// the CPU.
private ArrayList<Process> ready;
// finished stores the processes that have finished execution.
private ArrayList<Process> finished;
// time stores the current time of the system.
private int time;
// END_TIME indicates how long the simulation will run.
private final int END_TIME = 250;
// PROC_ARRIVE_PERCENT indicates the percent chance that a new process
// will arrive to run on the CPU at any given time unit.
private final int PROC_ARRIVE_PERCENT = 80;
// PROC_MIN_LENGTH is the minimum length that a process can be.
private final int PROC_MIN_LENGTH = 5;
// PROC_MAX_LENGTH is the maximum length that a process can be.
private final int PROC_MAX_LENGTH = 15;
// SCHEDULING_ALGORITHM indicates which algorithm the simulation
// will be using. Options are "FIFO", "SJF", "STCF", "RR", and
// "MLFQ".
private final String SCHEDULING_ALGORITHM = "STCF";
/**
* The constructor for the Simulator class. Everything starts as
* a new Object, and time starts at 0.
*/
public Simulator( )
{
cpu1 = new CPU( );
ready = new ArrayList<Process>( ); // processes ready to go
finished = new ArrayList<Process>( ); // once processes are finished, they go here
time = 0;
}
/**
* The main function runs the program. This just creates a Simulator
* Object, and calls its run method.
* @param args
*/
public static void main(String[] args)
{
// create simulator then run
Simulator s = new Simulator( );
s.run( );
}
/**
* The addProcess starts a new process at the current time.
* @param name - The name of the process to add.
* @param start - The start time for the process to add.
* @param length - The length of the process to add.
*/
public void addProcess( String name, int start, int length )
{
// take stuff, creates new process, add to ArrayList
Process temp = new Process( name, start, length );
ready.add( temp );
}
/**
* Select the next process to run on the CPU. The algorithm is determined
* by the value of the SCHEDULING_ALGORITHM variable.
* @param ready The set of processes that are ready to run.
* @return Returns the process selected to run.
*/
public Process select( ArrayList<Process> ready )
{
// TO-DO
// **This is the part I'm working on**
if (SCHEDULING_ALGORITHM.equals("STCF"))
{
int i = 0;
int shortestTime = 0;
int startTime = 0;
int timeLeft = 0;
Process proc = new Process(SCHEDULING_ALGORITHM, startTime, timeLeft);
CPU cpu = new CPU();
timeLeft = proc.getTimeLeft();
if (ready.isEmpty() == false)
{
// check the ready ArrayList for the next job with the shortest time left
// if the current process has the shortest time, it will automatically be selected again.
while (i <= ready.size())
{
if (proc.getTimeLeft() < cpu.remainingTime())
{
shortestTime = proc.getTimeLeft();
}
i++;
}
i = 0;
while (i <= ready.size())
{
if (proc.getTimeLeft() == shortestTime)
{
// remove running process from CPU. Store for later.
ready.remove(i);
}
i++;
}
}
}
return null;
}
/*
* remove first process from ready ArrayList
* run that process
* while running that process, check the ready ArrayList.
* if a process in the ready ArrayList is smaller than the remaining time of the one running,
* remove that process from the CPU. Save the remaining time left of that job.
* Put the new job on.
* When remaining time left on a job is 0, move to finished ArrayList.
*/
// run the first process for 2 seconds first
// Check ready ArrayList for shortest process.
// If shortest process is still the current array, run for 2 more seconds.
// Else if it is different, run that different process. Save spot of last process.
/**
* The run method runs the entire simulation. We assume that this
* method is run immediately after creating the Simulator object.
*/
public void run( )
{
// Various variables needed.
Process temp;
Random r = new Random( ); // processes arriving randomly
int procTime;
char ch1 = 'A', ch2 = 'A';
char[] n;
String s;
System.out.println( "Beginning simulation." );
// Loop until the time gets to the end.
while ( time < END_TIME )
{
// See if a new process arrives.
if ( r.nextInt( 100 ) < PROC_ARRIVE_PERCENT )
{
System.out.println( "New process arrived at time = " + time );
// Create a new name by incrementing the characters.
n = new char[2];
n[0] = ch1;
n[1] = ch2;
s = new String( n );
if ( ch2 == 'Z' )
{
ch1++;
ch2 = 'A';
}
else
{
ch2++;
}
// Creates a process with a random length between the minimum and maximum lengths.
procTime = r.nextInt( PROC_MAX_LENGTH - PROC_MIN_LENGTH + 1 ) + PROC_MIN_LENGTH;
addProcess( s, time, procTime );
}
// Increment the time on the CPU.
temp = cpu1.tick( time );
// If a job finished, add it to finished.
if ( temp != null )
{
finished.add( temp );
}
// If there is nothing on the CPU, add something to it.
if ( cpu1.isEmpty( ) )
{
temp = select( ready );
cpu1.begin( temp, time );
}
time++;
}
// Print the results.
printResults( ready, finished );
}
/**
* This method prints the results of the simulation.
* @param ready - the list of jobs waiting for the CPU.
* @param finished - the list of jobs that finished during the simulation.
*/
public void printResults( ArrayList<Process> ready, ArrayList<Process> finished )
{
// Variables to calculate results.
int totalResponseTime = 0;
int totalTurnaroundTime = 0;
double avgRT, avgTT;
int s, f, fr, r, t, l;
String n;
// Everything on ready is unfinished. Ignore the job currently running on the CPU.
System.out.println( "There are " + ready.size() + " unfinished processes." );
System.out.println( );
// Print results from all finished processes.
System.out.println( "Name Arrive First Run Finished Length Response Turnaround" );
for ( Process p : finished )
{
n = p.getName( );
s = p.getStartTime( );
f = p.getEndTime( );
fr = p.getFirstRunTime( );
l = p.getLength( );
r = fr - s;
t = f - s;
System.out.format( "%4s %6d %9d %8d %6d %8d %10d%n", n, s, fr, f, l, r, t );
totalResponseTime += r;
totalTurnaroundTime += t;
}
// Calculate average response and turnaround times.
avgRT = (double)totalResponseTime / finished.size( );
avgTT = (double)totalTurnaroundTime / finished.size( );
System.out.println( );
System.out.format( "Averages %8.2f %10.2f%n", avgRT, avgTT );
// Print out the CPU usage chart.
System.out.println( );
cpu1.printChart( );
}
}
到目前为止,这是我对STCF所拥有的,但在这种情况下我一直遇到错误:
Beginning simulation.
New process arrived at time = 0
Exception in thread "main" java.lang.NullPointerException
at CPU.remainingTime(CPU.java:118)
at Simulator.select(Simulator.java:153)
at Simulator.run(Simulator.java:246)
at Simulator.main(Simulator.java:64)
当进程到达的可能性变为20%,进程的最小长度为2,进程的最大长度为5,结束时间为100时,它应该看起来像这样的FIFO; FIFO Results (Picture)
我只是想知道我的STCF / SRTF代码是否产生了这些错误。一直试图弄清楚这几个小时,但没有成功。任何帮助将不胜感激。非常感谢你。