先來先服務(FCFS)演算法
阿新 • • 發佈:2018-12-26
快期末了在複習作業系統,順便把實驗課上寫的演算法重新寫一遍
先來先服務演算法非常簡單,當作業來了,先來先執行,後來的按到達時間的先後排在就緒佇列上,每次取隊首元素執行。
實驗環境是WIN7 64位+Visual Studio 2015
//main.cpp #include "FCFS.h" int main() { std::vector<PCB> PCBList; //輸入作業資訊 InputPCB(PCBList); //FCFS演算法 FCFS(PCBList); //顯示結果 show(PCBList); return 0; } //FCFS.h #ifndef FCFS_H_ #define FCFS_H_ #include <iostream> #include <iomanip> #include <vector> #include <queue> //作業結構體 typedef struct PCB { int ID; //識別符號 int ComeTime; //到達時間 int ServerTime; //服務時間 int FinishTime; //完成時間 int TurnoverTime; //週轉時間 double WeightedTurnoverTime; //帶權週轉時間 }PCB; /* 函式功能:輸入作業資訊 引數說明: PCBList std::vector<PCB>& PCB鏈 */ void InputPCB(std::vector<PCB> &PCBList); /* 函式功能:FCFS演算法 引數說明: PCBList std::vector<PCB>& PCB鏈 */ void FCFS(std::vector<PCB> &PCBList); /* 函式功能:顯示結果 引數說明: PCBList std::vector<PCB>& PCB鏈 */ void show(std::vector<PCB> &PCBList); /* 函式功能:比較函式,用於sort(),按ComeTime升序排列 引數說明: p1 const PCB& PCB p2 const PCB& PCB */ bool cmp(const PCB &p1, const PCB &p2); #endif //FCFS.cpp #include "FCFS.h" void InputPCB(std::vector<PCB> &PCBList) { do { PCB temp; std::cout << "輸入識別符號: "; std::cin >> temp.ID; std::cout << "輸入到達時間: "; std::cin >> temp.ComeTime; std::cout << "輸入服務時間: "; std::cin >> temp.ServerTime; PCBList.push_back(temp); std::cout << "繼續輸入?Y/N: "; char ans; std::cin >> ans; if ('Y' == ans || 'y' == ans) continue; else break; } while (true); } void FCFS(std::vector<PCB> &PCBList) { std::sort(PCBList.begin(), PCBList.end(), cmp); //按升序排列ComeTime int FinishTime = -1; //上一個作業的完成時間 for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) { if ((*it).ComeTime < FinishTime) //作業到達時,上一個作業還沒有結束 (*it).FinishTime = (*(it - 1)).FinishTime + (*it).ServerTime; else (*it).FinishTime = (*it).ComeTime + (*it).ServerTime; (*it).TurnoverTime = (*it).FinishTime - (*it).ComeTime; (*it).WeightedTurnoverTime = (double)(*it).TurnoverTime / (*it).ServerTime; FinishTime = (*it).FinishTime; } } void show(std::vector<PCB> &PCBList) { int SumTurnoverTime = 0; double SumWeightedTurnoverTime = 0; std::cout.setf(std::ios::left); std::cout << std::setw(20) << "識別符號"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).ID; std::cout << std::endl; std::cout << std::setw(20) << "到達時間"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).ComeTime; std::cout << std::endl; std::cout << std::setw(20) << "服務時間"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).ServerTime; std::cout << std::endl; std::cout << std::setw(20) << "完成時間"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).FinishTime; std::cout << std::endl; std::cout << std::setw(20) << "週轉時間"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) { std::cout << std::setw(5) << (*it).TurnoverTime; SumTurnoverTime += (*it).TurnoverTime;; } std::cout << std::endl; std::cout << std::setw(20) << "帶權週轉時間"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) { std::cout << std::setw(5) << (*it).WeightedTurnoverTime; SumWeightedTurnoverTime += (*it).WeightedTurnoverTime;; } std::cout << std::endl; std::cout << "平均週轉時間: " << (double)SumTurnoverTime / PCBList.size() << std::endl; std::cout << "平均帶權週轉時間: " << SumWeightedTurnoverTime / PCBList.size() << std::endl; } bool cmp(const PCB &p1, const PCB &p2) { return p1.ComeTime < p2.ComeTime; }