C++操作系统之高响应比(HRRN)算法(非抢占式)

本文介绍了一个使用C++实现的HRRN(HighResponseRatioNext)算法,该算法计算进程的优先级并据此进行调度。在初始化时,进程按到达时间和服务时间排序,之后根据等待时间动态更新优先级。代码展示了如何处理多个进程的排序和执行,最终计算周转时间和平均周转时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HRRN算法:
每个进程都进行优先级的计算,计算公式为(等待时间+当前进程所需的服务时间)/当前进程所需的服务时间
计算出的结果则为优先权。优先权高的先执行
主要逻辑:
主要考虑两个点:
第一:初始化时,进程到达的时间要排序,此时排序要考虑的是进程的到达时间以及服务时间,同时间下服务时间越短的则优先级越高,分母越小得数越大嘛,小学知识。
第二:考虑的是当前时间片有多少进程进来先按上述的排序方式来排序,然后进来的进程要考虑等待时间,即每个进程结束后,当前的时间片内到达的进程都要对这个时间计算等待时间,然后再按优先级进行排序。是一个比较复杂的多排序问题。

代码区:

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct PCB {
	string Name;//进程名字
	double Priority;//优先权
	double arriveTime;//进程到达时间
	double serviceTime;//进程需要服务时间
	double endTime;//已服务时间
	double finishTime;//完成时间点记录
	string State;//进程状态
	double RunTime;//周转时间

};
/*
	周转时间=完成时间 - 到达时间
*/
bool myCompare_ArriveTime(PCB P1, PCB P2) {
	//按到达时间排序
	return P1.arriveTime < P2.arriveTime;

}


bool myCompare_ArriveTime_ServiceTime(PCB P1, PCB P2) {
	//如果到达时间相同则按服务时间排序,如果到达时间不同那么就按到达时间排序
	if (P1.arriveTime == P2.arriveTime) {
		return P1.serviceTime < P2.serviceTime;
	}
	//按到达时间排序
	return P1.arriveTime < P2.arriveTime;
}

bool myCompare_ArriveTime_Priority(PCB P1, PCB P2) {
	//如果到达时间相同则按服务时间排序,如果到达时间不同那么就按到达时间排序
	if (P1.arriveTime == P2.arriveTime) {
		return P1.Priority > P2.Priority;
	}
	//按到达时间排序
	return P1.arriveTime < P2.arriveTime;
}

bool myCompare_Priority(PCB P1, PCB P2) {
	//如果到达时间相同则按服务时间排序,如果到达时间不同那么就按到达时间排序
	if (P1.arriveTime == P2.arriveTime) {
		return P1.Priority > P2.Priority;
	}
	//按到达时间排序
	return P1.Priority > P2.Priority;
}
queue<PCB> Change(queue<PCB>q) {
	vector<PCB>v;
	while (q.size() != 0) {
		v.push_back(q.front());
		q.pop();
	}

	sort(v.begin(), v.end(), myCompare_Priority);
	for (vector<PCB>::iterator it = v.begin(); it != v.end(); it++) {
		q.push(*it);
	}
	return q;
}

void showData_Vector(vector<PCB>& v) {
	cout << "----------------------------------------------------当前进程情况----------------------------------------------------" << endl;
	cout << "进程名\t" << "进程到达时间\t" << "进程服务时间\t" << " 进程优先级\t" << "进程已服务时间\t" << " 进程完成时间\t" <<"进程周转时间\t" << "进程当前状态\t" << endl;
	for_each(v.begin(), v.end(), [](PCB p) {cout << p.Name << "\t    " << p.arriveTime
		<< "   \t\t" << p.serviceTime << "\t\t" << p.Priority << "\t\t" << p.endTime << "\t\t" << p.finishTime << "\t\t"<<p.RunTime<<"      \t" << p.State << endl; });
	cout << "--------------------------------------------------------------------------------------------------------------------" << endl;
}

void showData(queue<PCB>& q) {
	cout << "----------------------------------------------------当前进程情况----------------------------------------------------" << endl;
	cout << "进程名\t" << "进程到达时间\t" << "进程服务时间\t" << " 进程优先级\t" << "进程已服务时间\t" << " 进程完成时间\t" << "进程周转时间\t" << "进程当前状态\t" << endl;
	while (q.size() != 0) {
		cout << q.front().Name << "\t    " << q.front().arriveTime <<"   \t\t"
			<< q.front().serviceTime << "\t\t" << q.front().Priority <<"\t\t" << q.front().endTime << "\t\t"
			<< q.front().finishTime << "\t\t"<<q.front().RunTime<< "      \t" << q.front().State << endl;
		q.pop();
	}
	
}
/*
	响应比高者优先
*/
void HNNR() {
	vector<PCB>v;
	cout << "请输入进程个数:";
	int sum;
	int increatment = 0;
	cin >> sum;
	increatment = sum - 1;
	PCB p;
	queue<PCB>readyList;
	for (int i = 1; i <= sum; i++) {
		cout << "请输入第 " << i << " 个进程名字:";
		cin >> p.Name;
		cout << "请输入第 " << i << " 个进程到达时间:";
		cin >> p.arriveTime;
		cout << "请输入第 " << i << " 个进程需要服务的时间:";
		cin >> p.serviceTime;
		p.State = "就绪状态";
		p.endTime = 0;
		p.finishTime = 0;
		p.Priority = 0;
		p.RunTime = 0;
		//把进程放入容器中
		v.push_back(p);
	}
	//按进程到达的时间排序
	sort(v.begin(), v.end(), myCompare_ArriveTime);
	//展示当前情况
	showData_Vector(v);

	//把数据放入队列中
	for (vector<PCB>::iterator it = v.begin(); it != v.end(); it++) {
		readyList.push(*it);
	}

	int tempTime = 0;
	queue<PCB>endlist;
	queue<PCB>runList;
	queue<PCB>tempList;
	PCB temp;
	//获取第一个数据前的操作:
	while (1) {
		if (tempTime == readyList.front().arriveTime) {
			break;
		}
		else {
			cout << "第" << tempTime << "秒没有进程被执行" << endl;
			tempTime++;
		}
		
	}

	while (readyList.size() != 0) {
		if (tempTime == readyList.front().arriveTime) {
			cout << "-------------------------------------------------------" << endl;
			cout << "第" << tempTime << "秒" << readyList.front().Name << "进程到达" << endl;
			cout << "-------------------------------------------------------" << endl;
			//把进来的进程放入执行队列中
			readyList.front().Priority = (tempTime - readyList.front().arriveTime + readyList.front().serviceTime) / readyList.front().serviceTime;
			runList.push(readyList.front());
			readyList.pop();
		}
		else
		{
			tempList.push(readyList.front());
			readyList.pop();
		}
	}
	
	runList = Change(runList);
	//拿到第一个开始执行下一步操作:
	while (runList.size() != 0)
	{

		if (runList.front().serviceTime != 0) {
			//如果时间还需服务时间不为0则做相关的加或者减
			cout << "第" << tempTime << "秒 -" << runList.front().Name << "- 正在执行" << endl;
			tempTime++;
			runList.front().endTime++;
			runList.front().serviceTime--;
		}

		if (runList.front().serviceTime == 0) {
			//如果第一个数据的所需服务时间为0了那么这个数据所需执行的时间够了则交给下一个程序
			cout << "-------------------------------------------------------" << endl;
			cout << "第" << tempTime << "秒 -" << runList.front().Name << "- 已执行完毕" << endl;
			cout << "-------------------------------------------------------" << endl;
			//把结束时间标记上
			runList.front().finishTime = tempTime;
			//把状态进行修改
			runList.front().State = "执行完毕";
			//重新计算优先权
			//runList.front().Priority = (tempTime - runList.front().arriveTime + runList.front().serviceTime) / runList.front().serviceTime;
			//周转时间记录
			runList.front().RunTime = tempTime - runList.front().arriveTime;
			//放入另一个队列中保存起来
			endlist.push(runList.front());
			//弹出第一个数据让下一个开始执行
			runList.pop();

			if (tempList.size() != 0) {
				while (tempList.size() != 0) {
					if (tempTime >= tempList.front().arriveTime) {
						tempList.front().Priority = (tempTime - tempList.front().arriveTime + tempList.front().serviceTime) / tempList.front().serviceTime;
						runList.push(tempList.front());
						tempList.pop();
					}
					else {
						tempTime++;
						if (tempTime == tempList.front().arriveTime) {
							tempList.front().Priority = (tempTime - tempList.front().arriveTime + tempList.front().serviceTime) / tempList.front().serviceTime;
							runList.push(tempList.front());
							tempList.pop();
							break;
						}
					}
				}

				if (runList.size() != 0) {
					if (tempTime >= runList.front().arriveTime) {
						cout << "-------------------------------------------------------" << endl;
						cout << "第" << runList.front().arriveTime << "秒" << runList.front().Name << "进程到达" << endl;
						cout << "-------------------------------------------------------" << endl;
					}
				}
				runList = Change(runList);
			}

		}

	}
	queue<PCB>result;
	result = endlist;
	showData(endlist);
	//平均周转时间
	double avgTime = 0;
	double sumTime = 0;
	int size = result.size();
	
	while (result.size() != 0) {
		sumTime += result.front().RunTime;
		result.pop();
	}
	avgTime = sumTime / size;
	cout << "平均周转时间:" << avgTime << endl;
	cout << "--------------------------------------------------------------------------------------------------------------------" << endl;
}

int main() {
	HNNR();
	return 0;
}
实验一 批处理系统的作业调度 1.实验目的 加深对作业概念的理解; 深入了解批处理系统如何组织作业、管理作业和调度作业; 2.实验预备知识 作业的概念; 作业的创建; 作业的调度。 3.实验内容 编写程序完成批处理系统中的作业调度,要求采用响应比高优先的作业调度算法。实验具体包括:首先确定作业控制块的内容,作业控制块的组成方式;然后完成作业调度;最后编写主函数对所作工作进程测试。 4.提示与讲解 操作系统根据允许并行工作的道数和一定的算法从系统中选取若干作业把它们装入主存储器,使它们有机会获得处理器运行,这项工作被称为“作业调度”。实现这部分功能的程序就是“作业调度程序”。 作业调度的实现主要有两个问题,一个是如何将系统中的作业组织起来;另一个是如何进行作业调度。 为了将系统中的作业组织起来,需要为每个进入系统的作业建立档案以记录和作业相关的信息,例如作业名、作业所需资源、作业执行时间、作业进入系统的时间、作业信息在存储器中的位置、指向下一个作业控制块的指针等信息。这个记录作业相关信息的数据块称为作业控制块(JCB),并将系统中等待作业调度的作业控制块组织成一个队列,这个队列称为后备队列。一个作业全部信息进入系统后,就为其建立作业控制块,并挂入后备队列。当进行作业调度时,从后备队列中查找选择作业。 由于实验中没有实际作业,作业控制块中的信息内容只使用了实验中需要的数据。作业控制块中首先应该包括作业名;其次是作业所需资源,根据需要,实验中只包括需要主存的大小(采用可移动的动态分区方式管理主存,作业大小就是需要主存的大小)、需要打印机的数量和需要磁带机的数量;采用响应比作业调度算法,为了计算响应比,还需要有作业的估计执行时间、作业在系统中的等待时间;另外,指向下一个作业控制块的指针必可少。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值