class Scheduler{ //SuperClass for inheritance private: public: // the following is for overridden in all of 4 schedulers. // all are abstract base classes. virtual void insertReady(Event Ready) = 0; virtual bool QEmpty() = 0; // check whether the deque is empty virtual Event toProcess() = 0; }; class FCFS : public Scheduler{ private: public: FCFS(); //constructor deque modeReady; // Initialize the deque /* the following is first come first serve. when calling, always pop the first element in the deque of modeReady */ void insertReady(Event newEvent){ modeReady.push_back(newEvent); } bool QEmpty(){ if (modeReady.size() == 0){ return true; } else{ return false; } } Event toProcess(){ // the first element go processing, and remove it from the deque container Event newProcess = modeReady.front(); // the first ready goes to processing, log it. modeReady.pop_front(); // pop up the current first element from deque. return newProcess; } }; class LCFS : public Scheduler{ private: public: LCFS(); // constructor deque modeReady; // Initializae the queue void insertReady(Event newEvent){ modeReady.push_back(newEvent); } bool QEmpty(){ if (modeReady.size() == 0){ return true; } else{ return false; } } Event toProcess(){ // return the Event newProcess = modeReady.back(); // the last element go processing, and remove it from the deque container modeReady.pop_back(); // pop up the current last element from deque. return newProcess; } }; class RR : public Scheduler{ /* // Concept: // queue Q while(true) { w = Q.front() w = work(w) if(hasProcess()) Q.push(nextPorcess()) if(w != null) Q.push(w) time++ } */ private: public: RR(int timeQuantum); // constructor: as requirement in the document, we need to accept time quantum for RoundRobin // the format is similar to FCFS, we deal the remain details in the main function deque modeReady; void insertReady(Event newEvent){ modeReady.push_back(newEvent); } bool QEmpty(){ if (modeReady.size() == 0){ return true; } else{ return false; } } Event toProcess(){ Event newProcess = modeReady.front(); // the first ready goes to processing, log it. modeReady.pop_front(); // pop up the current first element from deque. return newProcess; } }; class PRIO : public Scheduler{ private: public: PRIO(int timeQuantum); // constructor: as requirement in the document, we need to accept time quantum for RoundRobin // the format is similar to FCFS, we deal the remain details in the main function deque modeReady; void insertReady(Event newEvent){ modeReady.push_back(newEvent); } bool QEmpty(){ if (modeReady.size() == 0){ return true; } else{ return false; } } Event toProcess(){ Event newProcess = modeReady.front(); // the first ready goes to processing, log it. modeReady.pop_front(); // pop up the current first element from deque. return newProcess; } }; class ShortJobFirst : public Scheduler{ private: public: ShortJobFirst(); // Constructor deque modeReady; // Initialize deque void insertReady(Event newEvent){ deque::iterator it; for (it = modeReady.begin() ; it != modeReady.end() ; ++it){ if (newEvent.timeEXE < (*it).timeEXE){ // choose the current shortest one break; } } modeReady.insert(it , newEvent); } bool QEmpty(){ if (modeReady.size() == 0){ return true; } else{ return false; } } // now, the prior element in container deque is the shortest, it shoud be pushed to process first. Event toProcess(){ Event newProcess = modeReady.front(); deque::iterator it; /* // test // std::cout << "mydeque contains:"; for (std::deque::iterator it = mydeque.begin(); it!=mydeque.end(); ++it){ std::cout << ' ' << *it; std::cout << '\n'; } */ modeReady.erase(modeReady.begin()); return newProcess; } }; Scheduler* SchInitial(string& schAlgo , int& quantum , string& sCMD) string sCMD = "F"; // Default: 'F':= FCFS. double cpuTimeSum = 0; // total time of CPU running. Initialization: 0. int quantum = 2147483647; // Default: the max integer. int main(int argc , char* argv[]){ string schAlgo; Scheduler* scheduler; // since the scheduler is abstract, so we need to set a pointer for it. scheduler = SchInitial(schAlgo , quantum , sCMD); } Scheduler* SchInitial(string& schAlgo , int& quantum , string& sCMD){ if (sCMD == "F"){ schAlgo = "FCFS"; return new FCFS(); } else if (sCMD == "L"){ schAlgo = "LCFS"; return new LCFS(); } else if (sCMD == "S"){ schAlgo = "ShortJobFirst"; return new ShortJobFirst(); } else if (sCMD == "R10"){ schAlgo = "RR"; quantum = atoi(sCMD.substr(1).c_str()); return new RR(quantum); } else { schAlgo = "PRIO"; quantum = atoi(sCMD.substr(1).c_str()); return new PRIO(quantum); } }