In class we discussed the timeslice CPU scheduling algorithm. We also considered
ID: 3818574 • Letter: I
Question
In class we discussed the timeslice CPU scheduling algorithm. We also considered other ways that the processes in the CPU could be scheduled. Modify the time-slicing CPU scheduling algorithm found here: http://www.cs.uri.edu/~cingiser/csc110/handouts/python/cpu_timeslice.py to implement priority-based scheduling.
Assume that there are two priority levels in the operating system, HIGH and LOW. In the data following data file: [http://www.cs.uri.edu/~cingiser/csc110/assignments/prio_proc.txt]
each process has associated with it a priority specified by ‘H’ for high, and ‘L’ for low. Write the program so that high priority processes get two times the time slice and low priority processes get only one time slice.
Note: The input file has three items on each line instead of two. You will have to split the input accordingly. You also have to make sure put the process back into the queue in the correct format. Your output will look something like this:
Enter the name of the file containing the processes prio proc .txt The time slice is 3 The contents of the queue are P1,4,H P2,5,L P3,3,L P4,7,H P5,8,L P6,9,L P7,3,H P8,2,H P9,4,L Getting next process Process P1 has 4 instructions to execute with priority H Executing instruction 3 of process P1 Timer 1 Executing instruction 2 of process P1. Timer Executing instruction 1 of process P1 Timer 3 Executing instruction 0 of process P1 Timer 4 Process P1 Complete Getting next process Process P2 has 5 instructions to execute with priority L Executing instruction 4 of process P2 Timer 1 Executing instruction 3 of process P2 Timer 2 Executing instruction 2 of process P2 Timer Put process P2 back in queue with instructions left to execute Getting next process Process P3 has 3 instructions to execute with priority L Executing instruction 2 of process P3 Timer 1 Executing instruction 1 of process P3 Timer 2 Executing instruction 0 of process P3 Timer Process P3 Complete Getting next process Process P4 has 7 instructions to execute with priority H Executing instruction 6 of process P4 Timer 1 Executing instruction 5 of process P4 Timer 2 Executing instruction 4 of process P4 Timer 3 Executing instruction 3 of process P4 Timer 4 Executing instruction 2 of process P4 Timer 5 Executing instruction 1 of process P4 Timer 6 Put process P4 back in queue with 1 instructions left to execute Getting next process Process P5 has 8 instructions to execute with priority L Executing instruction 7 of process P5 Timer 1 Executing instruction 6 of process P5 Timer 2 Executing instruction 5 of process P5 Timer 3 Put process P5 back in queue with 5 instructions left to execute Getting next process Process P6 has 90 instructions to execute with priority L Executing instruction 8 of process P6 Timer 1 Executing instruction 7 of process P6 Timer 2 Executing instruction 6 of process P6 Timer 3 Put process P6 back in queue with 6 instructions left to execute Getting next process Process P7 has 3 instructions to execute with priority H Executing instruction 2 of process P7 Timer 1 Executing instruction 1 of process P7 Timer 2 Executing instruction 0 of process P7 Timer 3 Process P7 CompleteExplanation / Answer
package com.chegg;
import java.util.Vector;
public class Scheduler extends Thread {
private Vector queue;
private Vector queue1;
private Vector queue2;
private int timeSlice;
private static final int DEFAULT_TIME_SLICE = 2000;
private static final int DEFAULT_MAX_THREADS = 20000;
private boolean[] tids;
private int nextId = 0;
public TCB getMyTcb( ) {
Thread myThread = Thread.currentThread( );
synchronized( queue ) {
for ( int i = 0; i < queue.size( ); i++ ) {
TCB tcb = ( TCB )queue.elementAt( i );
Thread thread = tcb.getThread( );
if ( thread == myThread ) return tcb;
}
}
synchronized( queue1 ) {
for ( int i = 0; i < queue1.size( ); i++ ) {
TCB tcb = ( TCB )queue1.elementAt( i );
Thread thread = tcb.getThread( );
if ( thread == myThread ) return tcb;
}
}
synchronized( queue2 ) {
for ( int i = 0; i < queue2.size( ); i++ ) {
TCB tcb = ( TCB )queue2.elementAt( i );
Thread thread = tcb.getThread( );
if ( thread == myThread ) return tcb;
}
}
return null;
}
private void initTid( int maxThreads ) {
tids = new boolean[maxThreads];
for ( int i = 0; i < maxThreads; i++ ) {
tids[i] = false;
}
}
private int getNewTid( ) {
for ( int i = 0; i < tids.length; i++ ) {
int tentative = ( nextId + i ) % tids.length;
if ( tids[tentative] == false ) {
tids[tentative] = true;
nextId = ( tentative + 1 ) % tids.length;
return tentative;
}
}
return -1;
}
private boolean returnTid( int tid ) {
if ( tid >= 0 && tid < tids.length && tids[tid] == true ) {
tids[tid] = false;
return true;
}
return false;
}
public int getMaxThreads( ) {
return tids.length;
}
public Scheduler( ) {
timeSlice = DEFAULT_TIME_SLICE;
queue = new Vector( );
queue1 = new Vector( );
queue2 = new Vector( );
initTid( DEFAULT_MAX_THREADS );
}
public Scheduler(int quantum) {
timeSlice = quantum;
queue = new Vector( );
queue1 = new Vector( );
queue2 = new Vector( );
initTid( DEFAULT_MAX_THREADS );
}
public Scheduler( int quantum, int maxThreads ) {
timeSlice = quantum;
queue = new Vector( );
queue1 = new Vector( );
queue2 = new Vector( );
initTid( maxThreads );
}
private void schedulerSleep( ) {
try {
Thread.sleep( timeSlice );
} catch ( InterruptedException e ) {
}
}
public TCB addThread( Thread t ) {
TCB parentTcb = getMyTcb( );
int pid = ( parentTcb != null ) ? parentTcb.getTid( ) : -1;
int tid = getNewTid( ); //Get a new TID
TCB tcb = new TCB( t, tid, pid );
queue.add( tcb );
return tcb;
}
public boolean deleteThread( ) {
TCB tcb = getMyTcb( );
if ( tcb!= null ) {
return tcb.setTerminated( );
}
}
public void sleepThread( int milliseconds ) {
try {
sleep( milliseconds );
} catch ( InterruptedException e ) { }
}
public void run( ) {
Thread current = null;
while ( true ) {
try {
if(allQueuesAreEmpty()) continue; //Back to top
if(queue_hasContent()){
if(processqueue(current)) continue; //Process queue
continue; //Back to top
}
if(queue_isEmpty() && queue1_hasContent()){
if(processQueue1(current)) continue; //Process queue1
continue; //Back to top
}
if(queue_isEmpty() && queue1_isEmpty() && queue2_hasContent()){
if(processQueue2(current)) continue; //Process queue2
continue; //Back to top
}
} catch ( NullPointerException e3 ) { };
}
}
private boolean processqueue(Thread current){
TCB currentTCB = (TCB)queue.firstElement( ); //grab queue's first TCB
if(threadIsDead(currentTCB, queue)) return true;
current = currentTCB.getThread( ); //grab thread object
getThreadGoing(current); //start/resume thread
sleepThread(timeSlice/2); //sleep the scheduler
//Move TCBs from queue to queue1
finishProcessingQueue(queue, queue1, current, currentTCB);
return false;
}
private boolean processQueue2(Thread current){
TCB currentTCB = (TCB)queue2.firstElement( ); //grab queue's first TCB
if(threadIsDead(currentTCB, queue2)) return true;
current = currentTCB.getThread( ); //grab thread object
getThreadGoing(current); //start/resume thread
sleepThread(timeSlice/2); //first timeSlice/2
if(queue_hasContent() || queue1_hasContent()) processNewTcb(current);
sleepThread(timeSlice/2); //second timeSlice/2
sleepThread(timeSlice); //last timeSlice
//Keep TCBs in queue2 so that their bursts can be completed.
finishProcessingQueue(queue2, queue2, current, currentTCB);
return false;
} private void processNewTcb(Thread current){
if (current != null && current.isAlive()){
current.suspend(); //put thread to sleep
Thread newProcess = null; //create new thread
processqueue(newProcess); //process new TCB
current.resume(); //resume the old TCB
}
}
private void finishProcessingQueue(Vector myQueue, Vector nextQueue,
Thread current, TCB currentTCB){
synchronized ( myQueue ) {
if ( current != null && current.isAlive( ) ) {
current.suspend(); //put thread to sleep
myQueue.remove( currentTCB ); //remove TCB from queue
nextQueue.add(currentTCB); //add TCB to queue1
}
}
}
private boolean threadIsDead(TCB currentTCB, Vector queue){
if ( currentTCB.getTerminated( ) == true ) { //if TCB is dead, run
queue.remove( currentTCB ); //remove TCB from queue
returnTid( currentTCB.getTid( ) ); //update the Tid array
return true; //return method call
}
return false;
}
private void getThreadGoing(Thread current){
if ( current != null ) { //No null threads!
if ( current.isAlive( ) ) { //if thread is suspened,
current.resume(); //resume the thread
} else { //Otherwise,
current.start( ); //'spin' it up
}
}
}
private boolean queue_hasContent(){ return (queue.size() > 0); }
private boolean queue1_hasContent(){ return (queue1.size() > 0); }
private boolean queue2_hasContent(){ return (queue2.size() > 0); }
private boolean queue_isEmpty(){ return (queue.size() == 0); }
private boolean queue1_isEmpty(){ return (queue1.size() == 0); }
private boolean queue2_isEmpty(){ return (queue2.size() == 0); }
private boolean allQueuesAreEmpty(){
return (queue_isEmpty() && queue1_isEmpty() && queue2_isEmpty());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.