

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 3
This page cannot be seen from the preview
Don't miss anything!


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. */ };
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(); } };
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. */ };