最高响应比作业优先算法是对FCFS方式和SJF方式的一种综合平衡响应比R定义为系统对作业的响应时间与作业要求运行时间的比值
R=响应时间/ 要求运行时间
=(作业等待时间+需运行时间)/ 需运行时间
=1+已等待时间/ 需运行时间
=1+W/T
响应比R不仅是要求运行时间的函数,而且还是等待时间的函数。
由于R与要求运行时间成反比,故对短作业是有利的,另一方面,因R与等待时间成正比,故长作业随着其等待时间的增长,也可获的较高的相应比。这就克服了短作业优先数法的缺点,既照顾了先来者,又优待了短作业。
作业 | 进入时刻 | 运行时间 | 开始时刻 | 完成时刻 | 周转时间 | 带权周转 |
---|---|---|---|---|---|---|
1 | 8.00 | 2.00 | 8.00 | 10.00 | 2.00 | 1.00 |
2 | 8.50 | 0.50 | 10.01 | 10.60 | 2.10 | 4.20 |
3 | 9.00 | 0.10 | 10.00 | 10.10 | 1.10 | 11.00 |
4 | 9.50 | 0.20 | 10.60 | 10.80 | 1.30 | 6.50 |
平均周转时间1.625 h
带权周转时间 5.625
我们这里只考虑1个cpu资源,其他资源不考虑
程序采用键盘输入,输入格式为:
K
TJ1 YS1
……
TJK YSK
其中K是作业数(>0),TJi提交时间,YSi (i=1~K)是作业预计的运行时间(以分钟计)TJ的输入格式是XXYY,其中XX是时,YY是分,如10点28分,输入为1028。但内部计算要以60进制来算。要求输出按照作业调度的先后次序输出结果,每行为一个作业状态,从左到右分别是调度次序,作业号,调度时间,周转时间和带权周转时间,最后一行输出两个数,第一为平均周转时间,第二为平均带权周转时间。
总的思路是在代码中模拟一个cpu时间序列。通过从第一个作业进入开始循环模拟时间的流失,代码如下
int HRRN::operating() {
int doing_no = getStMin(getMinArrTime(w));
cout << "[<";
for (int clock = init_time, flag = 1 ;; ++clock) { // 通过从第一个作业进入开始循环模拟时间的流失
for (int i = 0; i < size; ++i) {
if (clock > w[i].arrival_time && doing_no != i && !w[i].if_finished) {
w[i].waiting_time++;
}
}
if (w[doing_no].arrival_time + w[doing_no].duration + w[doing_no].waiting_time == clock) {
w[doing_no].if_finished = true;
w[doing_no].order_num = flag;flag++;
for (int i = 0; i < size; ++i) {
if (!w[i].if_finished) {
getRp(w[i]);
}
}
cout <<setfill('=') << setw((int) 30/size) << "=";
Sleep(500);
if (allFinished(w)) {
getTurnaroundTime();
getWTurnaroundTime();
getATT();
getAWTT();
cout << ">]\nAll process are finished!!" << endl;
Sleep(750);
system("cls");
cout << ".." << endl;
Sleep(750);
cout << "..." << endl;
cout.put('\n');
Sleep(750);
return 0;
} else {
doing_no = getMaxRpNo(w);
}
// is_doing = 1;
}
}
return 0;
}
以下是文件结构:
.
|-- HRRN.cpp
|-- HRRN.h
|-- func.cpp
|-- func.h
|-- input.txt //测试用例
|-- main.cpp
| - - HRRN.cpp
/**
* HRRN.cpp
*/
#include "HRRN.h"
#include "func.h"
using namespace std;
HRRN::HRRN(vector<int> &arrival_times, vector<int> &duration) {
if (arrival_times.size() == duration.size()) {
size = arrival_times.size();
w = new work_attribute[size];
for (int i = 0; i < size; i++) {
w[i].duration = duration[i];
w[i].arrival_time = getMinute(arrival_times[i]);
w[i].if_finished = false;
w[i].no = i + 1;
}
}
initTime();
getTotalTimeSeries();
}
int HRRN::operating() {
int doing_no = getStMin(getMinArrTime(w));
cout << "[<";
for (int clock = init_time, flag = 1 ;; ++clock) {
for (int i = 0; i < size; ++i) {
if (clock > w[i].arrival_time && doing_no != i && !w[i].if_finished) {
w[i].waiting_time++;
}
}
if (w[doing_no].arrival_time + w[doing_no].duration + w[doing_no].waiting_time == clock) {
w[doing_no].if_finished = true;
w[doing_no].order_num = flag;flag++;
for (int i = 0; i < size; ++i) {
if (!w[i].if_finished) {
getRp(w[i]);
}
}
cout <<setfill('=') << setw((int) 30/size) << "=";
Sleep(500);
if (allFinished(w)) {
getTurnaroundTime();
getWTurnaroundTime();
getATT();
getAWTT();
cout << ">]\nAll process are finished!!" << endl;
Sleep(750);
system("cls");
cout << ".." << endl;
Sleep(750);
cout << "..." << endl;
cout.put('\n');
Sleep(750);
return 0;
} else {
doing_no = getMaxRpNo(w);
}
// is_doing = 1;
}
}
return 0;
}
int HRRN::getRp(work_attribute &wa) {
wa.Rp = (double )(wa.waiting_time) / (double )wa.duration + 1;
return 0;
}
int HRRN::getMaxRpNo(work_attribute *wo) const {
int maxRp = 0;
for (int i = 0; i < size; ++i) {
if (wo[maxRp].Rp < wo[i].Rp && !wo[i].if_finished) {
maxRp = i;
}
}
return maxRp;
}
int HRRN::initTime() {
getMinArrTime(w);
return 0;
}
bool HRRN::allFinished(work_attribute *wo) {
for (int i = 0; i < size; ++i) {
if (!w[i].if_finished)
return false;
}
return true;
}
int HRRN::getStMin(int min) {
int s = min;
for (int i = 0; i < size; ++i) {
if (w[i].arrival_time == w[min].arrival_time) {
if (w[i].duration < w[s].duration) {
s = i;
}
}
}
return s;
}
int HRRN::getMinArrTime(work_attribute *wo) {
int min_time = 0;
for (int i = 0; i < size; ++i) {
if (w[min_time].Rp > w[i].Rp && !w[i].if_finished) {
min_time = i;
}
}
return min_time;
}
int HRRN::getTotalTimeSeries() {
longest_time = getMinute(6060);
return longest_time;
}
//int HRRN::exchange(work_attribute &w1, work_attribute &w2) {
// work_attribute temp;
// temp = w1;
// w1 = w2;
// w2 = temp;
// return 0;
//}
ostream &operator<<(ostream &out, work_attribute wo) {
out << "Process no: \t" << wo.no << endl;
out << "Arrival time: \t" << wo.arrival_time << endl;
out << "Duration: \t" << wo.duration << endl;
out << "Waiting time: \t" << wo.waiting_time << endl;
out << "Finished? : \t" << wo.if_finished << endl;
out << "-=Rp=-:\t\t" << wo.Rp << endl;
return out;
}
ostream &operator<<(ostream &out, HRRN hrrn) {
/**
* @doc:
* 周转时间=作业完成时刻—作业到达时刻;
* 带权周转时间=周转时间/服务时间;
*/
if (hrrn.size != 0) {
cout << "Ord: Order number" << endl;
cout << "No.: number" << "\n";
cout << "St: Scheduling time" << endl;
cout << "Tt: Turnaround time" << "\n";
cout << "wTt: Weighted turnaround time" << "\n";
cout << "------------------------------------------------" << endl;
cout << "Ord" << "\t";
cout << "No." << "\t";
cout << "St" << "\t";
cout << "Tt(min)" << "\t";
cout << "wTt(min)" << "\n";
work_attribute *w = hrrn.w;
for (int i = 0; i < hrrn.size; ++i, w++) {
out << (*w).order_num << "\t";
out << (*w).no << "\t";
out << setfill('0') << setw(4) << getFormatTime((*w).arrival_time + (*w).waiting_time);
out << fixed << setprecision(2);
out << "\t"
<< (*w).turnaround_time << "\t" << (*w).w_turnaround_time << endl;
}
out << "------------------------------------------------" << endl;
out << "Average turnaround time: " << hrrn.ATT << endl;
out << "Average turnaround time with weights: " << hrrn.AWTT << endl;
} else {
out << "ERROR: The Size is '0'!" << endl;
}
return out;
}
void HRRN::show() {
for (int i = 0; i < size; ++i) {
cout << w[i] << endl;
}
}
void HRRN::showInTale() {
for (int i = 0; i < size; ++i) {
cout << w[i] << endl;
}
}
double HRRN::getTurnaroundTime() {
/**
* 周转时间=作业完成时刻—作业到达时刻;
*/
for (int i = 0; i < size; ++i) {
w[i].turnaround_time = w[i].duration + w[i].waiting_time;
}
return 0;
}
double HRRN::getWTurnaroundTime() {
for (int i = 0; i < size; ++i) {
w[i].w_turnaround_time = (double)w[i].waiting_time / (double)w[i].duration + 1;
}
return 0;
}
double HRRN::getATT() {
double TT = 0;
for (int i = 0; i < size; ++i) {
TT += w[i].turnaround_time;
}
ATT = TT / size;
return ATT;
}
double HRRN::getAWTT() {
double WTT = 0;
for (int i = 0; i < size; ++i) {
WTT += w[i].w_turnaround_time;
}
AWTT = WTT / size;
return AWTT;
}
HRRN.h
//
// Created by pce on 2021/4/8.
//
#ifndef EXPERIMENT_01_HRRN_H
#define EXPERIMENT_01_HRRN_H
#include
#include
#include
#include
#include
using namespace std;
struct work_attribute {
int no{};
int order_num{};
int arrival_time{};
int duration{};
double Rp = 0;
int waiting_time = 0;
bool if_finished = false;
double turnaround_time{};
double w_turnaround_time{};
// int end_time{};
// work_attribute *next{};
};
class HRRN {
work_attribute *w;
int longest_time{};
int init_time{};
int size;
double ATT{};
double AWTT{};
public:
HRRN (vector<int> &arrival_times, vector<int> &duration);
static int getRp(work_attribute &wa);
int operating();
int initTime();
int getTotalTimeSeries();
int getMaxRpNo(work_attribute *wo) const;
// static int exchange(work_attribute &, work_attribute &);
friend ostream & operator<< (ostream &, work_attribute wo);
friend ostream & operator<< (ostream &out, HRRN hrrn);
void show();
int getMinArrTime(work_attribute *wo);
bool allFinished(work_attribute *wo);
void showInTale();
double getTurnaroundTime();
double getWTurnaroundTime();
double getATT();
double getAWTT();
int getStMin(int min);
};
#endif //EXPERIMENT_01_HRRN_H
func.cpp
#include "func.h"
void getA_DTime(vector<int> &arrival_times, vector<int> &duration, int k)
{
cout << "--Enter the information--" << endl;
cout.put('\n');
arrival_times.reserve(k);
duration.reserve(k);
for (int i = 0; i < k; ++i)
{
cout << "==" << i + 1 << "==" << endl;
cout << "Arrival time(XXYY): " << endl;
int temp;
cin >> temp;
cin.ignore();
arrival_times.push_back(temp);
cout << "Duration(min):" << endl;
cin >> temp;
cin.ignore();
duration.push_back(temp);
cout.put('\n');
}
}
int to_10(int target) {
int temp = target;
vector<int> copy;
while (temp != 0) {
int m = (temp % 10);
copy.push_back(m);
temp /= 10;
}
// reverse(copy.begin(), copy.end());
int decimal = 0, i = -1;
for (int & it : copy) {
i++;
decimal += (it * intPow(60, i));
}
return decimal;
}
int to_60(int origin) {
int target = 0;
int copy = origin;
int i = -1;
do {
i++;
target += (copy % 60) * intPow(10, i);
copy /= 60;
} while (copy);
return target;
}
int intPow(int x, int p) {
if (p == 0) return 1;
if (p == 1) return x;
return x * intPow(x, p-1);
}
int getMinute(int hm) {
return hm / 100 * 60 + hm % 100;
}
int getFormatTime(int m) {
return m / 60 * 100 + m % 60;
}
int exchange(int &a, int &b) {
int temp = a;
a = b;
b = temp;
return 0;
}
func.h
#ifndef EXPERIMENT_01_FUNC_H
#define EXPERIMENT_01_FUNC_H
#include
#include
#include
#include
using namespace std;
void getA_DTime(vector<int> &arrival_times, vector<int> &duration, int k);
int to_60(int );
int to_10(int );
int intPow(int x, int p);
int getMinute(int );
int exchange(int , int);
int getFormatTime(int m);
#endif
main.cpp
#include "HRRN.h"
#include "func.h"
using namespace std;
int main()
{
int k;
cout << "Please enter the quantity of all operations: " << endl;
cin >> k;
cin.ignore();
vector<int> arrival_times;
vector<int> duration;
getA_DTime(arrival_times, duration, k);
HRRN hrrn(arrival_times, duration);
cout << "-----------------------" << endl;
hrrn.operating();
cout << hrrn << endl;
system("pause");
// getchar();
return 0;
}
测试用例: input.txt
4 (总作业个数)
(进入时间点:XXYY)(工作时长)
0800 120
0830 30
0900 6
0930 12
现在我们输入一下:
Please enter the quantity of all operations:
4
--Enter the information--
==1==
Arrival time(XXYY):
800 120
Duration(min):
==2==
Arrival time(XXYY):
830 30
Duration(min):
==3==
Arrival time(XXYY):
900 6
Duration(min):
==4==
Arrival time(XXYY):
930 12
..
...
Duration(min)
Ord: Order number
No.: number
St: Scheduling time
Tt: Turnaround time
wTt: Weighted turnaround time
------------------------------------------------
Ord No. St Tt(min) wTt(min)
1 1 0800 120.00 1.00
3 2 1006 126.00 4.20
2 3 1000 66.00 11.00
4 4 1036 78.00 6.50
------------------------------------------------
Average turnaround time: 97.50 (min)
Average turnaround time with weights: 5.67
请按任意键继续. . .
我们看到,将单位转换一下和之前表格中的内容计算是一样的。学费了吗?
PS:func.cpp中只用到了其中部分函数,没用用到的懒得删了,大家学习本篇代码的时候注意一下
——————
原创不易,转载请注明出处!