Program for Round Robin CPU Scheduling

0

 



#include <iostream>
#include <queue>

using namespace std;

const int TIME_QUANTUM = 2; // Time quantum in seconds

struct Process {
    int processId;
    int burstTime;
    int arrivalTime;
};

void roundRobin(vector<Process> processes) {
    queue<Process> readyQueue;
    int time = 0;
    int currentProcess = 0;

    while (currentProcess < processes.size() || !readyQueue.empty()) {
        // Add new processes to the ready queue
        while (currentProcess < processes.size() && processes[currentProcess].arrivalTime <= time) {
            readyQueue.push(processes[currentProcess++]);
        }

        // Check if there is a process in the ready queue
        if (!readyQueue.empty()) {
            Process current = readyQueue.front();
            readyQueue.pop();

            // Check if the current process has completed
            if (current.burstTime > TIME_QUANTUM) {
                time += TIME_QUANTUM;
                current.burstTime -= TIME_QUANTUM;
                readyQueue.push(current);
            } else {
                time += current.burstTime;
                cout << "Process " << current.processId << " completed at time " << time << endl;
            }
        } else {
            time++;
        }
    }
}

int main() {
    vector<Process> processes = {
        {1, 6, 0},
        {2, 8, 1},
        {3, 7, 2}
    };

    roundRobin(processes);

    return 0;
}


In this example, the roundRobin function implements the Round Robin scheduling algorithm using a queue to store the ready processes. The function keeps track of the current time and the current process being executed. At each iteration of the while loop, new processes that have arrived are added to the ready queue, and the next process in the queue is executed for the specified time quantum (in this case, 2 seconds). If the current process has not completed, it is added back to the end of the ready queue. If there are no processes in the ready queue, the time is incremented by 1. This process continues until all processes have completed.


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !