Write the code in C++ aboutProcess Scheduling Algorithm Simulation:1、 Simulate the operation of the round-robin algorithm for process scheduling. 2、 Create at least 15 processes and output their scheduling situation under the scheduling algorithm mentioned above and output it to the terminal to check the execution of the algorithm. 3、 The output should include the arrival time of the processes, the end time, and the average execution time.
时间: 2024-01-26 11:05:21 浏览: 168
Here's a possible implementation of the round-robin algorithm simulation in C++:
```cpp
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Process {
int id;
int arrival_time;
int burst_time;
int remaining_time;
int end_time;
int turnaround_time;
int waiting_time;
};
void simulate_rr(vector<Process>& processes, int quantum) {
queue<Process*> ready_queue;
int current_time = 0;
int total_burst_time = 0;
int n = processes.size();
for (int i = 0; i < n; i++) {
total_burst_time += processes[i].burst_time;
}
while (total_burst_time > 0) {
// Add arrived processes to ready queue
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0) {
ready_queue.push(&processes[i]);
}
}
if (!ready_queue.empty()) {
// Execute process for quantum or until completion
Process* current_process = ready_queue.front();
ready_queue.pop();
int execution_time = min(current_process->remaining_time, quantum);
current_process->remaining_time -= execution_time;
total_burst_time -= execution_time;
current_time += execution_time;
// Check if process is done or needs to go back to ready queue
if (current_process->remaining_time == 0) {
current_process->end_time = current_time;
current_process->turnaround_time = current_process->end_time - current_process->arrival_time;
current_process->waiting_time = current_process->turnaround_time - current_process->burst_time;
} else {
ready_queue.push(current_process);
}
} else {
// No processes ready, advance time
current_time++;
}
}
}
void print_processes(const vector<Process>& processes) {
int n = processes.size();
int total_turnaround_time = 0;
int total_waiting_time = 0;
cout << "ID\tArrival\tBurst\tEnd\tTurnaround\tWaiting" << endl;
for (int i = 0; i < n; i++) {
const Process& p = processes[i];
total_turnaround_time += p.turnaround_time;
total_waiting_time += p.waiting_time;
cout << p.id << "\t" << p.arrival_time << "\t" << p.burst_time << "\t"
<< p.end_time << "\t" << p.turnaround_time << "\t\t" << p.waiting_time << endl;
}
cout << "Average turnaround time: " << (double) total_turnaround_time / n << endl;
cout << "Average waiting time: " << (double) total_waiting_time / n << endl;
}
int main() {
vector<Process> processes = {
{1, 0, 10, 10, 0, 0, 0},
{2, 1, 5, 5, 0, 0, 0},
{3, 2, 3, 3, 0, 0, 0},
{4, 3, 8, 8, 0, 0, 0},
{5, 4, 6, 6, 0, 0, 0},
{6, 5, 2, 2, 0, 0, 0},
{7, 6, 4, 4, 0, 0, 0},
{8, 7, 7, 7, 0, 0, 0},
{9, 8, 1, 1, 0, 0, 0},
{10, 9, 9, 9, 0, 0, 0},
{11, 10, 5, 5, 0, 0, 0},
{12, 11, 3, 3, 0, 0, 0},
{13, 12, 6, 6, 0, 0, 0},
{14, 13, 4, 4, 0, 0, 0},
{15, 14, 2, 2, 0, 0, 0},
};
int quantum = 3;
simulate_rr(processes, quantum);
print_processes(processes);
return 0;
}
```
This program simulates the round-robin algorithm for process scheduling using a queue to represent the ready queue of processes. The `simulate_rr` function takes a vector of `Process` objects and a quantum as input, and updates the remaining time, end time, turnaround time, and waiting time of each process. The `print_processes` function outputs the scheduling information for each process, including the arrival time, burst time, end time, turnaround time, and waiting time, as well as the average turnaround time and average waiting time for all processes. The main function creates 15 processes with different arrival and burst times, and calls the simulation and print functions with a quantum of 3.
阅读全文