Question
Java/Operating Systems Objective To implement Thread scheduling on the OSP2 simulator using a version of the Shortest Job Remaining strategy with ageing. You will implement
Java/Operating Systems
Objective
To implement Thread scheduling on the OSP2 simulator using a version of the Shortest Job Remaining strategy with ageing. You will implement the module ThreadCB.java to further your understanding of CPU scheduling.
Building and Executing the Simulation
As in the previous assignment, I suggest that you create a new directory. Copy your files from the previous cpu scheduling project and starting with that version of ThreadCB.java modify it to create the SJR solution. The only file you should have to modify is ThreadCB.java. Modifying the other files will probably "break" OSP2. The only changes you need to make to your code from the previous project are:
1) Each time you change the status of a thread from ThreadRunning to any other state, you will estimate the cpu burst time using exponential averaging (n = tn-1 + (1-)n-1). Use an alpha value of 0.75, =0.75+0.25. If then set = 5. To make this work you will need to keep track of the last cpu burst for the thread. You will also need to keep track of the last estimate of the burst time. Do this by adding additional variables to the ThreadCB class:
a. int lastCpuBurst (this is in the equation)
b. int estimatedBurstTime (this is t in the equation)
c. long lastDispatch (you need this to compute lastCpuBurst)
When you first create a thread you should initialize the values of lastCpuBurst and estimatedBurstTime to 10. The reason for initializing these values is that there are no previous values of and that you can use to compute estimatedBurstTime.
2) In order to estimate the burst time, you need the value of lastCpuBurst. Each time you change the status of a thread from ThreadRunnig to some other state you should calculate the value of lastCpuBurst and save it in this variable. You calculate the value using the following equation:
thread.lastCpuBurst = HClock.get() thread.lastDispatch; As you can see, you will need the value of lastDispatch in order to do this. Be sure to set this variable each time you dispatch a new thread, i.e., thread.lastDispatch = HClock.get() just before returning from do_dispatch.
3) Be sure to save the estimated burst time into the class variable estimatedBurstTime.
4) When your do_dispatch()is invoked, after checking whether there was a threadrunning (and calculating how much time is remaining for that thread by taking the difference of the current time, HClock.get() and the lastDispatch time of that thread), you will check the ready queue to see if there is a thread that has a shorter remaining burst time. If the remaining burst time for the current thread is as short or shorter than the estimated burst time of the threads in the ready queue then continue with the current thread and return SUCCESS. Or if the current burst of the current thread is less than 2 milliseconds, then continue with the current thread. In this case, do not change the value of lastDispatch since you are continuing to run the current thread. Otherwise, preempt the current thread and dispatch the thread in the ready queue with the shortest estimated burst time and return SUCCESS. In this case be sure to update lastDispatch for this new thread. If there is no current thread running and no threads are available in the ready queue then set PTBR=NULL and return FAILURE.
5) Since we are not doing round robin, DO NOT SET THE TIMER.
6) Ageing: every time you check the ready queue for a thread to dispatch, decrement the
estimated burst time for each of the threads in the ready queue by one. If t then set t = 5. The reason for ageing is that we want to ensure that even threads with large estimated burst time eventually get scheduled.
Here is the code this needs to be applied to:
package osp.Threads;
import java.util.Vector;
import java.util.Enumeration;
import osp.Utilities.*;
import osp.IFLModules.*;
import osp.Tasks.*;
import osp.EventEngine.*;
import osp.Hardware.*;
import osp.Devices.*;
import osp.Memory.*;
import osp.Resources.*;
public class ThreadCB extends IflThreadCB {
static GenericList redQue;
public ThreadCB()
{
super();
}
public static void init()
{
redQue = new GenericList();
}
static public ThreadCB do_create(TaskCB task) {
ThreadCB thread = new ThreadCB(); //Step 2b
if(task == null){ //Step 1
dispatch();
return null;
}
if(task.getThreadCount() == MaxThreadsPerTask){ //Step 2a
dispatch();
return null;
}
else {
thread.setPriority(task.getPriority()); //Step 3
thread.setStatus(ThreadReady); //Step 4
thread.setTask(task);//Step 5
if(task.addThread(thread) == 0) {
dispatch(); //Step 6
return null;
}
}
redQue.append(thread); //Step 7
dispatch(); //Step 8
return thread; //Step 9
}
public void do_kill() {
if(this.getStatus() == ThreadReady) { //Step 2
redQue.remove(this);
this.setStatus(ThreadKill);
}
if(this.getStatus() == ThreadRunning) { //Step 3
MMU.setPTBR(null);
getTask().setCurrentThread(null);
}
//if(this.getStatus() >= ThreadWaiting) {
//this.setStatus(ThreadKill);
//}
TaskCB task1 = null;
task1 = this.getTask();
task1.removeThread(this);//Step 4
this.setStatus(ThreadKill);//Step 5
for(int i = 0; i
Device.get(i).cancelPendingIO(this); //Step 6
}
ResourceCB.giveupResources(this);//Step 7
if(this.getTask().getThreadCount() == 0) { //Step 9
this.getTask().kill();
}
dispatch(); //Step 8
}
public void do_suspend(Event event) {
int stat = this.getStatus();
if(stat == ThreadRunning){
setStatus(ThreadWaiting);
this.getTask().setCurrentThread(null);
}
else if(stat >= ThreadWaiting){
setStatus(stat + 1);
}
redQue.remove(this);
if (redQue.contains(this) == false)
event.addThread(this);
dispatch(); //Step 8
}
public void do_resume() {
//Copied word for word from the text
if(getStatus()
MyOut.print(this, "Attempt to resume " + this + ", which wasn't waiting");
return;
}
MyOut.print(this, "Resuming " + this);
if(getStatus() == ThreadWaiting) {
setStatus(ThreadReady);
}
else if(getStatus() > ThreadWaiting) {
setStatus(getStatus() - 1);
}
if(getStatus() == ThreadReady) {
redQue.append(this);
}
dispatch();
}
public static int do_dispatch() {
ThreadCB Threadz = null;
ThreadCB newThreadz = null;
try{
Threadz = MMU.getPTBR().getTask().getCurrentThread();
} catch (NullPointerException e){}
if(Threadz != null){ //Step 1
Threadz.getTask().setCurrentThread(null); //Step 1a
MMU.setPTBR(null); //Step 1b
Threadz.setStatus(ThreadReady); //Step 1c
redQue.append(Threadz); //Step 1d
}
if(redQue.isEmpty()){ //Step 3
MMU.setPTBR(null);
return FAILURE;
}
else {
newThreadz = (ThreadCB)redQue.removeHead();
MMU.setPTBR(newThreadz.getTask().getPageTable());
newThreadz.getTask().setCurrentThread(newThreadz); //Step 4
newThreadz.setStatus(ThreadRunning); //Step 5
}
return SUCCESS; //Step 6
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started