3
1

fixed round robin code

This commit is contained in:
Tanmay 2024-11-02 20:19:01 +05:30
parent 60eccf7b93
commit ccfa65c057

View File

@ -1,88 +1,85 @@
#include<iostream> #include<iostream>
#include<queue> #include<queue>
#include <vector>
#include <iomanip> // For formatting output
using namespace std; using namespace std;
struct process { struct Process{
int burst, arrival, id, completion, priority, waiting, turnaround, response, remainingBurst; int id, burst, arrival, remaining, completion, waiting, turnaround, response;
bool active;
}; };
Process processes[30];
process meh[30];
class RoundRobin{ class RoundRobin{
public: public:
int n; int n, timeQuantum;
int timeQuantum;
void inputProcesses() { void input(){
cout<<"\nEnter number of processes: "; cout<<"\nEnter number of processes: ";
cin>>n; cin>>n;
for (int i = 1; i <= n; i++) { for(int i = 0; i < n; i++){
cout<<"\nEnter arrival time of P"<<i<<": "; cout<<"\nEnter arrival time of P"<<i<<": ";
cin >> meh[i].arrival; cin>>processes[i].arrival;
cout << "\nEnter burst time of P" << i << ": "; cout<<"Enter burst time of P"<<i<<": ";
cin >> meh[i].burst; cin>>processes[i].burst;
cout << "\nEnter priority of P" << i << ": "; processes[i].id = i;
cin >> meh[i].priority; // Priority is not used in RR, but kept here for completeness. processes[i].remaining = processes[i].burst;
meh[i].id = i; processes[i].response = -1;
meh[i].remainingBurst = meh[i].burst;
meh[i].active = false;
} }
cout<<"\nEnter time quantum: "; cout<<"\nEnter time quantum: ";
cin>>timeQuantum; cin>>timeQuantum;
cout << "\n | Arrival | Burst | Priority\n"; }
for (int j = 1; j <= n; j++) {
cout << "P" << j << " | " << meh[j].arrival << " | " << meh[j].burst << " | " << meh[j].priority << "\n"; void process(){
int currentTime = 0;
queue<int> q;
bool inQueue[30] = {false};
int completedProcesses = 0;
for(int i = 0; i < n; i++){
if(processes[i].arrival <= currentTime){
q.push(i);
inQueue[i] = true;
} }
} }
void roundRobinProcess() { while(completedProcesses < n){
int k = 0; // Current time if(q.empty()){
int completed = 0; // Number of completed processes currentTime++;
queue<int> readyQueue; for(int i = 0; i < n; i++){
if(!inQueue[i] && processes[i].arrival <= currentTime){
vector<bool> isProcessed(n + 1, false); // Track whether a process has been added to the ready queue q.push(i);
inQueue[i] = true;
while (completed < n) {
// Add processes that have arrived to the ready queue
for (int i = 1; i <= n; i++) {
if (meh[i].arrival <= k && !isProcessed[i]) {
readyQueue.push(i);
isProcessed[i] = true;
} }
} }
if (readyQueue.empty()) {
// If no process is in the queue, increment time
k++;
continue; continue;
} }
int currentProcess = readyQueue.front(); int idx = q.front();
readyQueue.pop(); q.pop();
// Calculate response time for the process if it starts now if(processes[idx].response == -1){
if (!meh[currentProcess].active) { processes[idx].response = currentTime - processes[idx].arrival;
meh[currentProcess].response = k - meh[currentProcess].arrival;
meh[currentProcess].active = true;
} }
int timeSlice = min(timeQuantum, meh[currentProcess].remainingBurst); if(processes[idx].remaining <= timeQuantum){
currentTime += processes[idx].remaining;
// Process the current process processes[idx].remaining = 0;
meh[currentProcess].remainingBurst -= timeSlice; processes[idx].completion = currentTime;
k += timeSlice; processes[idx].turnaround = processes[idx].completion - processes[idx].arrival;
processes[idx].waiting = processes[idx].turnaround - processes[idx].burst;
if (meh[currentProcess].remainingBurst == 0) { completedProcesses++;
meh[currentProcess].completion = k;
meh[currentProcess].turnaround = meh[currentProcess].completion - meh[currentProcess].arrival;
meh[currentProcess].waiting = meh[currentProcess].turnaround - meh[currentProcess].burst;
completed++;
}else{ }else{
// If the process is not finished, re-add it to the queue currentTime += timeQuantum;
readyQueue.push(currentProcess); processes[idx].remaining -= timeQuantum;
}
for(int i = 0; i < n; i++){
if(!inQueue[i] && processes[i].arrival <= currentTime && processes[i].remaining > 0){
q.push(i);
inQueue[i] = true;
}
}
if(processes[idx].remaining > 0){
q.push(idx);
} }
} }
} }
@ -91,14 +88,11 @@ public:
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0; double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n"; cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
for (int j = 1; j <= n; j++) { for(int i = 0; i < n; i++){
totalWaiting += meh[j].waiting; totalWaiting += processes[i].waiting;
totalTurnaround += meh[j].turnaround; totalTurnaround += processes[i].turnaround;
totalCompletion += meh[j].completion; totalCompletion += processes[i].completion;
cout << "P" << j << " | " << setw(15) << meh[j].completion cout<<"P"<< processes[i].id<<" | "<<processes[i].completion<<" | "<< processes[i].waiting<<" | "<<processes[i].turnaround<<" | "<<processes[i].response<<"\n";
<< " | " << setw(12) << meh[j].waiting
<< " | " << setw(15) << meh[j].turnaround
<< " | " << setw(12) << meh[j].response << "\n";
} }
cout<<"\nAverage completion time: "<<totalCompletion/n; cout<<"\nAverage completion time: "<<totalCompletion/n;
@ -108,9 +102,9 @@ public:
}; };
int main(){ int main(){
RoundRobin obj; RoundRobin rr;
obj.inputProcesses(); rr.input();
obj.roundRobinProcess(); rr.process();
obj.displayMetrics(); rr.displayMetrics();
return 0; return 0;
} }