用c++实现多种进程调度算法
时间: 2024-06-01 20:02:43 浏览: 146
在操作系统中,进程调度算法用于决定哪个进程将获得处理器的控制权。以下是使用C++实现多种进程调度算法的示例:
1. 先来先服务(FCFS)调度算法
```c++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Process {
int pid; // 进程ID
int burst_time; // 进程需要的CPU时间
int arrival_time; // 进程到达时间
int waiting_time; // 进程等待时间
int turnaround_time; // 进程周转时间
};
void fcfs(vector<Process> processes) {
int n = processes.size();
queue<Process> q;
int current_time = 0; // 当前时间
for (int i = 0; i < n; i++) {
while (!q.empty() && q.front().burst_time <= current_time) {
Process p = q.front();
q.pop();
p.turnaround_time = current_time - p.arrival_time;
p.waiting_time = p.turnaround_time - p.burst_time;
cout << "进程 " << p.pid << " 周转时间为 " << p.turnaround_time
<< " 等待时间为 " << p.waiting_time << endl;
}
q.push(processes[i]);
current_time += processes[i].burst_time;
}
while (!q.empty()) {
Process p = q.front();
q.pop();
p.turnaround_time = current_time - p.arrival_time;
p.waiting_time = p.turnaround_time - p.burst_time;
cout << "进程 " << p.pid << " 周转时间为 " << p.turnaround_time
<< " 等待时间为 " << p.waiting_time << endl;
current_time += p.burst_time;
}
}
int main() {
vector<Process> processes = {{1, 10, 0, 0}, {2, 5, 1, 0}, {3, 8, 2, 0}};
fcfs(processes);
return 0;
}
```
2. 最短作业优先(SJF)调度算法
```c++
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Process {
int pid; // 进程ID
int burst_time; // 进程需要的CPU时间
int arrival_time; // 进程到达时间
int waiting_time; // 进程等待时间
int turnaround_time; // 进程周转时间
};
bool compare(Process a, Process b) {
return a.burst_time < b.burst_time;
}
void sjf(vector<Process> processes) {
int n = processes.size();
sort(processes.begin(), processes.end(), compare);
queue<Process> q;
int current_time = 0; // 当前时间
for (int i = 0; i < n; i++) {
while (!q.empty() && q.front().burst_time <= current_time) {
Process p = q.front();
q.pop();
p.turnaround_time = current_time - p.arrival_time;
p.waiting_time = p.turnaround_time - p.burst_time;
cout << "进程 " << p.pid << " 周转时间为 " << p.turnaround_time
<< " 等待时间为 " << p.waiting_time << endl;
}
q.push(processes[i]);
current_time += processes[i].burst_time;
}
while (!q.empty()) {
Process p = q.front();
q.pop();
p.turnaround_time = current_time - p.arrival_time;
p.waiting_time = p.turnaround_time - p.burst_time;
cout << "进程 " << p.pid << " 周转时间为 " << p.turnaround_time
<< " 等待时间为 " << p.waiting_time << endl;
current_time += p.burst_time;
}
}
int main() {
vector<Process> processes = {{1, 10, 0, 0}, {2, 5, 1, 0}, {3, 8, 2, 0}};
sjf(processes);
return 0;
}
```
3. 时间片轮转(RR)调度算法
```c++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Process {
int pid; // 进程ID
int burst_time; // 进程需要的CPU时间
int arrival_time; // 进程到达时间
int waiting_time; // 进程等待时间
int turnaround_time; // 进程周转时间
};
void rr(vector<Process> processes, int time_slice) {
int n = processes.size();
queue<Process> q;
int current_time = 0; // 当前时间
int i = 0; // 当前进程的下标
while (!q.empty() || i < n) {
if (!q.empty()) {
Process p = q.front();
q.pop();
int burst_time = min(p.burst_time, time_slice);
p.burst_time -= burst_time;
current_time += burst_time;
for (; i < n && processes[i].arrival_time <= current_time; i++) {
q.push(processes[i]);
}
if (p.burst_time == 0) {
p.turnaround_time = current_time - p.arrival_time;
p.waiting_time = p.turnaround_time - p.burst_time;
cout << "进程 " << p.pid << " 周转时间为 " << p.turnaround_time
<< " 等待时间为 " << p.waiting_time << endl;
} else {
q.push(p);
}
} else {
q.push(processes[i]);
current_time = processes[i].arrival_time;
i++;
}
}
}
int main() {
vector<Process> processes = {{1, 10, 0, 0}, {2, 5, 1, 0}, {3, 8, 2, 0}};
rr(processes, 2);
return 0;
}
```
以上是使用C++实现三种进程调度算法的示例。实现其他进程调度算法时,只需根据算法的特点和规则修改以上代码即可。
阅读全文