操作系统先来先服务调度算法c++代码
时间: 2023-11-01 19:07:25 浏览: 227
好的,以下是操作系统先来先服务调度算法的C++代码:
```c++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
bool cmp(Process a, Process b) {
return a.arrival_time < b.arrival_time;
}
void FCFS(vector<Process>& processes) {
sort(processes.begin(), processes.end(), cmp); // 按到达时间排序
int n = processes.size();
int current_time = 0;
for (int i = 0; i < n; i++) {
if (current_time < processes[i].arrival_time) {
current_time = processes[i].arrival_time;
}
processes[i].waiting_time = current_time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
current_time += processes[i].burst_time;
}
}
int main() {
vector<Process> processes = {{1, 0, 5}, {2, 1, 3}, {3, 2, 8}, {4, 3, 6}, {5, 4, 4}};
FCFS(processes);
cout << "进程ID\t到达时间\t执行时间\t等待时间\t周转时间" << endl;
for (auto process : processes) {
cout << process.pid << "\t" << process.arrival_time << "\t\t" << process.burst_time << "\t\t" << process.waiting_time << "\t\t" << process.turnaround_time << endl;
}
return 0;
}
```
阅读全文