生活随笔
收集整理的這篇文章主要介紹了
高响应比优先调度算法(HRRN)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
BOOM,困到不行,這個寫完就睡覺了,今天好像有點感冒 ,翹了晚上的課一直睡到10點起來,睡不著在寫代碼,現在又困了
高響應比算法,是一種動態調整優先級的算法,在上面介紹的PSA算法中,給每個作業安排一個優先級后,始終這個優先級不再改變,這有些不合理。
因為可能造成一個低優先級作業始終得不到執行。
為了解決這個問題,HRRN算法每次都計算作業的優先級,隨著作業等待時間的變長,優先級不斷的提高,所以能夠得到更快的執行。
這個優先級可以描述為: 優先級 = (作業已等待時間 + 作業的服務時間) / 作業的服務時間
從上式可以看到,作業的服務時間是固定的, 優先級隨著已等待時間的提高而變大
//main.cpp
#include "HRRN.h"int main()
{std::vector<PCB> PCBList;//輸入作業信息InputPCB(PCBList);//HRRN算法HRRN(PCBList);//顯示結果show(PCBList);return 0;
}//HRRN.h
#ifndef HRRN_H_
#define HRRN_H_#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>//作業結構體
typedef struct PCB
{int ID; //標識符double Level; //優先級int ComeTime; //到達時間int ServerTime; //服務時間int FinishTime; //完成時間int TurnoverTime; //周轉時間double WeightedTurnoverTime; //帶權周轉時間
}PCB;/*
函數功能:輸入作業信息
參數說明:
PCBList std::vector<PCB>& PCB鏈
*/
void InputPCB(std::vector<PCB> &PCBList);/*
函數功能:HRRN算法
參數說明:
PCBList std::vector<PCB>& PCB鏈
*/
void HRRN(std::vector<PCB> &PCBList);/*
函數功能:計算優先級
參數說明:
b std::vector<PCB>::iterator 起始位置
e std::vector<PCB>::iterator 結束位置
CurTime int 當前時間
*/
void CalPriority(std::vector<PCB>::iterator b, std::vector<PCB>::iterator e, int CurTime);/*
函數功能:顯示結果
參數說明:
PCBList std::vector<PCB>& PCB鏈
*/
void show(std::vector<PCB> &PCBList);/*
函數功能:比較函數,用于sort(),按ComeTime升序排列
參數說明:
p1 const PCB& PCB
p2 const PCB& PCB
*/
bool CmpByComeTime(const PCB &p1, const PCB &p2);/*
函數功能:比較函數,用于sort(),按Level降序排列
參數說明:
p1 const PCB& PCB
p2 const PCB& PCB
*/
bool CmpByLevel(const PCB &p1, const PCB &p2);#endif//HRRN.cpp
#include "HRRN.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;elsebreak;} while (true);
}//HRRN算法
void HRRN(std::vector<PCB> &PCBList)
{std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime); //按到達時間排序//同時到達的按優先級降序排序,決定首先運行的作業int i = 1;std::vector<PCB>::iterator it = PCBList.begin() + 1;while ((*it).ComeTime == (*(it - 1)).ComeTime){++i;++it;}CalPriority(PCBList.begin(), PCBList.begin() + i, 0); //計算優先級std::sort(PCBList.begin(), PCBList.begin() + i, CmpByLevel);int FinishTime = -1;for (it = PCBList.begin(); it < PCBList.end(); ++it){if ((*it).ComeTime >= FinishTime) //沒有作業正在運行,取隊首作業運行(*it).FinishTime = (*it).ComeTime + (*it).ServerTime;else //有作業正在運行,等待作業完畢,此作業再運行(*it).FinishTime = FinishTime + (*it).ServerTime;(*it).TurnoverTime = (*it).FinishTime - (*it).ComeTime;(*it).WeightedTurnoverTime = (double)(*it).TurnoverTime / (*it).ServerTime;FinishTime = (*it).FinishTime;//在一個作業運行期間,如果有其他作業到達,將他們按照優先級降序排列i = 1;while ((it + i) < PCBList.end() && (*(it + i)).ComeTime <= FinishTime)++i;CalPriority(it + 1, it + i, FinishTime);std::sort(it + 1, it + i, CmpByLevel);}std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime); //重新排列,用于顯示結果
}//計算優先級
void CalPriority(std::vector<PCB>::iterator b, std::vector<PCB>::iterator e, int CurTime)
{while (b < e){(*b).Level = (double)((*b).ServerTime + (CurTime - (*b).ComeTime)) / (*b).ServerTime;++b;}
}//顯示結果
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;
}//比較函數,按ComeTime升序排列
bool CmpByComeTime(const PCB &p1, const PCB &p2)
{return p1.ComeTime < p2.ComeTime;
}//比較函數,按Level降序排列
bool CmpByLevel(const PCB &p1, const PCB &p2)
{return p1.Level > p2.Level;
}
總結
以上是生活随笔為你收集整理的高响应比优先调度算法(HRRN)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。