SlideShare a Scribd company logo
I Need Serious Help with this assignment. Plenty Points to be earned here
Objective: To implement Thread scheduling on the OSP2 simulator using a modified version of
the Linux Completely Fair Scheduler strategy. You will implement the module ThreadCB.java to
further your understanding of CPU scheduling. Required to turn in: Follow all directions
regarding hand-in procedures. Points will be deducted if you do not follow directions precisely.
You must submit an electronic copy of your Linux Completely Fair Scheduler solution (or your
best try) via dropbox. Late assignments will be assessed a 10% penalty per day. You must
document your code and provide a one-page explanation of how you accomplished the
assignment (or what you have currently and why you could not complete). You should describe
your use, creation, and manipulation of data structures to accomplish the assignment. 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 Linux Completely Fair Scheduler solution. The changes
you need to make to your code from the previous project are: 1) You will need to add three
instance variables and three class variables to the ThreadCB class: Private long lastDispatch; an
instance variable that keeps track of when a thread is dispatched. private int lastCpuBurst; an
instance variable that you use to update vRunTime. private long vRunTime; an instance variable
that keeps track of the virtual run time of each thread, and private static long numThreads; a
static class variable that keeps track of how many threads there are. private static ThreadCB[]
heap; a static class variable that is used as a heap structure. private static int heapSize; a static
class variable that is used keep track of how many threads are in the heap. Be sure to initialize it
to 0. 2) Each time you change the status of a thread from ThreadRunning to any other state, you
will update its vRunTime value. You will start by determining its current burst time. 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(). Next you update vRunTime using the
following expression: thread.vRunTime = ((thread.vRunTime+thread.lastCpuBurst); 3) Instead
of using a GenericList readyQueue, you will use a priority queue implemented as a heap. You
will code the heap yourself. This is much simpler than the red black tree that Linux actually uses.
While you could create a separate heap class, I suggest that you simply declare the heap and the
two methods indicated below. My suggestion is that you look at the implementation of the heap
from Mark Weiss' Data Structures and Problem Solving with Java, Addison Wesley for
inspiration. http://www.cs.duke.edu/csed/ap/subset/annotated/ap/HeapPriorityQueue.java The
declaration of the heap is listed in step 1) on the previous page. The actual initialization of the
heap is: heap = new ThreadCB[200]; heapSize = 0; The methods that your will have to code are:
private static void insert(ThreadCB t) private static ThreadCB removeTopOfHeap() The method
insert() inserts the ThreadCB t into the appropriate position in the heap. The thread with the
smallest vRunTime should be at the top of the heap. In other words, your insert method should
be comparing the vRunTime values of threads in order to place a newly inserted thread into the
appropriate position in the heap. The method removeTopOfHeap() removes and returns the
thread at the top of the heap and repairs the heap so that of the remaining threads, the thread with
the smallest vRuntTime is at the top of the heap. BE SURE TO REMOVE ALL REFERENCES
TO THE GENERICLIST READYQUEUE FROM YOUR CODE SO YOU DON’T GET
TRIPPED UP. 4) In do_create(), the newly created thread will be assigned the default vRunTime
of 0 and be placed in the heap. In the context of the heap, vRunTime is the value that is used to
sort the heap. Don't forget to update the value of numThreads. 5) In do_kill(), you will update
the value of numThreads. You also make sure that the thread you are killing is no longer in the
heap. 6) In do_dispatch() when the current thread is preempted, you need to update it's values
for lastDispatch and vRunTime and insert it into the heap. 7) In do_dispatch(), you should
dispatch the thread with the smallest vRunTime. If the heap is empty, then should you idle the
CPU by setting PTBR to null and returning FAILURE. Otherwise, use the heap method
removeTopOfHeap() to retrieve the thread with the smallest vRunTime value. The quantum for
the thread will be calculated by the following expression: 1000/numThreads; Use this quantum
value to set the timer interrupt. Some more hints: The outlined algorithm for the solution to CPU
scheduling is derived from a discussion in your OSP2 book regarding the thread scheduling
module ThreadCB. Your OSP2 book (and the Silberschatz book) are your best references for
conceptual questions on implementation of ThreadCB.java. Be very careful that you do not
leave a thread in the ready queue when it is dispatched. If you do, and then re-insert it after a
block, the queue may become disconnected, making some ready threads inaccessible. These
stranded threads can never be re-scheduled, so threads may starve in the ready queue.
//ThreadCB.java file
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 readyQ;
public ThreadCB() {
super();
}
public static void init() {
readyQ = new GenericList();
}
/*creates the thread*/
static public ThreadCB do_create(TaskCB task) {
/*Checks to see if the task is null */
if (task == null)
{
ThreadCB.dispatch();
return null;
}
if (task.getThreadCount() >= MaxThreadsPerTask)
{
ThreadCB.dispatch();
return null;
}
/*creates a new thread */
ThreadCB thread = new ThreadCB();
thread.setPriority(task.getPriority());
thread.setStatus(ThreadReady);
thread.setTask(task);
if(task.addThread(thread)==FAILURE)
{
dispatch();
return null;
}
readyQ.append(thread);
dispatch();
return thread;
}
/*destroys a thread*/
public void do_kill() {
TaskCB theTask = null;
if (this.getStatus() == ThreadReady) { /*checks to see if the status of the thread is ready*/
readyQ.remove(this);
this.setStatus(ThreadKill);
}
if (this.getStatus() == ThreadRunning) {
if (MMU.getPTBR().getTask().getCurrentThread() == this) {
MMU.setPTBR(null);
getTask().setCurrentThread(null);
}
}
if (this.getStatus() >= ThreadWaiting) {
this.setStatus(ThreadKill);
}
theTask = this.getTask();
theTask.removeThread(this);
this.setStatus(ThreadKill);
for (int i = 0; i < Device.getTableSize(); i++) {
Device.get(i).cancelPendingIO(this);
}
ResourceCB.giveupResources(this);
if (this.getTask().getThreadCount() == 0) {
this.getTask().kill();
}
dispatch();
}
/*suspends/increments the ThreadWaiting status of a thread*/
public void do_suspend(Event event) {
int status = this.getStatus(); /*Gets the current state of the thread*/
TaskCB task = this.getTask();
if(status==ThreadRunning){
if (MMU.getPTBR().getTask().getCurrentThread() == this) {
this.setStatus(ThreadWaiting);
MMU.setPTBR(null);
this.getTask().setCurrentThread(null);
}
}
else if(status >= ThreadWaiting){
this.setStatus(this.getStatus()+1);
}
if(!event.contains(this)) {
event.addThread(this);
}
ThreadCB.dispatch();
}
/*resumes/decrements the ThreadWaiting status of a thread*/
public void do_resume()
{
if(getStatus() < ThreadWaiting) {
MyOut.print(this, "Attempt to resume " + this + ", which wasn't waiting");
return;
}
MyOut.print(this, "Resuming " + this);
if(this.getStatus() == ThreadWaiting) {
setStatus(ThreadReady);
} else if (this.getStatus() > ThreadWaiting) {
setStatus(getStatus()-1);
}
if (getStatus() == ThreadReady) {
readyQ.append(this);
}
dispatch();
}
/*Selects a thread to dispatch*/
public static int do_dispatch() {
ThreadCB thread = null;
ThreadCB newThread = null;
try {
thread = MMU.getPTBR().getTask().getCurrentThread(); /*Get current task and thread*/
}
catch (NullPointerException e){
}
if (thread != null) {
thread.getTask().setCurrentThread(null);
MMU.setPTBR(null);
thread.setStatus(ThreadReady);
readyQ.append(thread);
}
if (readyQ.isEmpty()) { /*if the the ready queue is empty, then it will return a failure*/
MMU.setPTBR(null);
return FAILURE;
}
else {
newThread = (ThreadCB)readyQ.removeHead();
MMU.setPTBR(newThread.getTask().getPageTable());
newThread.getTask().setCurrentThread(newThread);
newThread.setStatus(ThreadRunning);
}
return SUCCESS;
}
public static void atError() {
}
public static void atWarning() {
}
}
Solution
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 readyQ;
public ThreadCB() {
super();
}
public static void init() {
readyQ = new GenericList();
}
/*creates the thread*/
static public ThreadCB do_create(TaskCB task) {
/*Checks to see if the task is null */
if (task == null)
{
ThreadCB.dispatch();
return null;
}
if (task.getThreadCount() >= MaxThreadsPerTask)
{
ThreadCB.dispatch();
return null;
}
/*creates a new thread */
ThreadCB thread = new ThreadCB();
thread.setPriority(task.getPriority());
thread.setStatus(ThreadReady);
thread.setTask(task);
if(task.addThread(thread)==FAILURE)
{
dispatch();
return null;
}
readyQ.append(thread);
dispatch();
return thread;
}
/*destroys a thread*/
public void do_kill() {
TaskCB theTask = null;
if (this.getStatus() == ThreadReady) { /*checks to see if the status of the thread is ready*/
readyQ.remove(this);
this.setStatus(ThreadKill);
}
if (this.getStatus() == ThreadRunning) {
if (MMU.getPTBR().getTask().getCurrentThread() == this) {
MMU.setPTBR(null);
getTask().setCurrentThread(null);
}
}
if (this.getStatus() >= ThreadWaiting) {
this.setStatus(ThreadKill);
}
theTask = this.getTask();
theTask.removeThread(this);
this.setStatus(ThreadKill);
for (int i = 0; i < Device.getTableSize(); i++) {
Device.get(i).cancelPendingIO(this);
}
ResourceCB.giveupResources(this);
if (this.getTask().getThreadCount() == 0) {
this.getTask().kill();
}
dispatch();
}
/*suspends/increments the ThreadWaiting status of a thread*/
public void do_suspend(Event event) {
int status = this.getStatus(); /*Gets the current state of the thread*/
TaskCB task = this.getTask();
if(status==ThreadRunning){
if (MMU.getPTBR().getTask().getCurrentThread() == this) {
this.setStatus(ThreadWaiting);
MMU.setPTBR(null);
this.getTask().setCurrentThread(null);
}
}
else if(status >= ThreadWaiting){
this.setStatus(this.getStatus()+1);
}
if(!event.contains(this)) {
event.addThread(this);
}
ThreadCB.dispatch();
}
/*resumes/decrements the ThreadWaiting status of a thread*/
public void do_resume()
{
if(getStatus() < ThreadWaiting) {
MyOut.print(this, "Attempt to resume " + this + ", which wasn't waiting");
return;
}
MyOut.print(this, "Resuming " + this);
if(this.getStatus() == ThreadWaiting) {
setStatus(ThreadReady);
} else if (this.getStatus() > ThreadWaiting) {
setStatus(getStatus()-1);
}
if (getStatus() == ThreadReady) {
readyQ.append(this);
}
dispatch();
}
/*Selects a thread to dispatch*/
public static int do_dispatch() {
ThreadCB thread = null;
ThreadCB newThread = null;
try {
thread = MMU.getPTBR().getTask().getCurrentThread(); /*Get current task and thread*/
}
catch (NullPointerException e){
}
if (thread != null) {
thread.getTask().setCurrentThread(null);
MMU.setPTBR(null);
thread.setStatus(ThreadReady);
readyQ.append(thread);
}
if (readyQ.isEmpty()) { /*if the the ready queue is empty, then it will return a failure*/
MMU.setPTBR(null);
return FAILURE;
}
else {
newThread = (ThreadCB)readyQ.removeHead();
MMU.setPTBR(newThread.getTask().getPageTable());
newThread.getTask().setCurrentThread(newThread);
newThread.setStatus(ThreadRunning);
}
return SUCCESS;
}
public static void atError() {
}
public static void atWarning() {
}
}

More Related Content

PPTX
Java Multithreading.pptx
PPTX
Java Multithreading - how to create threads
PPT
PDF
Below is the question I need help with. It need to be done in Java. .pdf
PPTX
Java-Threads And Concurrency Presentation. 2024
PPTX
Java Threads And Concurrency Presentation. 2024
PPT
Java util concurrent
PPTX
Multithreading.pptx
Java Multithreading.pptx
Java Multithreading - how to create threads
Below is the question I need help with. It need to be done in Java. .pdf
Java-Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
Java util concurrent
Multithreading.pptx

Similar to I Need Serious Help with this assignment. Plenty Points to be earned.pdf (20)

PPTX
OS Module-2.pptx
PDF
Other Approaches (Concurrency)
ODP
Multithreading Concepts
PPTX
multithreading.pptx
PPT
Threads in Java
PPTX
Java programming PPT. .pptx
PDF
Bài tập lớn hệ điều hành HCMUT_HK232.pdf
PPTX
Multithreading
PDF
shashank_micro92_00697015
PDF
Here comes the Loom - Ya!vaConf.pdf
DOCX
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPT
Slot02 concurrency1
PPTX
FreeRTOS Xilinx Vivado: Hello World!
DOCX
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
DOCX
Programming Assignment #2CSci 430 Spring 2019Dates.docx
DOCX
Programming Assignment #2CSci 430 Spring 2019Dates.docx
PPTX
OS_module2. .pptx
PDF
JAVA THREADS.pdf
OS Module-2.pptx
Other Approaches (Concurrency)
Multithreading Concepts
multithreading.pptx
Threads in Java
Java programming PPT. .pptx
Bài tập lớn hệ điều hành HCMUT_HK232.pdf
Multithreading
shashank_micro92_00697015
Here comes the Loom - Ya!vaConf.pdf
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
multithreading,thread and processinjava-210302183809.pptx
Slot02 concurrency1
FreeRTOS Xilinx Vivado: Hello World!
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docx
OS_module2. .pptx
JAVA THREADS.pdf
Ad

More from ahntagencies (20)

PDF
Mitosis promoting factor is associated with cyclin b. Which class of .pdf
PDF
LTE uses an all IP data network, with voice calls reduced to a multim.pdf
PDF
I have code that reads and writes sstrings to file but I am having t.pdf
PDF
Genes A and B are on different chromosomes. A dihybrid with the geno.pdf
PDF
Do the following program in C++- Create a item class... with and i.pdf
PDF
Explain the process of RNAi and how it contributes to differential .pdf
PDF
Consider the following scenario A population of moderately pigmented.pdf
PDF
Consider the simple analog signal defined by x(t) = sin(2700t) shown.pdf
PDF
Cell adhesion, both cell-cell and cell-ECM, can influence whether a c.pdf
PDF
A(n) is a method of interacting with a PC by manipulating visual ele.pdf
PDF
You are about to perform a clean install of Windows 7 on a brandnew .pdf
PDF
Write the equation of a 4th degree polynomial function with the follo.pdf
PDF
Write a program which repeatedly calls a function named replaceIt() a.pdf
PDF
Which, of the below choices of inference rules, is used to show that.pdf
PDF
Which of the following protocols is used for router multicastingA.pdf
PDF
Which of the following isare not produced by homosporous plants [se.pdf
PDF
When Gregor Mendel was developing his ideas of inheritance of cha.pdf
PDF
what kind of short tandem repeats DNA markers can be used for tracin.pdf
PDF
What is the job of a layout managerTo render user interface compo.pdf
PDF
Using the Bernoulli method, solve the differential equation t^2 dyd.pdf
Mitosis promoting factor is associated with cyclin b. Which class of .pdf
LTE uses an all IP data network, with voice calls reduced to a multim.pdf
I have code that reads and writes sstrings to file but I am having t.pdf
Genes A and B are on different chromosomes. A dihybrid with the geno.pdf
Do the following program in C++- Create a item class... with and i.pdf
Explain the process of RNAi and how it contributes to differential .pdf
Consider the following scenario A population of moderately pigmented.pdf
Consider the simple analog signal defined by x(t) = sin(2700t) shown.pdf
Cell adhesion, both cell-cell and cell-ECM, can influence whether a c.pdf
A(n) is a method of interacting with a PC by manipulating visual ele.pdf
You are about to perform a clean install of Windows 7 on a brandnew .pdf
Write the equation of a 4th degree polynomial function with the follo.pdf
Write a program which repeatedly calls a function named replaceIt() a.pdf
Which, of the below choices of inference rules, is used to show that.pdf
Which of the following protocols is used for router multicastingA.pdf
Which of the following isare not produced by homosporous plants [se.pdf
When Gregor Mendel was developing his ideas of inheritance of cha.pdf
what kind of short tandem repeats DNA markers can be used for tracin.pdf
What is the job of a layout managerTo render user interface compo.pdf
Using the Bernoulli method, solve the differential equation t^2 dyd.pdf
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Classroom Observation Tools for Teachers
Chinmaya Tiranga quiz Grand Finale.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Cell Structure & Organelles in detailed.
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
History, Philosophy and sociology of education (1).pptx
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Anesthesia in Laparoscopic Surgery in India
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx

I Need Serious Help with this assignment. Plenty Points to be earned.pdf

  • 1. I Need Serious Help with this assignment. Plenty Points to be earned here Objective: To implement Thread scheduling on the OSP2 simulator using a modified version of the Linux Completely Fair Scheduler strategy. You will implement the module ThreadCB.java to further your understanding of CPU scheduling. Required to turn in: Follow all directions regarding hand-in procedures. Points will be deducted if you do not follow directions precisely. You must submit an electronic copy of your Linux Completely Fair Scheduler solution (or your best try) via dropbox. Late assignments will be assessed a 10% penalty per day. You must document your code and provide a one-page explanation of how you accomplished the assignment (or what you have currently and why you could not complete). You should describe your use, creation, and manipulation of data structures to accomplish the assignment. 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 Linux Completely Fair Scheduler solution. The changes you need to make to your code from the previous project are: 1) You will need to add three instance variables and three class variables to the ThreadCB class: Private long lastDispatch; an instance variable that keeps track of when a thread is dispatched. private int lastCpuBurst; an instance variable that you use to update vRunTime. private long vRunTime; an instance variable that keeps track of the virtual run time of each thread, and private static long numThreads; a static class variable that keeps track of how many threads there are. private static ThreadCB[] heap; a static class variable that is used as a heap structure. private static int heapSize; a static class variable that is used keep track of how many threads are in the heap. Be sure to initialize it to 0. 2) Each time you change the status of a thread from ThreadRunning to any other state, you will update its vRunTime value. You will start by determining its current burst time. 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(). Next you update vRunTime using the following expression: thread.vRunTime = ((thread.vRunTime+thread.lastCpuBurst); 3) Instead of using a GenericList readyQueue, you will use a priority queue implemented as a heap. You will code the heap yourself. This is much simpler than the red black tree that Linux actually uses. While you could create a separate heap class, I suggest that you simply declare the heap and the two methods indicated below. My suggestion is that you look at the implementation of the heap from Mark Weiss' Data Structures and Problem Solving with Java, Addison Wesley for inspiration. http://www.cs.duke.edu/csed/ap/subset/annotated/ap/HeapPriorityQueue.java The declaration of the heap is listed in step 1) on the previous page. The actual initialization of the
  • 2. heap is: heap = new ThreadCB[200]; heapSize = 0; The methods that your will have to code are: private static void insert(ThreadCB t) private static ThreadCB removeTopOfHeap() The method insert() inserts the ThreadCB t into the appropriate position in the heap. The thread with the smallest vRunTime should be at the top of the heap. In other words, your insert method should be comparing the vRunTime values of threads in order to place a newly inserted thread into the appropriate position in the heap. The method removeTopOfHeap() removes and returns the thread at the top of the heap and repairs the heap so that of the remaining threads, the thread with the smallest vRuntTime is at the top of the heap. BE SURE TO REMOVE ALL REFERENCES TO THE GENERICLIST READYQUEUE FROM YOUR CODE SO YOU DON’T GET TRIPPED UP. 4) In do_create(), the newly created thread will be assigned the default vRunTime of 0 and be placed in the heap. In the context of the heap, vRunTime is the value that is used to sort the heap. Don't forget to update the value of numThreads. 5) In do_kill(), you will update the value of numThreads. You also make sure that the thread you are killing is no longer in the heap. 6) In do_dispatch() when the current thread is preempted, you need to update it's values for lastDispatch and vRunTime and insert it into the heap. 7) In do_dispatch(), you should dispatch the thread with the smallest vRunTime. If the heap is empty, then should you idle the CPU by setting PTBR to null and returning FAILURE. Otherwise, use the heap method removeTopOfHeap() to retrieve the thread with the smallest vRunTime value. The quantum for the thread will be calculated by the following expression: 1000/numThreads; Use this quantum value to set the timer interrupt. Some more hints: The outlined algorithm for the solution to CPU scheduling is derived from a discussion in your OSP2 book regarding the thread scheduling module ThreadCB. Your OSP2 book (and the Silberschatz book) are your best references for conceptual questions on implementation of ThreadCB.java. Be very careful that you do not leave a thread in the ready queue when it is dispatched. If you do, and then re-insert it after a block, the queue may become disconnected, making some ready threads inaccessible. These stranded threads can never be re-scheduled, so threads may starve in the ready queue. //ThreadCB.java file 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.*;
  • 3. import osp.Memory.*; import osp.Resources.*; public class ThreadCB extends IflThreadCB { static GenericList readyQ; public ThreadCB() { super(); } public static void init() { readyQ = new GenericList(); } /*creates the thread*/ static public ThreadCB do_create(TaskCB task) { /*Checks to see if the task is null */ if (task == null) { ThreadCB.dispatch(); return null; } if (task.getThreadCount() >= MaxThreadsPerTask) { ThreadCB.dispatch(); return null; } /*creates a new thread */ ThreadCB thread = new ThreadCB(); thread.setPriority(task.getPriority()); thread.setStatus(ThreadReady);
  • 4. thread.setTask(task); if(task.addThread(thread)==FAILURE) { dispatch(); return null; } readyQ.append(thread); dispatch(); return thread; } /*destroys a thread*/ public void do_kill() { TaskCB theTask = null; if (this.getStatus() == ThreadReady) { /*checks to see if the status of the thread is ready*/ readyQ.remove(this); this.setStatus(ThreadKill); } if (this.getStatus() == ThreadRunning) { if (MMU.getPTBR().getTask().getCurrentThread() == this) { MMU.setPTBR(null); getTask().setCurrentThread(null); } }
  • 5. if (this.getStatus() >= ThreadWaiting) { this.setStatus(ThreadKill); } theTask = this.getTask(); theTask.removeThread(this); this.setStatus(ThreadKill); for (int i = 0; i < Device.getTableSize(); i++) { Device.get(i).cancelPendingIO(this); } ResourceCB.giveupResources(this); if (this.getTask().getThreadCount() == 0) { this.getTask().kill(); } dispatch(); } /*suspends/increments the ThreadWaiting status of a thread*/ public void do_suspend(Event event) { int status = this.getStatus(); /*Gets the current state of the thread*/ TaskCB task = this.getTask(); if(status==ThreadRunning){ if (MMU.getPTBR().getTask().getCurrentThread() == this) { this.setStatus(ThreadWaiting); MMU.setPTBR(null); this.getTask().setCurrentThread(null); } }
  • 6. else if(status >= ThreadWaiting){ this.setStatus(this.getStatus()+1); } if(!event.contains(this)) { event.addThread(this); } ThreadCB.dispatch(); } /*resumes/decrements the ThreadWaiting status of a thread*/ public void do_resume() { if(getStatus() < ThreadWaiting) { MyOut.print(this, "Attempt to resume " + this + ", which wasn't waiting"); return; } MyOut.print(this, "Resuming " + this); if(this.getStatus() == ThreadWaiting) { setStatus(ThreadReady); } else if (this.getStatus() > ThreadWaiting) { setStatus(getStatus()-1); } if (getStatus() == ThreadReady) { readyQ.append(this); }
  • 7. dispatch(); } /*Selects a thread to dispatch*/ public static int do_dispatch() { ThreadCB thread = null; ThreadCB newThread = null; try { thread = MMU.getPTBR().getTask().getCurrentThread(); /*Get current task and thread*/ } catch (NullPointerException e){ } if (thread != null) { thread.getTask().setCurrentThread(null); MMU.setPTBR(null); thread.setStatus(ThreadReady); readyQ.append(thread); } if (readyQ.isEmpty()) { /*if the the ready queue is empty, then it will return a failure*/ MMU.setPTBR(null); return FAILURE; } else {
  • 8. newThread = (ThreadCB)readyQ.removeHead(); MMU.setPTBR(newThread.getTask().getPageTable()); newThread.getTask().setCurrentThread(newThread); newThread.setStatus(ThreadRunning); } return SUCCESS; } public static void atError() { } public static void atWarning() { } } Solution 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.*;
  • 9. public class ThreadCB extends IflThreadCB { static GenericList readyQ; public ThreadCB() { super(); } public static void init() { readyQ = new GenericList(); } /*creates the thread*/ static public ThreadCB do_create(TaskCB task) { /*Checks to see if the task is null */ if (task == null) { ThreadCB.dispatch(); return null; } if (task.getThreadCount() >= MaxThreadsPerTask) { ThreadCB.dispatch(); return null; } /*creates a new thread */ ThreadCB thread = new ThreadCB(); thread.setPriority(task.getPriority()); thread.setStatus(ThreadReady); thread.setTask(task); if(task.addThread(thread)==FAILURE)
  • 10. { dispatch(); return null; } readyQ.append(thread); dispatch(); return thread; } /*destroys a thread*/ public void do_kill() { TaskCB theTask = null; if (this.getStatus() == ThreadReady) { /*checks to see if the status of the thread is ready*/ readyQ.remove(this); this.setStatus(ThreadKill); } if (this.getStatus() == ThreadRunning) { if (MMU.getPTBR().getTask().getCurrentThread() == this) { MMU.setPTBR(null); getTask().setCurrentThread(null); } } if (this.getStatus() >= ThreadWaiting) { this.setStatus(ThreadKill); }
  • 11. theTask = this.getTask(); theTask.removeThread(this); this.setStatus(ThreadKill); for (int i = 0; i < Device.getTableSize(); i++) { Device.get(i).cancelPendingIO(this); } ResourceCB.giveupResources(this); if (this.getTask().getThreadCount() == 0) { this.getTask().kill(); } dispatch(); } /*suspends/increments the ThreadWaiting status of a thread*/ public void do_suspend(Event event) { int status = this.getStatus(); /*Gets the current state of the thread*/ TaskCB task = this.getTask(); if(status==ThreadRunning){ if (MMU.getPTBR().getTask().getCurrentThread() == this) { this.setStatus(ThreadWaiting); MMU.setPTBR(null); this.getTask().setCurrentThread(null); } } else if(status >= ThreadWaiting){ this.setStatus(this.getStatus()+1);
  • 12. } if(!event.contains(this)) { event.addThread(this); } ThreadCB.dispatch(); } /*resumes/decrements the ThreadWaiting status of a thread*/ public void do_resume() { if(getStatus() < ThreadWaiting) { MyOut.print(this, "Attempt to resume " + this + ", which wasn't waiting"); return; } MyOut.print(this, "Resuming " + this); if(this.getStatus() == ThreadWaiting) { setStatus(ThreadReady); } else if (this.getStatus() > ThreadWaiting) { setStatus(getStatus()-1); } if (getStatus() == ThreadReady) { readyQ.append(this); } dispatch(); }
  • 13. /*Selects a thread to dispatch*/ public static int do_dispatch() { ThreadCB thread = null; ThreadCB newThread = null; try { thread = MMU.getPTBR().getTask().getCurrentThread(); /*Get current task and thread*/ } catch (NullPointerException e){ } if (thread != null) { thread.getTask().setCurrentThread(null); MMU.setPTBR(null); thread.setStatus(ThreadReady); readyQ.append(thread); } if (readyQ.isEmpty()) { /*if the the ready queue is empty, then it will return a failure*/ MMU.setPTBR(null); return FAILURE; } else { newThread = (ThreadCB)readyQ.removeHead();