优先级调度算法C++
时间: 2023-11-14 17:06:43 浏览: 163
优先级调度算法是一种用于操作系统进程调度的算法。它根据进程的优先级来确定执行顺序,优先级较高的进程会被优先执行。下面是一个简单的优先级调度算法的伪代码示例:
```
1. 初始化就绪队列和优先级队列
2. 循环直到就绪队列为空:
3. 从就绪队列中选择优先级最高的进程
4. 执行该进程
5. 更新进程的优先级(如果需要)
6. 将执行完的进程从就绪队列中移除
7. 结束循环
```
在这个算法中,首先需要初始化就绪队列和优先级队列。就绪队列用于存储可以立即执行的进程,而优先级队列用于按照优先级进行排序。然后,不断选择优先级最高的进程执行,执行完毕后更新进程的优先级,并将执行完的进程从就绪队列中移除。最后,当就绪队列为空时结束循环。
相关问题
编制时间片轮转调度算法和静态优先级调度算法C++
时间片轮转调度算法的C++代码如下:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Process {
public:
int pid;
int arrival_time;
int burst_time;
int remaining_time;
Process(int pid, int arrival_time, int burst_time) {
this->pid = pid;
this->arrival_time = arrival_time;
this->burst_time = burst_time;
this->remaining_time = burst_time;
}
int execute(int time_slice) {
if (remaining_time > time_slice) {
remaining_time -= time_slice;
return time_slice;
} else {
int time_executed = remaining_time;
remaining_time = 0;
return time_executed;
}
}
};
double round_robin_scheduling(vector<Process>& processes, int time_slice) {
int n = processes.size();
vector<Process> remaining_processes = processes;
int current_time = 0;
int waiting_time = 0;
int turnaround_time = 0;
while (!remaining_processes.empty()) {
Process current_process = remaining_processes[0];
remaining_processes.erase(remaining_processes.begin());
if (current_process.arrival_time > current_time) {
current_time = current_process.arrival_time;
}
int time_executed = current_process.execute(time_slice);
current_time += time_executed;
if (current_process.remaining_time == 0) {
waiting_time += current_time - current_process.arrival_time - current_process.burst_time;
turnaround_time += current_time - current_process.arrival_time;
} else {
remaining_processes.push_back(current_process);
}
}
double avg_waiting_time = (double) waiting_time / n;
double avg_turnaround_time = (double) turnaround_time / n;
return avg_waiting_time;
}
```
静态优先级调度算法的C++代码如下:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Process {
public:
int pid;
int arrival_time;
int burst_time;
int priority;
Process(int pid, int arrival_time, int burst_time, int priority) {
this->pid = pid;
this->arrival_time = arrival_time;
this->burst_time = burst_time;
this->priority = priority;
}
void execute() {
burst_time--;
}
};
double static_priority_scheduling(vector<Process>& processes) {
int n = processes.size();
vector<Process> remaining_processes = processes;
int current_time = 0;
int waiting_time = 0;
int turnaround_time = 0;
while (!remaining_processes.empty()) {
sort(remaining_processes.begin(), remaining_processes.end(), [](Process p1, Process p2){
if (p1.priority == p2.priority) {
return p1.arrival_time < p2.arrival_time;
} else {
return p1.priority > p2.priority;
}
});
Process current_process = remaining_processes[0];
remaining_processes.erase(remaining_processes.begin());
if (current_process.arrival_time > current_time) {
current_time = current_process.arrival_time;
}
current_process.execute();
current_time++;
if (current_process.burst_time == 0) {
waiting_time += current_time - current_process.arrival_time - current_process.priority;
turnaround_time += current_time - current_process.arrival_time;
} else {
remaining_processes.push_back(current_process);
}
}
double avg_waiting_time = (double) waiting_time / n;
double avg_turnaround_time = (double) turnaround_time / n;
return avg_waiting_time;
}
```
非抢占式静态优先级调度算法c++。输入进程个数、进程名称、到达时间、运行时间、优先级,输出完成时间、周转时间和带权周转时间
以下是非抢占式静态优先级调度算法的 C++ 代码实现,包括输入、处理和输出部分:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
struct Process {
string name;
int arrive_time, run_time, priority;
int finish_time, turnaround_time;
double weighted_turnaround_time;
Process(string n, int a, int r, int p) {
name = n;
arrive_time = a;
run_time = r;
priority = p;
}
};
bool compare_arrive_time(Process a, Process b) {
return a.arrive_time < b.arrive_time;
}
bool compare_priority(Process a, Process b) {
return a.priority > b.priority;
}
int main() {
int n;
cout << "请输入进程个数:";
cin >> n;
vector<Process> processes;
for (int i = 0; i < n; i++) {
string name;
int arrive_time, run_time, priority;
cout << "请输入第" << i + 1 << "个进程的名称、到达时间、运行时间和优先级:";
cin >> name >> arrive_time >> run_time >> priority;
processes.push_back(Process(name, arrive_time, run_time, priority));
}
sort(processes.begin(), processes.end(), compare_arrive_time);
int current_time = 0;
for (auto& p : processes) {
current_time = max(current_time, p.arrive_time); // 当前时间为最大的到达时间
p.finish_time = current_time + p.run_time; // 计算完成时间
p.turnaround_time = p.finish_time - p.arrive_time; // 计算周转时间
p.weighted_turnaround_time = p.turnaround_time * 1.0 / p.run_time; // 计算带权周转时间
current_time = p.finish_time; // 更新当前时间
}
sort(processes.begin(), processes.end(), compare_priority);
// 输出结果
cout << "\n进程\t到达时间\t运行时间\t优先级\t完成时间\t周转时间\t带权周转时间\n";
for (auto& p : processes) {
cout << p.name << "\t" << p.arrive_time << "\t" << p.run_time << "\t\t"
<< p.priority << "\t" << p.finish_time << "\t\t" << p.turnaround_time << "\t\t"
<< fixed << setprecision(2) << p.weighted_turnaround_time << endl;
}
return 0;
}
```
输入格式如下所示:
```
请输入进程个数:3
请输入第1个进程的名称、到达时间、运行时间和优先级:P1 0 10 3
请输入第2个进程的名称、到达时间、运行时间和优先级:P2 2 5 1
请输入第3个进程的名称、到达时间、运行时间和优先级:P3 5 8 2
```
输出结果如下所示:
```
进程 到达时间 运行时间 优先级 完成时间 周转时间 带权周转时间
P1 0 10 3 10 10 1.00
P3 5 8 2 18 13 1.62
P2 2 5 1 23 21 4.20
```
阅读全文