diff --git a/Codes/Group B/Assignment - 5/Round Robin (Preemptive).cpp b/Codes/Group B/Assignment - 5/Round Robin (Preemptive).cpp index 4877479..5fa9e8b 100644 --- a/Codes/Group B/Assignment - 5/Round Robin (Preemptive).cpp +++ b/Codes/Group B/Assignment - 5/Round Robin (Preemptive).cpp @@ -1,116 +1,110 @@ -#include -#include -#include -#include // For formatting output +#include +#include using namespace std; -struct process { - int burst, arrival, id, completion, priority, waiting, turnaround, response, remainingBurst; - bool active; +struct Process{ + int id, burst, arrival, remaining, completion, waiting, turnaround, response; }; +Process processes[30]; -process meh[30]; - -class RoundRobin { +class RoundRobin{ public: - int n; - int timeQuantum; + int n, timeQuantum; - void inputProcesses() { - cout << "\nEnter number of processes: "; - cin >> n; - for (int i = 1; i <= n; i++) { - cout << "\nEnter arrival time of P" << i << ": "; - cin >> meh[i].arrival; - cout << "\nEnter burst time of P" << i << ": "; - cin >> meh[i].burst; - cout << "\nEnter priority of P" << i << ": "; - cin >> meh[i].priority; // Priority is not used in RR, but kept here for completeness. - meh[i].id = i; - meh[i].remainingBurst = meh[i].burst; - meh[i].active = false; - } - cout << "\nEnter time quantum: "; - 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 input(){ + cout<<"\nEnter number of processes: "; + cin>>n; + for(int i = 0; i < n; i++){ + cout<<"\nEnter arrival time of P"<>processes[i].arrival; + cout<<"Enter burst time of P"<>processes[i].burst; + processes[i].id = i; + processes[i].remaining = processes[i].burst; + processes[i].response = -1; } + cout<<"\nEnter time quantum: "; + cin>>timeQuantum; } - void roundRobinProcess() { - int k = 0; // Current time - int completed = 0; // Number of completed processes - queue readyQueue; + void process(){ + int currentTime = 0; + queue q; + bool inQueue[30] = {false}; + int completedProcesses = 0; - vector isProcessed(n + 1, false); // Track whether a process has been added to the ready queue - - 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; - } + for(int i = 0; i < n; i++){ + if(processes[i].arrival <= currentTime){ + q.push(i); + inQueue[i] = true; } + } - if (readyQueue.empty()) { - // If no process is in the queue, increment time - k++; + while(completedProcesses < n){ + if(q.empty()){ + currentTime++; + for(int i = 0; i < n; i++){ + if(!inQueue[i] && processes[i].arrival <= currentTime){ + q.push(i); + inQueue[i] = true; + } + } continue; } - int currentProcess = readyQueue.front(); - readyQueue.pop(); + int idx = q.front(); + q.pop(); - // Calculate response time for the process if it starts now - if (!meh[currentProcess].active) { - meh[currentProcess].response = k - meh[currentProcess].arrival; - meh[currentProcess].active = true; + if(processes[idx].response == -1){ + processes[idx].response = currentTime - processes[idx].arrival; } - 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 - meh[currentProcess].remainingBurst -= timeSlice; - k += timeSlice; + 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 (meh[currentProcess].remainingBurst == 0) { - meh[currentProcess].completion = k; - 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); + if(processes[idx].remaining > 0){ + q.push(idx); } } } - void displayMetrics() { + void displayMetrics(){ double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0; - cout << "\n\n | Completion time | Waiting time | Turnaround time | Response time\n"; - for (int j = 1; j <= n; j++) { - totalWaiting += meh[j].waiting; - totalTurnaround += meh[j].turnaround; - totalCompletion += meh[j].completion; - cout << "P" << j << " | " << setw(15) << meh[j].completion - << " | " << setw(12) << meh[j].waiting - << " | " << setw(15) << meh[j].turnaround - << " | " << setw(12) << meh[j].response << "\n"; + cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n"; + for(int i = 0; i < n; i++){ + totalWaiting += processes[i].waiting; + totalTurnaround += processes[i].turnaround; + totalCompletion += processes[i].completion; + cout<<"P"<< processes[i].id<<" | "<