Implement the scheduling algorithm described below in the Scheduler class of the scheduling
simulation program. The simulation allows you to create a number of “Sim Threads” that the
“OS” will transition among the three thread states (Queued, Running and Blocked) according to
the scheduling algorithm in the Scheduler class. Currently, the simulation implements the First-
Come Fist-Served algorithm (when preemption is turned off) and the Round Robin algorithm
(when preemption is turned on).
Modify only the Scheduler and Test classes.
Here are some methods that you might find useful:
os.setPreempt(boolean) – turns Sim Thread preemption on and
off.
os.setQuantum(int) – sets the time quantum for preemptive
algorithms.
os.print(String) – just like System.out.println, except it
only prints if the simulations’s text I/O is turned on.
os.setRunning(SimThread) – schedules the given Sim Thread.
os.getRunning() – returns the Sim Thread that is currently
in the Running state.
os.preemptRunningThread(SimThread) – preempts the currently
running thread (returning it to Queued state) and schedules
the given Sim Thread instead.
os.createSimThread(int) – creates a new Sim Thread with the
given priority and random CPU burst and wait times.
os.createSimThread(int, int, int, int) – creates a new Sim
Thread with the given CPU burst, wait time, priority, and
number of cycles.
Also, you can get or set a Sim Thread’s priority by using the getPriority() and
setPriority(int) methods, respectively, of the SimThread class.
Scheduling Algorithm 1:
Implement a Round Robin scheduling algorithm with two priorities: 0 and 1, where 0 is the
higher priority. A thread is always placed in the queue that matches its priority. Threads never
change priority once created.
When scheduling a thread, always pick the available thread with the highest priority.
If a thread entering the Queued state (i.e. being “enqueued”) has a higher priority than the
Running thread, then the Running thread is preempted in favor of the higher-priority thread.
TEST YOUR ALGORITHM!
Scheduling Algorithm 2:
Implement a multi-level feedback queue scheduling algorithm. There are four different priority
levels, 0-3, where 0 is the highest priority. You must create an array of queues, one queue for
each priority level. A thread is always placed in the queue that matches its priority.
When scheduling a thread, always pick the available thread with the highest priority.
If a thread entering the Queued state (i.e. being “enqueued”) has a higher priority than the
Running thread, then the Running thread is preempted in favor of the higher-priority
thread.
If a thread exceeds its time quantum, its priority is lowed one level.
Whenever a thread returns from the Blocked state, its priority increases one level.
The Round Robin time quantum for each thread depends upon its priority as follows:
o Priority 0 => Time Quantum is 5
o Priority 1 => Time Quantum is 8
o Priority 2 => Time Quantum is 12
o Priority 3 => Time Quantum is 15


0 comments