当前位置:网站首页>High response ratio first

High response ratio first

2022-07-28 07:19:00

         High response priority scheduling algorithm (Highest Response Ratio Next) Is a kind of right CPU An algorithm for allocation of central controller response ratio .HRRN Is between FCFS( First come first serve algorithm ) And SJF( Short job first algorithm ) The compromise algorithm , Consider both job waiting time and job running time , Not only take care of short assignments, but also do not make long waiting time for long assignments , Improved scheduling performance .

 

#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;  // Process number 
	int _start;  // Entry time 
	void(*main_doing)(int args, char *ptr_argv[]);// Tasks completed by this process 
	int begin;   // Starting time 
	int finish;  // Completion time 
	int _function; // Run time required 
	int function; // Time that has been running 
	int wait;
	bool complete;	 // Whether it is completed or not    true  complete 
};

list<process_node*>q_list;// Process queue 


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 << " This is a running instance " << endl;
	Sleep(200);
}

void Come_Init_Prcess(void(*main_doing)(int args, char *ptr_argv[]), int _function) // The simulation process arrives and initializes 
{
	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)// Work before the end of the time slice 
{
	if (current_process.function >= current_process._function)// Judge whether it is finished 
	{
		current_process.complete = true;
		current_process.finish = g_time;
		cout << " process " << current_process.prcess_id << " To complete the task " << endl;
		int wait = current_process.finish - current_process.begin;
		q_list.remove(&current_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 time " << 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()// Get priority 
{
	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 << " The priority is the program " << 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);
	}
}
// Time instances arrive 
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 << " Time :[" << 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 << " Instance arrives " << 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();

}


 

原网站

版权声明
本文为[]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207280520515463.html