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

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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值