优先级调度算法的模拟(非抢占式C++实现)

设置参数

设置了四个作业进行实验,可以在init()函数中动态修改

根据计算,根据优先级调度算法,得出作业的调度流程图

运行程序,得出程序运行结果

 通过对比可以发现,程序运行正确,并且程序还可以输出,每一秒运行的状态,参数包括正在运行的进程名,开始运行时间,及当前就绪队列中的进程名

流程图

 

 源程序

#include<iostream>

using namespace std;

const int N = 20;
int h[N], hh, tt = -1;//用数组模拟队列,就绪队列, 存放的是进程的下标 

struct Process
{
	string name;//作业名
	int arriveTime;//到达时间
	int runTime;//运行时间
	bool st = false;//是否已经运行完毕, false为没运行完
	int startTime = 0x3f3f3f3f;//开始运行时间
	int endTime = 0x3f3f3f3f;//结束运行时间 
	int priority;//优先级 
}process[N];//N个作业 

void init()
{
	//初始化四个作业 
	process[1].arriveTime = 0, process[1].runTime = 4, process[1].name = "process 1", process[1].priority = 10;
	process[2].arriveTime = 2, process[2].runTime = 2, process[2].name = "process 2", process[2].priority = 9;
	process[3].arriveTime = 1, process[3].runTime = 3, process[3].name = "process 3", process[3].priority = 11;
	process[4].arriveTime = 4, process[4].runTime = 1, process[4].name = "process 4", process[4].priority = 12;
}

void arriveCheck(int syTime)//每秒钟都check一下有没有新进程来 
{
	for(int i = 1; i <= 4; i ++)
		if(!process[i].st && process[i].arriveTime <= syTime)//当作业到达,进入就绪队列
			{
				h[++ tt] = i; 
				process[i].st = true;//标记已经到达	
			}
}

void print(int i, int t)//打印每秒的输出 
{
	cout << "当前时间是 " << i << "    正在运行的进程是 " << process[t].name <<  "    开始运行时间为 " << process[t].startTime << endl;
	
	if(tt >= hh)
	{
		cout << "当前就绪队列中的进程为 ";
		for(int j = hh; j <= tt; j ++)
			cout << process[h[j]].name << ' ';
		cout << endl << endl;
	}
	else cout << "当前就绪队列为空" << endl << endl; 
} 

void printResult()//打印最终结果 
{
	cout << "优先级调度算法已经全部运行完毕" << endl;
	cout << "作业名      " << "到达时间   " << "开始运行时间   " << "结束运行时间   " << "周转时间   " << endl;
	double sum = 0;//计算平均周转时间 
	for(int i = 1; i <= 4; i ++)
	{
		int t = process[i].endTime - process[i].arriveTime;//周转时间
		sum += t; 
		cout << process[i].name << "       " << process[i].arriveTime << "            " << process[i].startTime << "            " << process[i].endTime << "             " << t << endl;
	}
	cout << "平均周转时间为 " << sum / 4 << endl;
}

int main()
{
	init();
	
	int syTime = 0;//系统时间 
	int t = 0;//当前正在运行的进程
	int n = 4;//总作业数 
	
	while(n)
	{

		arriveCheck(syTime);
			
		if(!t)//初始化,第一个进入的作业 
		{
			t = h[hh ++];
			process[t].startTime = syTime;//开始运行时间 
			process[t].st = true;
		} 
		
		print(syTime, t);//显示信息 
		
		if(process[t].runTime <= 0)//当前进程做完,调入就绪序列的下一个
		{
			
			process[t].endTime = syTime;//上个作业结束运行时间
			 
			if(tt > hh)//就绪队列中有两个以上的作业,排序后,调用优先级最高的那个 
			{
				int i = hh;//优先级最高的作业的下标
				for(int j = hh + 1; j <= tt; j ++)//找到优先级最高的作业的下标 
						if(process[h[i]].priority < process[h[j]].priority) i = j;
						
				t = h[i];//当前作业在process中的下标		
				
				if(i == tt) tt --;//利用覆盖法,把该作业从就绪队列中删除,因为它已经在做了 
				else
				{
					for(int j = i + 1; j <= tt; j ++) h[j - 1] = h[j];
					tt --;
				}

			}
			else t = h[hh ++];

			process[t].startTime = syTime;//该作业开始运行时间 
			n --; 
		}
		
		syTime ++;
		process[t].runTime --;
	}
	
	printResult();
	
	return 0;
}

 

 

 

  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的使用C++实现抢占优先级调度算法的代码: ``` #include <iostream> #include <queue> using namespace std; // 定义进程结构体 struct Process { int id; // 进程id int priority; // 进程优先级 int burst_time; // 进程执行时间 }; // 定义比较函数,用于进程优先级的比较 struct ComparePriority { bool operator()(const Process& p1, const Process& p2) { return p1.priority < p2.priority; } }; int main() { // 创建一个进程队列 priority_queue<Process, vector<Process>, ComparePriority> pq; // 添加进程到队列中 pq.push({1, 3, 10}); pq.push({2, 2, 5}); pq.push({3, 1, 8}); // 循环执行进程直到队列为空 while (!pq.empty()) { Process p = pq.top(); pq.pop(); // 执行进程 cout << "Running process " << p.id << " with priority " << p.priority << " and burst time " << p.burst_time << endl; // 模拟进程执行 for (int i = 0; i < p.burst_time; i++) { // do something } } return 0; } ``` 以上代码中,我们使用了STL中的`priority_queue`容器来实现进程的优先级调度。在定义`priority_queue`时,我们使用了自定义的比较函数`ComparePriority`,它用于比较进程的优先级。在主函数中,我们首先创建了一个进程队列,并添加了三个进程到队列中。然后,我们循环执行进程,每次从队列中取出优先级最高的进程,并执行它。在这个简单的示例中,每个进程都是顺序执行的,但在实际应用中,我们可能需要使用并发编程技术来实现多个进程的并行执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值