The Artima Developer Community
Sponsored Link

Java Answers Forum
Round Robin Scheduler.

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Albert Lee

Posts: 1
Nickname: roundrobin
Registered: Jan, 2003

Round Robin Scheduler. Posted: Jan 28, 2003 2:49 PM
Reply to this message Reply
Advertisement
Hi, can anyone help me? I was given 4 java files, which produce a Round Robin Scheduler using 1 queue. I have to do the following:


Modify the given Java based round robin scheduler to be a multilevel queue scheduler. In this scheme, jobs enter their assigned queue and do not move to other queues. Only when queue N is empty do we begin executing a job from queue N+1. A running job from queue N+1 will be put back in its queue (pre-empted) if a job arrives in queue N. In this way, assigning a job to a queue is like assigning a priority. Jobs within a given queue are scheduled round robin with a single time slice.

Modify the run routine to print out current scheduling info and queue status each time slice. (You can make the time slice large so you can watch the execution in real time.) Modify TestScheduler() to read an input text file, one line every 2 time slices. The input file format is as follows:

The first line specifies the number of queues (1 to 4). The next lines specify a new thread to be added to one of the queues. If there are 4 queues, they are designated Q1, Q2, Q3 and Q4, with Q1 having the highest priority. The format of these lines is thread ID, input queue number, number of time slices required to execute.

Example:
3
1 3 3
2 2 4
3 1 1
4 1 2

Explanation: This is a system with 3 queues. The first thread enters Q3 and requires 3 time slices. After 2 time slices, it is interrupted by the second thread entering Q2, etc.

The output of the system begins:

John Smith?s system started with 3 queues
time=1, read input, T1 enters Q3, time remaining 3
Q1:
Q2:
Q3: T1-3
T1 runs

time=2
Q1:
Q2:
Q3: T1-2
T1 runs

time=3, read input, T2 enters Q2, time remaining 4
Q1:
Q2: T2-4
Q3: T1-1
T2 running

time=4
Q1:
Q2: T2-3
Q3: T1-1
T2 runs

time=5 read input, T3 enters Q1, time remaining 1
Q1: T3-1
Q2: T2-3
Q3: T1-1
T3 runs

time=6
Q1:
Q2: T2-3
Q3: T1-1
T2 runs

I also need to change the code to run correctly in 1.3. The current files are geared towards 1.1
-----CircularList.java-------

/**
* CircularList.java
*
* This class implements a circular list using the Vector class
* note that elements in a vector with n elements are numbered 0 .. (n-1)
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/

import java.util.*;

public class CircularList
{
private Vector List;
private int index;

public CircularList() {
List = new Vector(10);
index = 0;
}

/**
* this method returns the next element in the list.
* @return Object
*/
public Object getNext() {
Object nextElement = null;
int lastElement;

if (!List.isEmpty() ) {
if (index == List.size() )
index = 0;

nextElement = List.elementAt(index);

++index;
}

return nextElement;
}

/**
* this method adds an item to the list
* @return void
*/
public void addItem(Object t) {
List.addElement(t);
}

}

-----Scheduler.java------

/**
* Scheduler.java
*
* This class is a simple round-robin scheduler.
* The idea for this scheduler came from "Java Threads"
* by Oaks and Wong (Oreilly, 1999).
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/


public class Scheduler extends Thread
{
private CircularList queue;
private int timeSlice;
private static final int DEFAULT_TIME_SLICE = 1000; // 1 second

public Scheduler() {
timeSlice = DEFAULT_TIME_SLICE;
queue = new CircularList();
}

public Scheduler(int quantum) {
timeSlice = quantum;
queue = new CircularList();
}

/**
* adds a thread to the queue
* @return void
*/
public void addThread(Thread t) {
t.setPriority(2);

queue.addItem(t);
}

/**
* this method puts the scheduler to sleep for a time quantum
* @return void
*/
private void schedulerSleep() {
try {
Thread.sleep(timeSlice);
} catch (InterruptedException e) { };
}


public void run() {
Thread current;

// set the priority of the scheduler to the highest priority
this.setPriority(6);

while (true) {
try {
current = (Thread)queue.getNext();

if ( (current != null) && (current.isAlive()) ) {
current.setPriority(4);

schedulerSleep();

System.out.println("* * * Context Switch * * * ");

current.setPriority(2);
}

} catch (NullPointerException e3) { } ;
}
}
}


------TestScheduler.java-------
/**
* TestScheduler.java
*
* This program demonstrates how the scheduler operates.
* This creates the scheduler and then the three example threads.
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/

public class TestScheduler
{
public static void main(String args[]) {
/**
* This must run at the highest priority to ensure that
* it can create the scheduler and the example threads.
* If it did not run at the highest priority, it is possible
* that the scheduler could preempt this and not allow it to
* create the example threads.
*/
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

Scheduler CPUScheduler = new Scheduler();

CPUScheduler.start();

TestThread t1 = new TestThread("Thread 1");
t1.start();
CPUScheduler.addThread(t1);

TestThread t2 = new TestThread("Thread 2");
t2.start();
CPUScheduler.addThread(t2);

TestThread t3 = new TestThread("Thread 3");
t3.start();
CPUScheduler.addThread(t3);
}
}

-------TestThread.java---------
/**
* TestThread.java
*
* This thread is used to demonstrate how the scheduler operates.
* This thread runs forever, periodically displaying its name.
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/

class TestThread extends Thread
{
private String name;

public TestThread(String id) {
name = id;
}

public void run() {
/*
* The thread does something
**/
while (true) {
for (int i = 0; i < 500000; i++)
;
System.out.println("I am thread " + name);
}
}
}


Any help would be very much appreciated, thanks.

Topic: Clear screen Previous Topic   Next Topic Topic: Filesearcher

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use