进程调度算法c++我要五种
时间: 2023-11-24 17:57:39 浏览: 80
以下是五种进程调度算法的 C++ 代码实现:
1. 先来先服务(FCFS)调度算法
```cpp
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_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++) {
Process process = processes[i];
if (current_time < process.arrival_time) {
current_time = process.arrival_time; // 当前时间更新为进程到达时间
}
process.waiting_time = current_time - process.arrival_time;
process.turnaround_time = process.waiting_time + process.burst_time;
current_time += process.burst_time; // 当前时间更新为进程执行结束时间
q.push(process);
}
while (!q.empty()) {
Process process = q.front();
q.pop();
cout << "PID: " << process.pid << ", Waiting Time: " << process.waiting_time << ", Turnaround Time: " << process.turnaround_time << endl;
}
}
int main() {
vector<Process> processes = {{1, 0, 10}, {2, 5, 4}, {3, 6, 2}, {4, 8, 6}};
fcfs(processes);
return 0;
}
```
2. 最短作业优先(SJF)调度算法
```cpp
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
struct CompareProcess {
bool operator()(Process& p1, Process& p2) {
return p1.burst_time > p2.burst_time; // 按执行时间从小到大排序
}
};
void sjf(vector<Process>& processes) {
int n = processes.size();
priority_queue<Process, vector<Process>, CompareProcess> q;
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
for (int i = 0; i < n; i++) {
Process process = processes[i];
if (current_time < process.arrival_time) {
current_time = process.arrival_time; // 当前时间更新为进程到达时间
}
process.waiting_time = current_time - process.arrival_time;
total_waiting_time += process.waiting_time;
process.turnaround_time = process.waiting_time + process.burst_time;
total_turnaround_time += process.turnaround_time;
current_time += process.burst_time; // 当前时间更新为进程执行结束时间
q.push(process);
}
while (!q.empty()) {
Process process = q.top();
q.pop();
cout << "PID: " << process.pid << ", Waiting Time: " << process.waiting_time << ", Turnaround Time: " << process.turnaround_time << endl;
}
cout << "Average Waiting Time: " << (double)total_waiting_time / n << endl;
cout << "Average Turnaround Time: " << (double)total_turnaround_time / n << endl;
}
int main() {
vector<Process> processes = {{1, 0, 10}, {2, 5, 4}, {3, 6, 2}, {4, 8, 6}};
sjf(processes);
return 0;
}
```
3. 最短剩余时间优先(SRTF)调度算法
```cpp
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
struct CompareProcess {
bool operator()(Process& p1, Process& p2) {
return p1.remaining_time > p2.remaining_time; // 按剩余执行时间从小到大排序
}
};
void srtf(vector<Process>& processes) {
int n = processes.size();
priority_queue<Process, vector<Process>, CompareProcess> q;
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
int completed_processes = 0;
while (completed_processes < n) {
for (int i = 0; i < n; i++) {
Process& process = processes[i];
if (process.arrival_time <= current_time && process.remaining_time > 0) {
q.push(process); // 将到达且未执行完的进程加入优先队列
}
}
if (!q.empty()) {
Process& process = q.top();
q.pop();
process.remaining_time--;
if (process.remaining_time == 0) {
completed_processes++;
process.waiting_time = current_time - process.arrival_time - process.burst_time + 1;
total_waiting_time += process.waiting_time;
process.turnaround_time = process.waiting_time + process.burst_time;
total_turnaround_time += process.turnaround_time;
} else {
q.push(process); // 将未执行完的进程再次加入优先队列
}
}
current_time++;
}
for (int i = 0; i < n; i++) {
cout << "PID: " << processes[i].pid << ", Waiting Time: " << processes[i].waiting_time << ", Turnaround Time: " << processes[i].turnaround_time << endl;
}
cout << "Average Waiting Time: " << (double)total_waiting_time / n << endl;
cout << "Average Turnaround Time: " << (double)total_turnaround_time / n << endl;
}
int main() {
vector<Process> processes = {{1, 0, 10}, {2, 5, 4}, {3, 6, 2}, {4, 8, 6}};
for (int i = 0; i < processes.size(); i++) {
processes[i].remaining_time = processes[i].burst_time; // 初始剩余执行时间等于执行时间
}
srtf(processes);
return 0;
}
```
4. 时间片轮转(RR)调度算法
```cpp
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
void rr(vector<Process>& processes, int quantum) {
int n = processes.size();
queue<Process> q;
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
int completed_processes = 0;
while (completed_processes < n) {
for (int i = 0; i < n; i++) {
Process& process = processes[i];
if (process.arrival_time <= current_time && process.remaining_time > 0) {
q.push(process); // 将到达且未执行完的进程加入队列
}
}
if (!q.empty()) {
Process process = q.front();
q.pop();
int execute_time = min(process.remaining_time, quantum); // 执行时间为剩余执行时间和时间片长的较小值
process.remaining_time -= execute_time;
current_time += execute_time; // 当前时间增加执行时间
if (process.remaining_time == 0) {
completed_processes++;
process.waiting_time = current_time - process.arrival_time - process.burst_time;
total_waiting_time += process.waiting_time;
process.turnaround_time = process.waiting_time + process.burst_time;
total_turnaround_time += process.turnaround_time;
} else {
q.push(process); // 将未执行完的进程再次加入队列
}
} else {
current_time++; // 如果队列为空,则时间增加1,等待下一个进程到达
}
}
for (int i = 0; i < n; i++) {
cout << "PID: " << processes[i].pid << ", Waiting Time: " << processes[i].waiting_time << ", Turnaround Time: " << processes[i].turnaround_time << endl;
}
cout << "Average Waiting Time: " << (double)total_waiting_time / n << endl;
cout << "Average Turnaround Time: " << (double)total_turnaround_time / n << endl;
}
int main() {
vector<Process> processes = {{1, 0, 10}, {2, 5, 4}, {3, 6, 2}, {4, 8, 6}};
for (int i = 0; i < processes.size(); i++) {
processes[i].remaining_time = processes[i].burst_time; // 初始剩余执行时间等于执行时间
}
int quantum = 2;
rr(processes, quantum);
return 0;
}
```
5. 优先级调度算法
```cpp
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
struct CompareProcess {
bool operator()(Process& p1, Process& p2) {
return p1.priority > p2.priority; // 按优先级从小到大排序
}
};
void priority(vector<Process>& processes) {
int n = processes.size();
priority_queue<Process, vector<Process>, CompareProcess> q;
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
int completed_processes = 0;
while (completed_processes < n) {
for (int i = 0; i < n; i++) {
Process& process = processes[i];
if (process.arrival_time <= current_time && process.burst_time > 0) {
q.push(process); // 将到达且未执行完的进程加入优先队列
}
}
if (!q.empty()) {
Process process = q.top();
q.pop();
process.burst_time--;
current_time++; // 当前时间增加1
if (process.burst_time == 0) {
completed_processes++;
process.waiting_time = current_time - process.arrival_time - process.turnaround_time;
total_waiting_time += process.waiting_time;
process.turnaround_time = current_time - process.arrival_time;
total_turnaround_time += process.turnaround_time;
} else {
q.push(process); // 将未执行完的进程再次加入优先队列
}
} else {
current_time++; // 如果队列为空,则时间增加1,等待下一个进程到达
}
}
for (int i = 0; i < n; i++) {
cout << "PID: " << processes[i].pid << ", Waiting Time: " << processes[i].waiting_time << ", Turnaround Time: " << processes[i].turnaround_time << endl;
}
cout << "Average Waiting Time: " << (double)total_waiting_time / n << endl;
cout << "Average Turnaround Time: " << (double)total_turnaround_time / n << endl;
}
int main() {
vector<Process> processes = {{1, 0, 10, 3}, {2, 5, 4, 1}, {3, 6, 2, 4}, {4, 8, 6, 2}};
priority(processes);
return 0;
}
```
阅读全文