fixed round robin code
This commit is contained in:
parent
60eccf7b93
commit
ccfa65c057
@ -1,116 +1,110 @@
|
|||||||
#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;
|
||||||
|
processes[idx].remaining = 0;
|
||||||
|
processes[idx].completion = currentTime;
|
||||||
|
processes[idx].turnaround = processes[idx].completion - processes[idx].arrival;
|
||||||
|
processes[idx].waiting = processes[idx].turnaround - processes[idx].burst;
|
||||||
|
completedProcesses++;
|
||||||
|
}else{
|
||||||
|
currentTime += timeQuantum;
|
||||||
|
processes[idx].remaining -= timeQuantum;
|
||||||
|
}
|
||||||
|
|
||||||
// Process the current process
|
for(int i = 0; i < n; i++){
|
||||||
meh[currentProcess].remainingBurst -= timeSlice;
|
if(!inQueue[i] && processes[i].arrival <= currentTime && processes[i].remaining > 0){
|
||||||
k += timeSlice;
|
q.push(i);
|
||||||
|
inQueue[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (meh[currentProcess].remainingBurst == 0) {
|
if(processes[idx].remaining > 0){
|
||||||
meh[currentProcess].completion = k;
|
q.push(idx);
|
||||||
meh[currentProcess].turnaround = meh[currentProcess].completion - meh[currentProcess].arrival;
|
|
||||||
meh[currentProcess].waiting = meh[currentProcess].turnaround - meh[currentProcess].burst;
|
|
||||||
completed++;
|
|
||||||
} else {
|
|
||||||
// If the process is not finished, re-add it to the queue
|
|
||||||
readyQueue.push(currentProcess);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void displayMetrics() {
|
void displayMetrics(){
|
||||||
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;
|
||||||
cout << "\nAverage waiting time: " << totalWaiting / n;
|
cout<<"\nAverage waiting time: "<<totalWaiting/n;
|
||||||
cout << "\nAverage turnaround time: " << totalTurnaround / n;
|
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main(){
|
||||||
RoundRobin obj;
|
RoundRobin rr;
|
||||||
obj.inputProcesses();
|
rr.input();
|
||||||
obj.roundRobinProcess();
|
rr.process();
|
||||||
obj.displayMetrics();
|
rr.displayMetrics();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user