Question: 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
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. ( Please help me finish Shortest Job fist base on the following code that i given for you )
Code:
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.*;
/**
This class is responsible for actions related to threads, including
creating, killing, dispatching, resuming, and suspending threads.
@OSPProject Threads
*/
//Runze Li runze@email.sc.edu
//CSCE 311
public class ThreadCB extends IflThreadCB
{
static GenericList readyQueue;
/**
The thread constructor. Must call
super();
as its first statement.
@OSPProject Threads
*/
public ThreadCB()
{
// your code goes here
super();
}
/**
This method will be called once at the beginning of the
simulation. The student can set up static variables here.
@OSPProject Threads
*/
public static void init()
{
// your code goes here
//A queue for any threads ready to run a task
readyQueue = new GenericList();
}
/**
Sets up a new thread and adds it to the given task.
The method must set the ready status
and attempt to add thread to task. If the latter fails
because there are already too many threads in this task,
so does this method, otherwise, the thread is appended
to the ready queue and dispatch() is called.
The priority of the thread can be set using the getPriority/setPriority
methods. However, OSP itself doesn't care what the actual value of
the priority is. These methods are just provided in case priority
scheduling is required.
@return thread or null
@OSPProject Threads
*/
static public ThreadCB do_create(TaskCB task)
{
// your code goes here
//if task is null or thread count has reached max, return null
if ((task == null) || (task.getThreadCount() == MaxThreadsPerTask))
{
dispatch();
return null;
}
//If not create new thread
ThreadCB thread = new ThreadCB();
//Ready the thread and assign thread values
thread.setPriority(task.getPriority());
thread.setStatus(ThreadReady);
thread.setTask(task);
//If thread fails to add, return null thread
if(task.addThread(thread) == 0)
{
dispatch();
return null;
}
//Add the thread to the queue
readyQueue.append(thread);
dispatch();
return thread;
}
/**
Kills the specified thread.
The status must be set to ThreadKill, the thread must be
removed from the task's list of threads and its pending IORBs
must be purged from all device queues.
If some thread was on the ready queue, it must removed, if the
thread was running, the processor becomes idle, and dispatch()
must be called to resume a waiting thread.
@OSPProject Threads
*/
public void do_kill()
{
// your code goes here
TaskCB task = null;
//if the thread is ready, remove it from the queue
if(this.getStatus() == ThreadReady)
{
readyQueue.remove(this);
}
//if the thread is running, then if that thread is the current thread, set the page table and it's task's current thread to null
if(this.getStatus() == ThreadRunning)
{
if(MMU.getPTBR().getTask().getCurrentThread() == this)
{
MMU.setPTBR(null);
getTask().setCurrentThread(null);
}
}
//remove the thread from the task and kill the thread
task = this.getTask();
task.removeThread(this);
this.setStatus(ThreadKill);
//Cancel any pending functions and release any resources in use
for(int i=0; i { Device.get(i).cancelPendingIO(this); } ResourceCB.giveupResources(this); dispatch(); //if the task has no threads running, kill the task if(this.getTask().getThreadCount() == 0) { this.getTask().kill(); } } /** Suspends the thread that is currenly on the processor on the specified event. Note that the thread being suspended doesn't need to be running. It can also be waiting for completion of a pagefault and be suspended on the IORB that is bringing the page in. Thread's status must be changed to ThreadWaiting or higher, the processor set to idle, the thread must be in the right waiting queue, and dispatch() must be called to give CPU control to some other thread. @param event - event on which to suspend this thread. @OSPProject Threads */ public void do_suspend(Event event) { // your code goes here //boolean for if the status of a thread is changed to waiting boolean waiting = false; //if the thread is running and that thread is running a task, suspend the thread and add it to the event queue if(this.getStatus() == ThreadRunning) { if(MMU.getPTBR().getTask().getCurrentThread() == this) { MMU.setPTBR(null); this.getTask().setCurrentThread(null); this.setStatus(ThreadWaiting); event.addThread(this); waiting = true; } } //if the thread is waiting and was not changed from running to waiting, then if it is not in the thread queue, add the thread to the event queue if(this.getStatus() >= ThreadWaiting && !waiting) { this.setStatus(this.getStatus()+1); if(!readyQueue.contains(this)) { event.addThread(this); } } dispatch(); } /** Resumes the thread. Only a thread with the status ThreadWaiting or higher can be resumed. The status must be set to ThreadReady or decremented, respectively. A ready thread should be placed on the ready queue. @OSPProject Threads */ public void do_resume() { // your code goes here //if thread status is less than waiting, no need to resume if(this.getStatus() < ThreadWaiting) { return; } //if thread status is greater than wait, lower the status if(this.getStatus() > ThreadWaiting) { this.setStatus(this.getStatus()-1); } //or else if thread status is waiting, resume the thread else if(this.getStatus() == ThreadWaiting) { this.setStatus(ThreadReady); } //if the thread is ready, add it to the queue if(this.getStatus() == ThreadReady) { readyQueue.append(this); } dispatch(); } /** Selects a thread from the run queue and dispatches it. If there is just one theread ready to run, reschedule the thread currently on the processor. In addition to setting the correct thread status it must update the PTBR. @return SUCCESS or FAILURE @OSPProject Threads */ public static int do_dispatch() { // your code goes here ThreadCB thread = null; ThreadCB newThread = null; //Check if the current thread exists try { thread = MMU.getPTBR().getTask().getCurrentThread(); } catch (NullPointerException e) {} //if the thread isn't null, set it to ready and add it to the thread queue if(thread != null) { thread.getTask().setCurrentThread(null); MMU.setPTBR(null); thread.setStatus(ThreadReady); readyQueue.append(thread); } //If the thread queue is empty, set the page table to null and return FAILURE int, //else get the head of the thread queue, give it a task, set it's status to running and return SUCCESS int if(readyQueue.isEmpty()) { MMU.setPTBR(null); return FAILURE; } else { newThread = (ThreadCB)readyQueue.removeHead(); MMU.setPTBR(newThread.getTask().getPageTable()); newThread.getTask().setCurrentThread(newThread); newThread.setStatus(ThreadRunning); } return SUCCESS; } /** Called by OSP after printing an error message. The student can insert code here to print various tables and data structures in their state just after the error happened. The body can be left empty, if this feature is not used. @OSPProject Threads */ public static void atError() { // your code goes here } /** Called by OSP after printing a warning message. The student can insert code here to print various tables and data structures in their state just after the warning happened. The body can be left empty, if this feature is not used. @OSPProject Threads */ public static void atWarning() { // your code goes here } /* Feel free to add methods/fields to improve the readability of your code */ } /* Feel free to add local classes to improve the readability of your code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
