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

plz include prototype of sleeep

For DevCpp (Windows Users):

Add #include
and make 'sleep' to 'Sleep'

to overcome the error in Dev-C++
by using
#include and then replace "sleep" to "Sleep"

thanks for trying this one in dev c++ and sharing the modification.

for error just add these (2008):
#include "stdafx.h"
#include
void sleep(unsigned int msecond){
clock_t goal = msecond + clock();
while(goal>clock());
}

header file for delay() or sleep()??????????

this is compiled in g++ compiler

g++

your right...

hi

..this code has an error

this is tested and compiled in GCC.

Post new comment

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.