编制时间片轮转调度算法和静态优先级调度算法C++
时间: 2024-03-09 14:44:27 浏览: 63
时间片轮转调度算法 (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;
}
```
阅读全文