Round Robin Code In C++ With Gantt Chart
Round Robin is a popular scheduling algorithm that is used in operating systems. It is a preemptive algorithm that is used to schedule processes in a way that each process gets an equal amount of CPU time. The Round Robin algorithm is a time-sharing algorithm that is designed to handle multiple processes simultaneously.
How Does Round Robin Algorithm Work?
The Round Robin algorithm works by dividing the CPU time into small time slices. Each process is assigned a time slice, and the CPU switches between processes after the time slice expires. This ensures that each process gets a fair share of the CPU time, and no process is starved of CPU time.
The Round Robin algorithm is implemented using a circular queue. Each process is added to the end of the queue, and the CPU picks the process at the front of the queue. The process is then executed for a fixed time slice, and if the process is not completed, it is moved to the end of the queue. The CPU then picks the next process in the queue, and the process is executed for the same time slice. This process continues until all processes have been completed.
Implementing Round Robin Algorithm in C++
Here is the code to implement the Round Robin algorithm in C++:
#include <iostream>#include <queue>using namespace std;struct Process {int id;int burstTime;};int main() {queue<Process> readyQueue;int timeSlice = 2;int totalTime = 0;int numOfProcesses = 5;Process p[numOfProcesses] = {{1, 6},{2, 3},{3, 1},{4, 7},{5, 5}};for(int i=0; i<numOfProcesses; i++) {readyQueue.push(p[i]);totalTime += p[i].burstTime;}cout << "Gantt Chart:" << endl;while(!readyQueue.empty()) {Process currentProcess = readyQueue.front();readyQueue.pop();cout << "P" << currentProcess.id << " | ";totalTime -= timeSlice;if(currentProcess.burstTime > timeSlice) {currentProcess.burstTime -= timeSlice;readyQueue.push(currentProcess);totalTime += timeSlice;} else {totalTime += currentProcess.burstTime;}}cout << endl << "Total time taken: " << totalTime << endl;return 0;}
The above code creates a queue of processes, initializes the time slice and total time variables, and sets the number of processes. The code then adds the processes to the queue, calculates the total time, and prints the Gantt chart. The code then picks each process from the queue, executes it for a time slice, and moves it to the end of the queue if it is not completed. The code then prints the total time taken.
Gantt Chart
The Gantt chart is a graphical representation of the Round Robin algorithm. It shows the execution of each process, the time slice, and the total time taken. The Gantt chart is useful in visualizing the scheduling of processes and identifying any bottlenecks or inefficiencies in the system.
Conclusion
The Round Robin algorithm is a simple and effective scheduling algorithm that is used in operating systems. It ensures that each process gets a fair share of CPU time and prevents any process from being starved of CPU time. The Round Robin algorithm is implemented using a circular queue and a fixed time slice. The Gantt chart is a useful tool in visualizing the scheduling of processes and identifying any inefficiencies in the system.
Meta Description: Round Robin is a popular scheduling algorithm that is used in operating systems. Here is the code to implement the Round Robin algorithm in C++ with a Gantt chart. Meta Keywords: Round Robin, scheduling algorithm, operating systems, C++, Gantt chart.