多级反馈队列调度算法(c++)

如果对你有帮助,可以给卑微的博主留个赞、关注、收藏   (不是) 

(骗一下数据,说不定以后面试就过了,拜谢)

操作系统基本调度算法,多级反馈队列调度算法。在划分时间片的调度算法中,多级反馈队列算法兼顾提高系统吞吐率和及减少进程饥饿。设置多个反馈队列,q1~qn ,q1的优先级最高,但是在q1队列中能运行的时间片最短。当进程到达时,进入q1,在反馈队列内部执行先来先服务原则。如果进程在时间片用完后 还没有执行完成,掉入下一级反馈队列q2(q2时间片会更长)。当q1队列为空时,开始调度q2的进程执行,以此类推。而当q1有新进程到达时,又回头调度q1的进程执行。这样短作业在前几个反馈队列就执行完了,长进程在到达前几个反馈队列时也会执行一段时间,防止饿死。

设置进程pcb结构,创建优先级不同的反馈队列,数量由用户输入, 创建完成队列和未到达队列 ,创建数组存储各反馈队列的时间片。

将作业按到达时间升序排序后,创建pcb后插入未到达队列。

开始调度进程执行。设置系统当前时间为0。

循环(进程还有未执行完毕)

{   当前时间到达进程进入第一级队列。

    从第一级队列开始查看直到找到待执行进程。

    根据进程所在队列,得到此次运行时间片大小。

    创建中间变量存放进程pcb,运行一个时间片后,如果有进程到达,

    将所有到达的进程送入第 一级队列。

     如果进程已执行完毕,送入完成队列,重新进行循环调度。

    如果进程还没执行完且时间片用完,将进程送入下一级队列,

    如果已经是最后一级则放队尾。

    如果进程还没执行完且时间片没用完,且有进程到达,同时能够 被抢占,

    那么将正在执行的进程放入本队列末尾,重新进行循环调度。

    否则继续执行一个时间片。

}

代码如下

#define time_slice  1       //第一队列时间片长度
#define N			5		//进程数
#include<iostream>
#include <queue>

using namespace std;

int* Queue_time_slice = nullptr;    //指向存放各反馈队列时间片数组的指针

struct pcb
{
	char process_name[10];   //进程名字
	int arrive_time;   		//到达时间
	int service_time;   	//需要服务时间
	int remain_time;        //还差多少时间
	int complete_time;   	//完成时间
	char state; 			//进程状态

}PCB[N + 1];             //0号单元不存数据,直接用作交换


queue<pcb> Coming_queue;	//还未到达队列
queue<pcb> Finish_queue;	//已完成队列
queue<pcb>** Wait_queue = new (queue<pcb>*);    //指向存放 各反馈队列的指针 的指针
											//Wait_queue[0]为第一级反馈队列的指针
int Queue_Num;           //反馈队列数量

void sort(int n)
{
	int i, j;
	for (i = 1; i < n; i++)            //按到达时间升序
	{
		for (j = 1; j <= n - i; j++)
		{
			if (PCB[j].arrive_time > PCB[j + 1].arrive_time)
			{
				PCB[0] = PCB[j];
				PCB[j] = PCB[j + 1];
				PCB[j + 1] = PCB[0];
			}
		}
	}
}

void init_wait_queue(const int queue_num,queue<pcb>** &wait_queue)
{
					//创建各反馈队列,计算其时间片 1 2 4 8 
	Queue_time_slice = new int[queue_num];       
	for (int i = 0; i < queue_num; i++)
	{
		Queue_time_slice[i] = time_slice * pow(2,i);
		wait_queue[i] = new queue<pcb>;          
	}	
}

int input(int n)
{
	int i;
	cout << "请输入反馈队列级数: ";
	cin >> Queue_Num;

	cout << "\n请输入各进程的信息\n" << endl;
	for (i = 1; i <= n; i++)      //输入各进程信息,插入未到达队列
	{
		PCB[i].state = 'w';
		cout << "------\n请输入第" << i << "进程名字: ";
		cin >> PCB[i].process_name;
		cout << "请输入到达时间: ";
		cin >> PCB[i].arrive_time;
		cout << "请输入服务时间: ";
		cin >> PCB[i].service_time;
		PCB[i].remain_time = PCB[i].service_time;

	}
	sort(n);
	for (i = 1; i <= n; i++)
	{
		Coming_queue.push(PCB[i]);
	}

	init_wait_queue(Queue_Num,Wait_queue);
	return 0;
}

bool accept_process(int current_time)
{
	bool tag = false;
	while (1)
	{
		if (Coming_queue.empty() == false && Coming_queue.front().arrive_time <= current_time)
		{
			Wait_queue[0]->push(Coming_queue.front());
			Coming_queue.pop();
			tag = true;
		}
		else break;
	}
	return tag;
}


void run()
{
	bool over;     //true表示进程都执行完
	int current_time = Coming_queue.front().arrive_time;    //系统当前时间
	pcb temp;      //存放要执行进程的pcb
	int t;         //存放时间片
	int queue_serial;    //要执行进程所在队列号,从1~Queue_Num
	int count = 1;   //调度次数
	while (1)
	{
		int i;
		over = true;
		accept_process(current_time);   //接收当前时间到达的进程

		for (i = 0; i < Queue_Num; i++)
		{
			if (Wait_queue[i]->empty() == false)
			{
				over = false;
				temp = Wait_queue[i]->front();
				Wait_queue[i]->pop();
				t = Queue_time_slice[i];
				queue_serial = i + 1;
				break;
			}
		}
		if (Coming_queue.empty() == false)
			over = false;

		if (over == true)
			break;
		cout << "\n第" << count << "次调度进程: " << temp.process_name 
			 <<"  时间 :  " << current_time << endl;
		while (1)
		{
			{   //运行一个时间片
				t--;
				current_time++;
				temp.state = 'r';
				temp.remain_time--;
			}
			if (temp.remain_time == 0)   //进程执行完
			{
				temp.state = 'f';
				temp.complete_time = current_time;
				Finish_queue.push(temp);
				break;
			}

			else if (t == 0)           //进程还没执行完但是时间片用完
			{
				temp.state = 'w';

				if (queue_serial == Queue_Num)
					queue_serial--;
				Wait_queue[queue_serial]->push(temp);  //插入下一级反馈队列或者最后一级队尾
				break;
			}
			//进程还没执行完,时间片还没用完
			else 
			{	//可以被抢占,且有进程到达
				if (queue_serial != 1 && true == accept_process(current_time))   
				{
					temp.state = 'w';
					Wait_queue[queue_serial - 1]->push(temp);  //插入本级反馈队列
					break;
				}
				
			}
		}
		count++;
	}
}
void print()
{
	int i = 0;
	float round_time[N],		  //周转时间
		force_round_time[N],    //带权周转时间
		sum = 0;				  //存放各进程的带权周转时间和
	cout << "\n 进程   |" << "到达时间  |" << "  服务时间   |" << "  完成时间   |" << "  周转时间  |" << " 带权周转时间" << endl;
	while (Finish_queue.empty() == false)
	{
		round_time[i] = Finish_queue.front().complete_time - Finish_queue.front().arrive_time;
		force_round_time[i] = round_time[i] / Finish_queue.front().service_time;
		cout << Finish_queue.front().process_name
			<< "\t|  " << Finish_queue.front().arrive_time
			<< "\t   |  " << Finish_queue.front().service_time << " \t |  " << Finish_queue.front().complete_time
			<< "\t       |  " << round_time[i]
			<< "\t    |  " << force_round_time[i]
			<< endl;
		Finish_queue.pop();

		sum += force_round_time[i];
		i++;
	}
	cout << "\n\n系统平均带权周转时间: " << (sum / i) << endl;
}

int main()
{
	
	cout << "\t\t多级反馈队列调度算法" << endl;
	input(N);
	run();
	print();

	return 0;
}

  • 37
    点赞
  • 147
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
多级反馈队列算法是一种用于调度进程的算法,它将进程分为多个队列,并根据进程的优先级和执行时间的长度将进程放入不同的队列中。队列越高,进程的优先级越高,执行时间也越短。当一个进程在一个队列中执行了一段时间后,如果还没有完成,它将被移到更低优先级的队列中,以便给更高优先级的进程更多的机会。 以下是一个简单的 C++ 实现多级反馈队列算法的代码示例: ```c++ #include <iostream> #include <queue> using namespace std; // 进程结构体 struct Process { int id; // 进程 ID int priority; // 进程优先级 int execution; // 执行时间 }; // 定义多级反馈队列 queue<Process> q1, q2, q3; // 调度函数 void schedule() { // 先处理高优先级队列 q1 if (!q1.empty()) { Process p = q1.front(); q1.pop(); // 执行进程 cout << "Process " << p.id << " is running in q1." << endl; // 如果还未执行完成,则放入下一个队列 q2 if (p.execution > 1) { p.execution--; p.priority++; q2.push(p); } } // 如果 q1 为空,则处理中优先级队列 q2 else if (!q2.empty()) { Process p = q2.front(); q2.pop(); // 执行进程 cout << "Process " << p.id << " is running in q2." << endl; // 如果还未执行完成,则放入下一个队列 q3 if (p.execution > 1) { p.execution--; p.priority++; q3.push(p); } } // 如果 q1 和 q2 都为空,则处理低优先级队列 q3 else if (!q3.empty()) { Process p = q3.front(); q3.pop(); // 执行进程 cout << "Process " << p.id << " is running in q3." << endl; // 如果还未执行完成,则放回队列 q3 if (p.execution > 1) { p.execution--; q3.push(p); } } } int main() { // 将进程放入队列 q1.push({1, 3, 3}); q1.push({2, 1, 4}); q1.push({3, 2, 2}); q1.push({4, 4, 1}); // 调度进程 while (!q1.empty() || !q2.empty() || !q3.empty()) { schedule(); } return 0; } ``` 在这个示例中,我们定义了一个简单的进程结构体,包括进程 ID、优先级和执行时间。我们使用三个队列 q1、q2 和 q3 来模拟多级反馈队列,其中 q1 为高优先级队列,q2 为中优先级队列,q3 为低优先级队列。在调度函数中,我们首先处理 q1 中的进程,如果 q1 中没有进程,则处理 q2 中的进程,如果 q2 中也没有进程,则处理 q3 中的进程。在处理进程时,我们根据进程的优先级和执行时间将其放入不同的队列中,以模拟多级反馈队列算法的调度过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凛_Lin~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值