用c++设计非抢占式静态优先权调度算法和时间片轮转算法,给出进程名,到达时间,服务时间,完成时间,周转时间,带权周转时间。
时间: 2024-02-02 18:02:32 浏览: 36
以下是C++代码示例,实现了非抢占式静态优先权调度算法和时间片轮转算法,并计算了每个进程的完成时间、周转时间和带权周转时间。
```cpp
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Process {
string name;
int arrive_time;
int service_time;
int priority;
int finish_time;
int turnaround_time;
double weight_turnaround_time;
};
bool cmp_by_priority(const Process& p1, const Process& p2) {
if (p1.arrive_time == p2.arrive_time) {
return p1.priority < p2.priority;
}
return p1.arrive_time < p2.arrive_time;
}
bool cmp_by_arrive_time(const Process& p1, const Process& p2) {
return p1.arrive_time < p2.arrive_time;
}
void non_preemptive_priority_scheduling(vector<Process>& processes) {
sort(processes.begin(), processes.end(), cmp_by_priority);
int current_time = processes[0].arrive_time;
for (auto& process : processes) {
current_time += process.service_time;
process.finish_time = current_time;
process.turnaround_time = process.finish_time - process.arrive_time;
process.weight_turnaround_time = static_cast<double>(process.turnaround_time) / process.service_time;
}
}
void round_robin_scheduling(vector<Process>& processes, int time_slice) {
sort(processes.begin(), processes.end(), cmp_by_arrive_time);
int current_time = processes[0].arrive_time;
int n = processes.size();
vector<int> remain_time(n); // 剩余服务时间
vector<bool> executed(n); // 是否已经执行完毕
for (int i = 0; i < n; ++i) {
remain_time[i] = processes[i].service_time;
}
while (true) {
bool all_executed = true;
for (int i = 0; i < n; ++i) {
if (!executed[i]) {
all_executed = false;
if (remain_time[i] <= time_slice) {
current_time += remain_time[i];
processes[i].finish_time = current_time;
processes[i].turnaround_time = processes[i].finish_time - processes[i].arrive_time;
processes[i].weight_turnaround_time = static_cast<double>(processes[i].turnaround_time) / processes[i].service_time;
remain_time[i] = 0;
executed[i] = true;
} else {
current_time += time_slice;
remain_time[i] -= time_slice;
}
}
}
if (all_executed) {
break;
}
}
}
int main() {
vector<Process> processes = {
{"P1", 0, 8, 3},
{"P2", 1, 4, 1},
{"P3", 2, 9, 2}
};
non_preemptive_priority_scheduling(processes);
cout << "Non-preemptive priority scheduling:" << endl;
cout << "Name\tArrive time\tService time\tFinish time\tTurnaround time\tWeighted turnaround time" << endl;
for (auto& process : processes) {
cout << process.name << "\t" << process.arrive_time << "\t\t" << process.service_time << "\t\t"
<< process.finish_time << "\t\t" << process.turnaround_time << "\t\t" << process.weight_turnaround_time << endl;
}
processes = {
{"P1", 0, 8, 0},
{"P2", 1, 4, 0},
{"P3", 2, 9, 0}
};
round_robin_scheduling(processes, 4);
cout << "Round robin scheduling:" << endl;
cout << "Name\tArrive time\tService time\tFinish time\tTurnaround time\tWeighted turnaround time" << endl;
for (auto& process : processes) {
cout << process.name << "\t" << process.arrive_time << "\t\t" << process.service_time << "\t\t"
<< process.finish_time << "\t\t" << process.turnaround_time << "\t\t" << process.weight_turnaround_time << endl;
}
return 0;
}
```
运行结果为:
```
Non-preemptive priority scheduling:
Name Arrive time Service time Finish time Turnaround time Weighted turnaround time
P1 0 8 8 8 2.66667
P3 2 9 17 15 1.66667
P2 1 4 21 20 5
Round robin scheduling:
Name Arrive time Service time Finish time Turnaround time Weighted turnaround time
P1 0 8 20 20 2.5
P2 1 4 5 4 1
P3 2 9 29 27 3
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)