高响应比优先调度算法(Highest Response Ratio Next)是一种对CPU中央控制器响应比的分配的一种算法。HRRN是介于FCFS(先来先服务算法)与SJF(短作业优先算法)之间的折中算法,既考虑作业等待时间又考虑作业运行时间,既照顾短作业又不使长作业等待时间过长,改进了调度性能。
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<queue>
#include<list>
#include<thread>
#include<mutex>
#include<Windows.h>
using namespace std;
#define MAX_TIME 99999
int g_time = 0;
mutex g_mutex_time;
struct process_node
{
int prcess_id; //进程编号
int _start; //进入时间
void(*main_doing)(int args, char *ptr_argv[]);//本进程完成的任务
int begin; //开始时间
int finish; //完成时间
int _function; //需要运行时间
int function; //已经运行的时间
int wait;
bool complete; //是否完成 true 完成
};
list<process_node*>q_list;//进程队列
void showlist()
{
for (auto ib = q_list.begin(); ib != q_list.end(); ib++)
{
cout << (*ib)->prcess_id << " "<<(*ib)->wait<<" ||";
}
cout << "\n";
}
void main_doing(int args, char *ptr_argv[])
{
cout << args << "这是一个运行的实例" << endl;
Sleep(200);
}
void Come_Init_Prcess(void(*main_doing)(int args, char *ptr_argv[]), int _function) //模拟进程到来并且初始化
{
static int id = 0;
process_node *p = new process_node;
p->prcess_id = ++id;
p->_start = g_time;
p->main_doing = main_doing;
p->_function = _function;
p->begin = MAX_TIME;
p->wait = 0;
p->finish = MAX_TIME;
p->function = 0;
p->complete = false;
q_list.push_back(p);
}
void Time_End_Work(process_node & current_process)//时间片结束的前的工作
{
if (current_process.function >= current_process._function)//判断是否完成
{
current_process.complete = true;
current_process.finish = g_time;
cout << "进程" << current_process.prcess_id << "完成任务" << endl;
int wait = current_process.finish - current_process.begin;
q_list.remove(¤t_process);
for (auto ib = q_list.begin(); ib != q_list.end(); ib++)
{
(*ib)->wait += wait;
}
}
}
void One_End_Process(process_node & current_process)
{
int current_point = g_time;
cout << "当前时间" << current_point << endl;
while (current_point + current_process._function >= g_time)
{
current_process.main_doing(current_process.prcess_id, NULL);
}
current_process.function += g_time - current_point;
Time_End_Work(current_process);
}
process_node& Obtain_Obtain()//获取优先者
{
float temp = 0.0;
process_node *p_temp = nullptr;
while (q_list.size() == 0);
for (auto ib = q_list.begin(); ib != q_list.end(); ib++)
{
if ((float)(((*ib)->_function + (*ib)->wait)/ (*ib)->_function) > temp)
{
temp = (float)(((*ib)->_function + (*ib)->wait) / (*ib)->_function);
p_temp = (*ib);
}
}
cout << "优先的是程序" << p_temp->prcess_id << endl;
p_temp->begin = g_time;
return *p_temp;
}
void Run_begin()
{
while (1)
{
process_node &p_temp = Obtain_Obtain();
showlist();
One_End_Process(p_temp);
}
}
//时间实例到达
void pthread_model()
{
time_t ts;
srand((unsigned int)time(&ts));
while (1)
{
int x_temp = 0;
lock_guard<mutex>ldg(g_mutex_time);
cout << "时间:[" << g_time << "]" << endl;
if (g_time % 2 == 0)
{
while (1)
{
x_temp = rand() % 5;
if (x_temp > 0)
{
break;
}
}
Come_Init_Prcess(main_doing, x_temp);
cout << "实例到达" << endl;
}
Sleep(1000);
g_time++;
}
}
int main()
{
thread th_model(pthread_model);
thread th_main(Run_begin);
th_model.join();
th_main.join();
cin.get();
}