高响应比算法模拟(实验报告)

实验目的和要求

  1. 掌握高响应比作业调度的概念和算法
  2. 加深对处理机分配的理解

实验内容

在集成开发环境下使用C++语言,利用相应的函数,编写程序实现作业高响应比调度算法,学会运行程序和调试程序。

实验过程

响应比R定义为:R=(W+T)/T=1+W/T ,其中T为该作业估计需要的执行时间,W为作业在后备状态队列中的等待时间。

程序需要完成的任务

a. 等待用户输入作业名、到达时间、运行时间,并将其存储到指定的数据结构中。
b. 比较所有作业的响应比,并输出响应比最大的作业名,
c. 删除已输出作业名,执行步骤b 至所有作业输出完

数据结构

a. 使用class类存储作业信息,用list类型对象存储所有的作业信息
b. class类中包含作业名、到达时间、运行时间、响应比。

struct Process {
	std::string name;
	int co_time, run_time, w_time;
	double req;		//R=1+W/T    R:响应比   W:等待时间  T:运行时间
	Process(std::string name,int co_time,int run_time) :req(1.0){
		this->name = name;
		this->co_time = co_time;
		this->run_time = run_time;
		req = 1 + 1.0 / co_time;  //当所有作业未在第一时间到达时便于选择最小到达时间的作业
		}
	};

流程图

在这里插入图片描述

//  main.cpp
int main() {
	list<Process> lis;
	int all_time=0;
	init(lis);
	selNode(lis, all_time);
	return 0;
}
//选取响应比最高的结点输出
void selNode(list<Process>& lis,int all_time) {
	int flag = 0;
	while (lis.size() != 0) {
		list<Process>::iterator bgp = lis.begin();
		for (list<Process>::iterator it = lis.begin(); it != lis.end(); it++) {	
			bgp = bgp->req >= it->req ? bgp : it;	//比较每个作业的req 返回最大的作业
		}
		cout << bgp->name << "\t";
		all_time = bgp->run_time + bgp->co_time;
		lis.erase(bgp);

		for (auto elem : lis) {
			elem.w_time = all_time - elem.co_time;		//重新设置作业等待时间
			elem.req = 1 + elem.w_time * 1.0 / elem.run_time;
		}
	}
} 

运行结果

在这里插入图片描述

分析和讨论

错误分析:

  1. 思路错误:最初考虑抢占式和非抢占式,抢占式需要不停的检测响应比,实现复杂,浪费性能。非抢占式在每次作业完成后检测响应比即可。
  2. 第一次实现将类存储在vector类中,因为有大量随机删除步骤
    所有改为list类存储作业。

结论

可以考虑加入时间片概念在每个时间片后计算响应比,实现抢占式。
可以预防先到达的长作业长期占有cpu。

完整代码

#include <iostream>
#include <list>
#include <string>
//#include "Process_scheduling.h"
using namespace std;

struct Process {
	std::string name;
	int co_time, run_time, w_time;
	double req;		//R=1+W/T    R:响应比   W:等待时间  T:运行时间
	Process(std::string name,int co_time,int run_time){
		this->name = name;
		this->co_time = co_time;
		this->run_time = run_time;
		req = 1 + 1.0 / co_time;
		
	}
};
void init(list<Process> &lis) {
	int count;
	string name;
	int co_time, run_time, rep, all_time;
	int c_w_time{ 0 };
	cout << "请输入作业数!!" << endl;
	cin >> count;
	cout << "请输入作业名、到达时间、运行时间,并以空格键分隔" << endl;

	//输入各个作业信息,并插入lis中
	for (int i = 1; i <= count; i++) {

		cin >> name >> co_time >> run_time;
		Process* p = new Process(name, co_time, run_time);
		lis.push_back(*p);
		getchar();
	}
	return;

}

void selNode(list<Process>& lis,int all_time) {
	int flag = 0;
	while (lis.size() != 0) {
		list<Process>::iterator bgp = lis.begin();
		for (list<Process>::iterator it = lis.begin(); it != lis.end(); it++) {	
			bgp = bgp->req >= it->req ? bgp : it;	//比较每个作业的req 返回最大的作业
		}
		cout << bgp->name << "\t";
		all_time = bgp->run_time + bgp->co_time;
		lis.erase(bgp);

		for (auto elem : lis) {
			elem.w_time = all_time - elem.co_time;		//重新设置作业等待时间
			elem.req = 1 + elem.w_time * 1.0 / elem.run_time;
		}
	}
}
int main() {
	list<Process> lis;
	int all_time=0;
	init(lis);
	selNode(lis, all_time);

	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值