2
1

round robin #2

Closed
opened 2024-07-30 12:38:39 +05:30 by Kalaskar_admin03 · 1 comment
#include <iostream>
#include <queue>
#include <vector>
#include <iomanip> // For formatting output
using namespace std;

struct process {
    int burst, arrival, id, completion, waiting, turnaround, response, remainingBurst;
    bool active;
};

process meh[30];

class RoundRobin {
public:
    int n;
    int 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 roundRobinProcess() {
        int k = 0; // Current time
        int completed = 0; // Number of completed processes
        queue<int> readyQueue;

        vector<bool> 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;
                }
            }

            if (readyQueue.empty()) {
                // If no process is in the queue, increment time
                k++;
                continue;
            }

            int currentProcess = readyQueue.front();
            readyQueue.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;
            }

            int timeSlice = min(timeQuantum, meh[currentProcess].remainingBurst);

            // Process the current process
            meh[currentProcess].remainingBurst -= timeSlice;
            k += timeSlice;

            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);
            }
        }
    }

    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 << "\nAverage completion time: " << totalCompletion / n;
        cout << "\nAverage waiting time: " << totalWaiting / n;
        cout << "\nAverage turnaround time: " << totalTurnaround / n;
    }
};

int main() {
    RoundRobin obj;
    obj.inputProcesses();
    obj.roundRobinProcess();
    obj.displayMetrics();
    return 0;
}

```cpp #include <iostream> #include <queue> #include <vector> #include <iomanip> // For formatting output using namespace std; struct process { int burst, arrival, id, completion, waiting, turnaround, response, remainingBurst; bool active; }; process meh[30]; class RoundRobin { public: int n; int 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 roundRobinProcess() { int k = 0; // Current time int completed = 0; // Number of completed processes queue<int> readyQueue; vector<bool> 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; } } if (readyQueue.empty()) { // If no process is in the queue, increment time k++; continue; } int currentProcess = readyQueue.front(); readyQueue.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; } int timeSlice = min(timeQuantum, meh[currentProcess].remainingBurst); // Process the current process meh[currentProcess].remainingBurst -= timeSlice; k += timeSlice; 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); } } } 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 << "\nAverage completion time: " << totalCompletion / n; cout << "\nAverage waiting time: " << totalWaiting / n; cout << "\nAverage turnaround time: " << totalTurnaround / n; } }; int main() { RoundRobin obj; obj.inputProcesses(); obj.roundRobinProcess(); obj.displayMetrics(); return 0; } ```
Owner

Added to main branch.

Added to [main branch](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20B/Assignment%20-%205/Round%20Robin%20%28Preemptive%29..cpp).
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sppu-te-comp-content/SystemsProgrammingAndOperatingSystem#2
No description provided.