-
Round Robin Queue를 이용한 Process SchedulerC++ 2014. 2. 19. 06:34
######################## RoundRobin.h ##############################################
#include <vector>
template <typename T>
class RoundRobin
{
private:
std::vector<T> mElement;
typename std::vector<T>::iterator mCurElement;
RoundRobin(const RoundRobin& src);
RoundRobin& operator=(const RoundRobin& rhs);
public:
RoundRobin(int numExpected=0);
virtual ~RoundRobin(void);
void add(const T& element);
void remove(const T& element);
T& getNext() ;
};
template <typename T>
RoundRobin<T>::RoundRobin(int numExpected)
{
mElement.reserve(numExpected);
mCurElement = mElement.begin();
}
template <typename T>
RoundRobin<T>::~RoundRobin(void)
{
;
}
template <typename T>
void RoundRobin<T>::add(const T& element)
{
int pos = mCurElement - mElement.begin();
mElement.push_back(element);
mCurElement = mElement.begin()+pos;
}
template <typename T>
void RoundRobin<T>::remove(const T& element)
{
for (auto i=mElement.begin();i!=mElement.end();++i) {
if (*i==element) { ///// Process class에서 반드시 operator overriding이 필요
int newPos;
if (i<mCurElement) {
newPos = mCurElement - mElement.begin() - 1;
}
else
newPos = mCurElement - mElement.begin();
mElement.erase(i);
mCurElement = newPos + mElement.begin();
if (mCurElement==mElement.end()) {
mCurElement=mElement.begin();
}
return;
}
}
}
template <typename T>
T& RoundRobin<T>::getNext() throw(std::out_of_range)
{
if (mElement.empty()==true) {
throw out_of_range("No element in the list");
}
T& ret_element = *mCurElement;
if (++mCurElement==mElement.end())
mCurElement=mElement.begin();
return ret_element;
}
/////////////////// Scheduler.h ////////////////////////////////////////
#include "RoundRobin.h"
#include "Process.h"
class Scheduler
{
protected:
RoundRobin<Process> rrTable;
public:
Scheduler(/*const vector<Process>& processes*/);
void add(const Process& process);
void remove(const Process& process);
void scheduleTimeSlice();
virtual ~Scheduler(void);
};
################ Scheduler.cpp #############################
#include "Scheduler.h"
#include "Process.h"
#include <iostream>
Scheduler::Scheduler(/*const vector<Process>& processes*/)
{
rrTable;
}
Scheduler::~Scheduler(void)
{
}
void Scheduler::add(const Process& process)
{
rrTable.add(process);
}
void Scheduler::remove(const Process& process)
{
rrTable.remove(process);
}
void Scheduler::scheduleTimeSlice()
{
try {
rrTable.getNext().getProcess();
}
catch (const out_of_range&) {
cerr << "No more process to schedule" << endl;
}
}
######################## Process.h ##################################
#include <string>
using namespace std;
class Process
{
private:
string mName;
public:
Process(const string& name);
Process(const Process& src);
Process& operator=(const Process& rhs);
bool operator==(const Process& rhs);
void getProcess();
virtual ~Process(void);
};
##################### Process.cpp ######################################
#include "Process.h"
#include <iostream>
using namespace std;
Process::Process(const string& name) : mName(name)
{
}
Process::~Process(void)
{
}
Process::Process(const Process& src) : mName(src.mName)
{
;
}
Process& Process::operator=(const Process& rhs)
{
if (&rhs == this) return *this;
mName = rhs.mName;
return *this;
}
bool Process::operator==(const Process& rhs)
{
return (mName == rhs.mName);
}
void Process::getProcess()
{
std::cout << "Process " << mName << endl;
}
///////////// main.cpp
void main()
{
vector<Process> processes;
Scheduler sched;
Process p1("1");
processes.push_back(p1);
sched.add(p1);
Process p2("2");
processes.push_back(p2);
sched.add(p2);
Process p3("3");
processes.push_back(p3);
sched.add(p3);
for (int i=0;i<4;i++) {
sched.scheduleTimeSlice();
}
}
'C++' 카테고리의 다른 글
STL map 의 정리 (0) 2014.02.20 Packet Queue Example Code (0) 2014.02.20 Vector constructor and descructor (0) 2014.02.17 STL Container 선택 (0) 2014.02.17 Inheritance 에서 copy operator & assignment operator (0) 2014.02.15