Anda di halaman 1dari 4

//Class BusyInterruptionException.

java
package apps.tree.scheduling;
public class BusyInterruptionException extends Exception{
public BusyInterruptionException(){
super();
}
public BusyInterruptionException(String s){
super(s);
}
}
Following is the Scheduler class, which is the manager of the overall situation.
Scheduler class. This class coordinates the scheduling activities, and thus inte
racts with all the other classes of the apps.tree.scheduling package. Since it i
s also the only class in the package that interacts directly with the Heap class
, its methods are declared public.
Here is the class outline, followed by a detalied explanation.
//Class Scheduler.java
package apps.tree.scheduling;
import structures.tree.heap;
import java.io.PrintWriter;
public class Scheduler{
ProcessSource source; //where processes are created
Processor cpu; //executing processor
Heap<Process> procHeap; //priority queue of processes
int thruPut, intervalThruPut; //process arrival numbers
int numProcs, intervalNumProcs;//process arrival numbers
int clock, intervalClock; //simulation and interval clocks
public Scheduler() {
procHeap = new Heap<Process>();
}
public void init(float probofArrival, int maxExecTime,
int maxPriority, int timeSlice)
{...}
public void run(int howLong)
throws BusyInterruptionException
{
...}
public void printStatus(PrintWriter pw)
{
..
}
public void printHeap(PrintWriter pw)
{..
}
Fields. The number of processes that have completed execution is called the thro
ughput. This is measured on both a cumulative basis and a per-interval basis by
the variables thruPut and intervalThruPut, respectively. Interval here refers to
a certain number of time units for which the simulation runsand thyen pauses to
observe the state. Tbhis is expanded on in the discussion of the run method tha
t follows.
The fields numProcs and intervalNumProcs count the number of processes that arri
ve cumulatively and in the latest interval, respectively.
Initialization and status. The init method sets up the environment of the simula
tion system by initializing the various component and control parameters. The co
de is self explanatory.
public void init(float probOfArrival, int maxExecTime,
int maxPriority, int timeSlice)
{
//create system objects and initialize
source = new ProcessSourcde(probofArrival. maxExecTime, maxPriority);
cpu = new processor(timeSlice);
procHeap.clear();
clock =0 ; numProcs = 0; thruPut = 0;
}
The printStatus method prints various statistics that summarize the simulation p
rocess for the entire duration since the start of the simulation(cumulative) as
well as the duration of the latest time period (interval) of interest.
public void printStatus(PrintWriter pw)
{
pw.println("Interval cumulative");
pw.println("Processes Arrived" + intervalNumProcs + "" + numProcs);
pw.println("Throughput"+ intervalThruPut+ "" + thruPut);
pw.flush();
}
The printHeap method prints the content of the process queue at the time it is c
alled.
public void printHeap(PrintWriter pw)
{
pw.print("Heap: ");
Process proc = procHeap.first();
while(proc != null)
{
pw.print(proc + "");
proc = procHeap.next();
}
pw.println(); pw.flush();
}

Method run. The run method is the core simulation method, used to run the simula
tion for any desired number of time units, howLong. For instance, if you want ro
run a simulation for 50 time units, you may want to break it up into five perio
ds of 10 timde unis each so that you can study the statistics at the end of each
of these periods and look at the process queue. To do this, you would call run
(10) five times, once for each period.
public void run(int howLong)
throws BusyInterruptionException{
intervalClock = 0;
intervalNumProcs=0;
intervalThruPut = 0;
//run for given cycles
while(intervalClock , howLong)
{
//Process Event
...
//step up the time
clock++;
intervalClock++;
cpu.stepTime();
}
}
In each call to run, the simulation proceeds in steps of unit-time intervals. ea
ch such unit interval is simulated by a while loop.
In each time interval, one or more of the following event smay occur. They are p
rocessed (PROCESS EVENT in the while loop above) in the given order.
1.A new process arrives
Recall that the method getProcess of the class ProcessSource delivers a new proc
ess (with all but arrival Time filled in) if one has arrived or returns null. On
getting a new process, the run method fills in the arrival time and immediately
add this process to the heap.
The next two events occur if the processor is idle.
//has a new process arrived ?
Process process = source.getprocess();
if (process != null)
{
process.arrivalTime = clock;
procHeap.add(process);
intervalNumProcs++;
numProcs++;
}
if(cpu.isIdle()){
//Process CPU-Idle events
2. A process just finishes its turn at the processor.
If the processor is idle, it is checked (using the getProcess method) to see whe
ther a process has just finished its turn. If so, this process may be completely
done or may need to be rescheduled to finish up later.
If a process is to be rescheduled, its execution time is decremented by the proc
essor time slice, its priority is decremented (but not below zero). and it is ad
ded back to the heap with a new arrival time.
process = cpu.getProcess();
if(process != null)
{
//remaininmg execution time
process.execTime -= cpu.timeSlice;
if(process.execTime >0)
{
//decrease priority
process.execTime -=cpu.timeSlice;
if(process.priority < 0)
{
process.priority = 0;
}
process.arrivalTime = clock;
//reenter in heap with
//decreased priority
procheap.add(process);
}else
{
thruPut++;
intervalThruPut++;
}
}

Anda mungkin juga menyukai