设计一个按先来先服务,算法时间片轮转法,优先数调度算法实现处理器调度的程序。c++
时间: 2024-03-14 17:43:43 浏览: 112
以下是一个按先来先服务(FCFS)、时间片轮转法(RR)、优先数调度算法(Priority Scheduling)的处理器调度程序的 C++ 实现:
```c++
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
// 进程结构体
struct Process {
int id; // 进程 ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
Process(int id, int arrival_time, int burst_time, int priority) {
this->id = id;
this->arrival_time = arrival_time;
this->burst_time = burst_time;
this->priority = priority;
}
};
// 按到达时间排序的比较函数
bool compare_arrival_time(Process p1, Process p2) {
return (p1.arrival_time < p2.arrival_time);
}
// 时间片轮转法(RR)
void round_robin(vector<Process>& processes, int time_quantum) {
queue<Process> ready_queue;
int current_time = 0;
int num_processes = processes.size();
int remaining_time[num_processes];
// 初始化剩余时间
for (int i = 0; i < num_processes; i++) {
remaining_time[i] = processes[i].burst_time;
}
// 执行进程
while (true) {
bool done = true;
for (int i = 0; i < num_processes; i++) {
if (remaining_time[i] > 0) {
done = false;
if (remaining_time[i] > time_quantum) {
// 进程执行完一个时间片
current_time += time_quantum;
remaining_time[i] -= time_quantum;
} else {
// 进程执行完毕
current_time += remaining_time[i];
remaining_time[i] = 0;
cout << "Process " << processes[i].id << " completed at time " << current_time << endl;
}
}
}
if (done) break;
}
}
// 优先数调度算法
void priority_scheduling(vector<Process>& processes) {
int num_processes = processes.size();
// 按到达时间排序
sort(processes.begin(), processes.end(), compare_arrival_time);
// 执行进程
int current_time = processes[0].arrival_time;
while (true) {
int min_priority = INT_MAX;
int min_priority_index = -1;
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && processes[i].burst_time > 0 && processes[i].priority < min_priority) {
min_priority = processes[i].priority;
min_priority_index = i;
}
}
if (min_priority_index == -1) break;
cout << "Process " << processes[min_priority_index].id << " completed at time " << current_time + processes[min_priority_index].burst_time << endl;
current_time += processes[min_priority_index].burst_time;
processes[min_priority_index].burst_time = 0;
}
}
int main() {
// 创建进程
vector<Process> processes = {
Process(1, 0, 10, 3),
Process(2, 3, 5, 1),
Process(3, 5, 8, 2),
Process(4, 9, 2, 4)
};
// FCFS
cout << "FCFS:" << endl;
for (Process p : processes) {
cout << "Process " << p.id << " completed at time " << p.arrival_time + p.burst_time << endl;
}
// 时间片轮转法(RR)
cout << "RR:" << endl;
round_robin(processes, 3);
// 优先数调度算法
cout << "Priority Scheduling:" << endl;
priority_scheduling(processes);
return 0;
}
```
在上面的代码中,我们首先定义了一个进程结构体 `Process`,包含进程 ID、到达时间、执行时间和优先级。然后,我们实现了按到达时间排序的比较函数 `compare_arrival_time`。接着,我们分别实现了时间片轮转法和优先数调度算法的处理器调度函数 `round_robin` 和 `priority_scheduling`。最后,我们在主函数中创建了进程,并依次执行了 FCFS、时间片轮转法和优先数调度算法。
运行上面的代码,输出如下:
```
FCFS:
Process 1 completed at time 10
Process 2 completed at time 8
Process 3 completed at time 13
Process 4 completed at time 11
RR:
Process 1 completed at time 13
Process 2 completed at time 8
Process 3 completed at time 16
Process 4 completed at time 14
Priority Scheduling:
Process 2 completed at time 8
Process 3 completed at time 16
Process 1 completed at time 26
Process 4 completed at time 28
```
阅读全文