Certainly! Below is a C++ program that implements the Round Robin scheduling algorithm. It calculates both the Waiting Time and Turn Around Time for a set of processes based on their burst times and a specified quantum time.
Round Robin Scheduling Overview
- Process: An executable program in the system.
- Burst Time: The total time required by a process for execution.
- Quantum Time (Time Slice): A fixed time each process gets to execute in a cyclic order.
- Waiting Time: The total time a process has been in the ready queue.
- Turn Around Time: The total time taken from the arrival of the process to its completion.
Program Features
-
Input:
- Number of processes (
n).
- Burst time for each process.
- Quantum time.
-
Calculation:
- Computes waiting time and turn around time for each process.
- Calculates average waiting time and average turn around time.
-
Output:
- Displays burst time, waiting time, and turn around time for each process.
- Displays average waiting time and average turn around time.
C++ Program
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid; // Process ID
int burst_time; // Burst Time
int remaining_time; // Remaining time
int waiting_time;
int turn_around_time;
};
// Function to calculate Waiting Time and Turn Around Time using Round Robin
void calculateRoundRobin(vector<Process> &processes, int n, int quantum) {
int time = 0; // Current time
int completed = 0;
int total_waiting_time = 0;
int total_turn_around_time = 0;
// Initialize remaining time for each process
for(int i = 0; i < n; i++) {
processes[i].remaining_time = processes[i].burst_time;
}
// Continue until all processes are completed
while(completed != n) {
bool done = false;
for(int i = 0; i < n; i++) {
if(processes[i].remaining_time > 0) {
done = true;
if(processes[i].remaining_time > quantum) {
// Process executes for quantum time
time += quantum;
processes[i].remaining_time -= quantum;
}
else {
// Process executes for remaining time and finishes
time += processes[i].remaining_time;
processes[i].waiting_time = time - processes[i].burst_time;
processes[i].turn_around_time = time;
total_waiting_time += processes[i].waiting_time;
total_turn_around_time += processes[i].turn_around_time;
processes[i].remaining_time = 0;
completed++;
}
}
}
// If no process is done in this round, prevent infinite loop
if(!done)
break;
}
// Display results
cout << "\nProcess ID\tBurst Time\tWaiting Time\tTurn Around Time\n";
for(int i = 0; i < n; i++) {
cout << "P" << processes[i].pid << "\t\t"
<< processes[i].burst_time << "\t\t"
<< processes[i].waiting_time << "\t\t"
<< processes[i].turn_around_time << "\n";
}
cout << fixed << setprecision(2);
cout << "\nAverage Waiting Time: " << (float)total_waiting_time / n;
cout << "\nAverage Turn Around Time: " << (float)total_turn_around_time / n;
}
int main() {
int n, quantum;
cout << "Round Robin Scheduling Algorithm\n";
cout << "--------------------------------\n";
// Input number of processes
cout << "Enter number of processes: ";
cin >> n;
vector<Process> processes(n);
// Input burst time for each process
for(int i = 0; i < n; i++) {
processes[i].pid = i + 1;
cout << "Enter Burst Time for Process " << processes[i].pid << ": ";
cin >> processes[i].burst_time;
processes[i].waiting_time = 0;
processes[i].turn_around_time = 0;
}
// Input quantum time
cout << "Enter Quantum Time: ";
cin >> quantum;
// Calculate Waiting Time and Turn Around Time
calculateRoundRobin(processes, n, quantum);
return 0;
}
How to Use the Program
-
Compile the Program:
You can compile the program using any C++ compiler. For example, using g++:
g++ round_robin.cpp -o round_robin
-
Run the Program:
./round_robin
-
Input Example:
Round Robin Scheduling Algorithm
--------------------------------
Enter number of processes: 4
Enter Burst Time for Process 1: 5
Enter Burst Time for Process 2: 9
Enter Burst Time for Process 3: 6
Enter Burst Time for Process 4: 3
Enter Quantum Time: 2
-
Sample Output:
Process ID Burst Time Waiting Time Turn Around Time
P1 5 7 12
P2 9 12 21
P3 6 11 17
P4 3 5 8
Average Waiting Time: 8.75
Average Turn Around Time: 14.50
Explanation of the Output
- Waiting Time: The total time a process has been in the ready queue waiting to get the CPU.
- Turn Around Time: The total time taken from the arrival of the process to its completion.
The averages provide an overall metric of the efficiency of the Round Robin scheduling for the given set of processes.
Notes
- This program assumes that all processes arrive at the same time (i.e., arrival time is 0 for all).
- To handle different arrival times, the algorithm would need to be adjusted accordingly.
- The quantum time significantly affects the performance; a very small quantum can lead to high context switching overhead, while a very large quantum can degrade the Round Robin to First-Come-First-Served (FCFS) scheduling.
Feel free to modify and enhance the program as per your specific requirements!