3
1
SystemsProgrammingAndOperat.../Codes/RR.cpp
2024-08-21 01:16:26 +05:30

68 lines
1.8 KiB
C++
Executable File

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Process {
int id;
int burstTime;
int remainingTime;
int waitingTime;
int turnaroundTime;
};
void roundRobinScheduling(vector<Process> &processes, int quantum) {
int n = processes.size();
queue<int> readyQueue;
vector<int> completionTimes(n, 0);
int currentTime = 0;
for (int i = 0; i < n; ++i) {
readyQueue.push(i);
processes[i].remainingTime = processes[i].burstTime;
}
while (!readyQueue.empty()) {
int currentProcessId = readyQueue.front();
readyQueue.pop();
if (processes[currentProcessId].remainingTime <= quantum) {
currentTime += processes[currentProcessId].remainingTime;
processes[currentProcessId].remainingTime = 0;
processes[currentProcessId].turnaroundTime = currentTime;
processes[currentProcessId].waitingTime = processes[currentProcessId].turnaroundTime - processes[currentProcessId].burstTime;
completionTimes[currentProcessId] = currentTime;
} else {
currentTime += quantum;
processes[currentProcessId].remainingTime -= quantum;
readyQueue.push(currentProcessId);
}
}
cout<<"Time Quantum = 4";
cout << "Process ID\tBurst Time\tWaiting Time\tTurnaround Time\n";
for (int i = 0; i < n; ++i) {
cout << processes[i].id << "\t\t" << processes[i].burstTime << "\t\t" << processes[i].waitingTime
<< "\t\t" << processes[i].turnaroundTime << '\n';
}
}
int main() {
vector<Process> processes = {
{1, 6, 0, 0, 0},
{2, 8, 0, 0, 0},
{3, 7, 0, 0, 0},
{4, 3, 0, 0, 0}
};
int timeQuantum = 4;
roundRobinScheduling(processes, timeQuantum);
return 0;
}