高响应比优先调度算法(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;
		else
			break;
	} 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;
}


下面是一个简单的高响应比优先调度算法的C++代码示例: ```c++ #include <iostream> #include <queue> using namespace std; // 进程结构体 struct Process { int pid; // 进程ID int priority; // 优先级 int arrival; // 到达时间 int burst; // 执行时间 int response; // 响应时间 int turnaround; // 周转时间 }; // 定义一个比较器,用于将优先级高的进程排在队列前面 struct Compare { bool operator()(const Process& p1, const Process& p2) { double r1 = 1.0 * (p1.burst + p1.response) / p1.burst; double r2 = 1.0 * (p2.burst + p2.response) / p2.burst; return r1 < r2; } }; // 高响应比优先调度算法 void HRRN(Process processes[], int n) { // 按照到达时间排序 sort(processes, processes + n, [](Process p1, Process p2) { return p1.arrival < p2.arrival; }); // 创建一个优先队列,按照响应比排序 priority_queue<Process, vector<Process>, Compare> pq; int time = 0; // 当前时间 int sum_response_time = 0; // 总响应时间 int sum_turnaround_time = 0; // 总周转时间 for (int i = 0; i < n; i++) { // 将到达时间小于等于当前时间的进程加入队列 while (i < n && processes[i].arrival <= time) { processes[i].response = time - processes[i].arrival; pq.push(processes[i]); i++; } // 如果队列为空,说明当前时间没有进程需要执行,时间加1 if (pq.empty()) { time++; i--; continue; } // 取出一个响应比最高的进程执行 Process p = pq.top(); pq.pop(); // 更新响应时间和周转时间 sum_response_time += p.response; sum_turnaround_time += time + p.burst - p.arrival; // 执行进程 time += p.burst; // 将到达时间小于等于当前时间的进程加入队列 while (i < n && processes[i].arrival <= time) { processes[i].response = time - processes[i].arrival; pq.push(processes[i]); i++; } // 将该进程放回队列,等待下一次执行 p.burst = 0; pq.push(p); } // 计算平均响应时间和平均周转时间 double avg_response_time = 1.0 * sum_response_time / n; double avg_turnaround_time = 1.0 * sum_turnaround_time / n; // 输出结果 cout << "平均响应时间: " << avg_response_time << endl; cout << "平均周转时间: " << avg_turnaround_time << endl; } int main() { // 创建进程数组 Process processes[] = { {1, 3, 0, 8}, {2, 1, 1, 4}, {3, 4, 2, 9}, {4, 5, 3, 5}, {5, 2, 4, 6} }; int n = sizeof(processes) / sizeof(Process); // 调用高响应比优先调度算法 HRRN(processes, n); return 0; } ``` 上述代码实现了一个简单的高响应比优先调度算法,其中使用了一个优先队列来按照响应比排序。在每个时间片中,将所有到达时间小于等于当前时间的进程加入优先队列中,然后取出响应比最高的进程执行。在执行完一个进程后,将到达时间小于等于当前时间的进程再次加入优先队列中,并将当前进程放回队列等待下一次执行。最后,计算平均响应时间和平均周转时间并输出结果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值