优先级调度算法(PSA)

优先级调度算法,即给每个作业一个优先级,优先级越高越紧迫,应该先执行。

FCFS算法和SJF算法都可以看做一种特殊的PSA算法。

例如,在FCFS算法里,是将算法的到达时间当做优先级,先到达的优先级高。

在SJF算法里,将作业的服务时间看做优先级,服务时间短的优先级高。




//main.cpp
#include "PSA.h"

int main()
{
	std::vector<PCB> PCBList;

	//输入作业信息
	InputPCB(PCBList);

	//PSA算法
	PSA(PCBList);

	//显示结果
	show(PCBList);

	return 0;
}

//PSA.h
#ifndef PSA_H_
#define PSA_H_

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>

//作业结构体
typedef struct PCB
{
	int ID;							//标识符
	int Level;						//优先级
	int ComeTime;					//到达时间
	int ServerTime;					//服务时间
	int FinishTime;					//完成时间
	int TurnoverTime;				//周转时间
	double WeightedTurnoverTime;	//带权周转时间
}PCB;


/*
函数功能:输入作业信息
参数说明:
PCBList		std::vector<PCB>&
  • 7
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
优先级调度算法(Priority Scheduling Algorithm)是一种常用的进程调度算法,它根据进程的优先级来确定下一个要执行的进程。优先级越高的进程,被分配的CPU时间片越多。 以下是一个使用C++实现的简单优先级调度算法的代码示例: ```cpp #include <iostream> #include <queue> using namespace std; // 进程结构体 struct Process { int id; // 进程ID int priority; // 进程优先级 int burstTime; // 进程执行时间 }; // 优先级比较函数,用于定义进程的优先级比较规则 struct ComparePriority { bool operator()(const Process& p1, const Process& p2) { return p1.priority > p2.priority; } }; // 优先级调度函数 void priorityScheduling(vector<Process>& processes) { // 创建一个优先队列,按照进程的优先级进行排序 priority_queue<Process, vector<Process>, ComparePriority> pq; // 将所有进程加入优先队列 for (const auto& process : processes) { pq.push(process); } // 执行进程调度 while (!pq.empty()) { Process currentProcess = pq.top(); pq.pop(); cout << "执行进程 " << currentProcess.id << endl; // 执行进程的任务 for (int i = 0; i < currentProcess.burstTime; i++) { // 模拟进程执行 } } } int main() { // 创建进程列表 vector<Process> processes = { {1, 3, 5}, {2, 1, 3}, {3, 2, 4}, {4, 4, 2} }; // 执行优先级调度算法 priorityScheduling(processes); return 0; } ``` 上述代码中,我们定义了一个进程结构体 `Process`,包含进程的ID、优先级和执行时间。然后,我们使用优先队列 `priority_queue` 来按照进程的优先级进行排序。在 `priorityScheduling` 函数中,我们将所有进程加入优先队列,并按照优先级从高到低执行进程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值