Reply to comment

Round Robin Scheduling Algorithm - implementation in C++

Round-robin is a scheduling algorithm that assigns time slots(slices) to each process in equal portions and in circular order, handling all processes without priority. Below is my implementation in C++ language.

#include<iostream>
#include<unistd.h>

using namespace std;

int main(){
    int numberOfProcesses;
    int i;
    int timeSlot;
    int hover = 0;

    //number of processes
    cout << "\nNUMBER OF PROCESSES: ";
    cin >> numberOfProcesses;
    int burstTime[numberOfProcesses];

    //time slot
    cout << "TIME SLOT:";
    cin >> timeSlot;

    //burst time
    for(i=0; i<numberOfProcesses; i++){
        cout << "******************************\n";
        cout << "PROCESS " << i+1 << "\n";
        cout << "\tBURST TIME: ";
        cin>>burstTime[i];
    }

    //display burst time
    cout << "\n================================\n";
    cout << "     PROCESS\t    BURST TIME\t   ";
    for(i=0; i<numberOfProcesses; i++){
        cout << "\n    P"<<i+1<<"\t\t"<<burstTime[i];
        hover += burstTime[i];
    }
    cout << "\n================================\n\n";

    //start process execution
    int sec = timeSlot;
    while(hover != 0){
        for(i = 0; i < numberOfProcesses; i++){
            if(burstTime[i] > 0){
                if(burstTime[i] > timeSlot){
                    sleep(timeSlot);
                    burstTime[i] -= timeSlot;
                    hover -= timeSlot;
                    cout << "at " << sec <<"s\tPROCESS P";
                    cout << i+1 << " is running ......\n";
                    sec += timeSlot;
                }else{
                    sleep(burstTime[i]);
                    hover -= burstTime[i];
                    cout << "at " << sec <<"s\tPROCESS P";
                    cout << i+1 <<" has completed\n";
                    sec += burstTime[i];
                    burstTime[i] = 0;
                }
                
            }
        }
    }
    return 0;
} 

Reply

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.