Implementing Preemptive Round-Robin Scheduling: Classes Timer and RRScheduler, Assignments of Computer Science

The implementation details for the timer and rrscheduler classes in the context of preemptive round-robbin scheduling. The timer class manages timer events and handles their expiration, while the rrscheduler class extends the scheduler class and implements round-robin scheduling with a quantum timer. Students are required to implement these classes as part of a machine problem assignment.

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-zho
koofers-user-zho 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC–313 / Spring 2008 Machine Problem 2 / Step 3
Machine Problem 2: Simple Thread Scheduling
Total Machine Problem: 100 points
This Handout is for Step 3 of MP2
Due date: Check CSNET
Introduction
In Step 1 of this machine problem you implemented a simple task system that uses the system (or
the pthread library) scheduler for scheduling.
In Step 2 you added scheduling support to the task system.
In Step 3 we lay the foundation to extend the scheduling support to include preemptive round-
robin scheduling. The Round-Robin scheduler will be implemented as a class RRScheduler, which
is derived from class Scheduler. The class is defined as follows:
class RRScheduler : public Scheduler {
protected:
long time_quantum; /* in musec */
Timer * quantum_timer;
friend class EndOfQuantumTimer;
virtual void handle_end_of_quantum();
public:
RRScheduler(char _name[], long _quantum);
/* Instantiate a new scheduler. The quantum is given in musec. */
~RRScheduler();
/* Don’t need to implement for now... */
/* -- START SCHEDULING THE TASKS */
virtual int Kick_Off();
/* -- SCHEDULING OPERATIONS */
virtual int Start(Schedulable * _task);
/* Start scheduling this task. See Scheduler::Start(). */
virtual int Yield();
/* The calling task gives up the CPU.
Basically the same as Scheduler::Yield(), but we need to
take care of the quantum timer first. */
virtual int Resume(Schedulable * _task);
/* Resume the given _task. */
};
The quantum management in the scheduler is handled by an object of class EndOfQuantumTimer,
which is derived from class Timer. The forced context switch is called from within the event handler
of the end-of-quantum event handler.
class EndOfQuantumTimer : public Timer {
private:
RRScheduler * sched;
public:
EndOfQuantumTimer(RRScheduler * _sched) {
sched = _sched;
}
virtual void event_handler() {
printf("END-OF-QUANTUM TIMER: ");
Page 1
pf3

Partial preview of the text

Download Implementing Preemptive Round-Robin Scheduling: Classes Timer and RRScheduler and more Assignments Computer Science in PDF only on Docsity!

Machine Problem 2: Simple Thread Scheduling

Total Machine Problem: 100 points

This Handout is for Step 3 of MP

Due date: Check CSNET

Introduction

In Step 1 of this machine problem you implemented a simple task system that uses the system (or

the pthread library) scheduler for scheduling.

In Step 2 you added scheduling support to the task system.

In Step 3 we lay the foundation to extend the scheduling support to include preemptive round-

robin scheduling. The Round-Robin scheduler will be implemented as a class RRScheduler, which

is derived from class Scheduler. The class is defined as follows:

class RRScheduler : public Scheduler { protected: long time_quantum; /* in musec */ Timer * quantum_timer; friend class EndOfQuantumTimer; virtual void handle_end_of_quantum();

public: RRScheduler(char _name[], long _quantum); /* Instantiate a new scheduler. The quantum is given in musec. / ~RRScheduler(); / Don’t need to implement for now... / / -- START SCHEDULING THE TASKS / virtual int Kick_Off(); / -- SCHEDULING OPERATIONS / virtual int Start(Schedulable * _task); / Start scheduling this task. See Scheduler::Start(). / virtual int Yield(); / The calling task gives up the CPU. Basically the same as Scheduler::Yield(), but we need to take care of the quantum timer first. / virtual int Resume(Schedulable * _task); / Resume the given _task. */ };

The quantum management in the scheduler is handled by an object of class EndOfQuantumTimer,

which is derived from class Timer. The forced context switch is called from within the event handler

of the end-of-quantum event handler.

class EndOfQuantumTimer : public Timer { private: RRScheduler * sched; public: EndOfQuantumTimer(RRScheduler * _sched) { sched = _sched; } virtual void event_handler() { printf("END-OF-QUANTUM TIMER: ");

printf("\n"); sched->handle_end_of_quantum(); } };

The EndOfQuantumTimer is derived from class Timer, which has the following interface:

class Timer { private: static long clock_interval; /* in musec / / This is the period at which the low-level timer fires and triggers the check to see if any timer events have expired and need to be handled. / static bool compare_timers(const Timer * _t1, const Timer * _t2); / Compares whether timer _t1 is earlier than timer _t2. This function may come in handy, for example, whenever timers need to be inserted into an ordered queue. / static void * TimerHandlerThread(void * args); / This function is called by the separate thread that handles the low-level timer interrupts and checks whether timers events have expired and need to be executed. / / WHEN SHOULD THE TIMER FIRE? / timeval tv; public: static int Init(long _clock_interval_musec); / Initialize the timer handling. This should be called by the main thread early on, certainly before it creates the first timers. An implementation could look as follows: First, the main thread masks the SIGALARM (or SIGVTALARM) signals; this should be done before any tasks are created, in order for the tasks to have the signals masked. Then, method "Init()" would create a new timer thread, which would process the requests in the timer queue. For this thread, the signal would be unblocked. / Timer(); / Construct timer that calles "Event_Handler" whenever it fires. / virtual void Set(long _imusec); / Set this timer to fire after time ’_imusec’ from now. / virtual int Clear(); / Cancel the timer. / virtual void Event_Handler() = NULL; / This function is called when the timer expires. */ };

In our implementation we set timers by adding timer events to a timer event queue. A low-level (i.e.

POSIX) timer then periodically issue timer signals, which are captured by a timer signal handler.

The handler then compares the current time with the time associated with the events in the timer

event queue. If the time for any event has passed, the event handler for the associated timer is

called.

The Assignment (Step 3)

You are to implement the following classes:

  • class Timer: submit a file named timer.C that implements the class defined in timer.H.