先来先服务进程调度c++代码
时间: 2023-10-31 22:07:30 浏览: 84
以下是一个简单的先来先服务进程调度的C++代码:
```c++
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
};
bool operator<(const Process& p1, const Process& p2) {
return p1.arrival_time > p2.arrival_time;
}
void FCFS(vector<Process>& processes) {
priority_queue<Process> pq;
int current_time = 0;
double total_waiting_time = 0;
for (int i = 0; i < processes.size(); i++) {
pq.push(processes[i]);
}
while (!pq.empty()) {
Process p = pq.top();
pq.pop();
current_time += p.burst_time;
total_waiting_time += current_time - p.arrival_time - p.burst_time;
}
cout << "平均等待时间:" << total_waiting_time / processes.size() << endl;
}
int main() {
vector<Process> processes = {{1, 0, 10}, {2, 3, 5}, {3, 5, 8}, {4, 7, 3}};
FCFS(processes);
return 0;
}
```
阅读全文