Calculate server loads using Round Robin Scheduling

Given M servers that handle multiple requests having infinite computational capability and arrays arrivalTime[] and processTime[] of size N denoting the arrival time and load time of N requests in the following manner:

Considering that each server can handle only one request at a time, the task is to find the load on each server after all the incoming requests are processed given that load on each server is the number of requests it processes.

Examples:

Input: N = 4, M = 3, arrivalTime[] = {1, 3, 6, 8}, processTime[] = {1, 2, 2, 1}
Output:
1st Server -> 2
2nd Server -> 1
3rd Server -> 1
Explanation:
The first and fourth requests are assigned to the first server.
The second request is assigned to the second server and the third request is assigned to the third server.
Below is the transition table:

Request Number Arrival Time Load Time End Time Available Servers Demanded Server Assigned Server
0 1 1 2 0, 1, 2 0 0
1 3 2 5 0, 1, 2 1 1
2 6 2 8 0, 1, 2 2 2
3 8 1 9 0, 1, 2 1 1

Input: N = 4, M = 2, arrivalTime = {1, 2, 4, 6}, processTime = {7, 1, 4, 4}
Output:
1st Server -> 1
2nd Server -> 2
Explanation:
The first request is assigned to the first server and second request to the second server.
The third request is assigned to the second server. The demanded server for the third request is the first server but since, it is busy at the arrival time of the request,  
So, the second server is assigned to it.
The fourth request is dropped as both servers are busy at the time of its arrival.
Below is the transition table: 



Request Number Arrival Time Load Time End Time Available Servers Demanded Server Assigned Server
0 1 7 8 0, 1 0 0
1 2 1 3 1 1 1
2 4 4 8 1 0 1
3 6 4 10 1

Approach: The idea is to use a Minimum Priority Queue and a set. Priority queue keeps count of the busy servers and helps to release them as soon as they are free. Set is used to maintain the data of available servers to assign them to the incoming requests. Below are the steps:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print load on each server
void printLoadOnEachServer(
    int m, int loadOnServer[])
{
    // Traverse the loadOnServer and
    // print each loads
    for (int i = 0; i < m; i++) {
  
        cout << i + 1 << "st Server -> "
             << loadOnServer[i] << ".\n";
    }
}
  
// Function for finding the load
// on each server
void loadBalancing(int n, int m,
                   int arrivalTime[],
                   int processTime[])
{
    // Stores the load on each Server
    int loadOnServer[m];
  
    for (int i = 0; i < m; i++) {
  
        // Initialize load on each
        // server as zero
        loadOnServer[i] = 0;
    }
  
    // Minimum priority queue for
    // storing busy servers according
    // to their release time
    priority_queue<pair<int, int>,
                   vector<pair<int, int> >,
                   greater<pair<int, int> > >
        busyServers;
  
    // Set to store available Servers
    set<int> availableServers;
  
    for (int i = 0; i < m; i++) {
  
        // Initally, all servers are free
        availableServers.insert(i);
    }
  
    // Iterating through the requests.
    for (int i = 0; i < n; i++) {
  
        // End time of current request
        // is the sum of arrival time
        // and process time
        int endTime = arrivalTime[i]
                      + processTime[i];
  
        // Releasing all the servers which
        // have become free by this time
        while (!busyServers.empty()
               && busyServers.top().first
                      <= arrivalTime[i]) {
  
            // Pop the server
            pair<int, int> releasedServer
                = busyServers.top();
            busyServers.pop();
  
            // Insert available server
            availableServers.insert(
                releasedServer.second);
        }
  
        // If there is no free server,
        // the request is dropped
        if ((int)availableServers.empty()) {
            continue;
        }
  
        int demandedServer = i % m;
  
        // Searching for demanded server
        auto itr
            = availableServers.lower_bound(
                demandedServer);
  
        if (itr == availableServers.end()) {
  
            // If demanded Server is not free
            // and no server is free after it,
            // then choose first free server
            itr = availableServers.begin();
        }
  
        int assignedServer = *itr;
  
        // Inceasing load on assigned Server
        loadOnServer[assignedServer]++;
  
        // Removing assigned server from list
        // of assigned servers
        availableServers.erase(assignedServer);
  
        // Add assigned server in the list of
        // busy servers with its release time
        busyServers.push({ endTime,
                           assignedServer });
    }
  
    // Function to print load on each server
    printLoadOnEachServer(m, loadOnServer);
}
  
// Driver Code
int main()
{
    // Given arrivalTime and processTime
    int arrivalTime[] = { 1, 2, 4, 6 };
    int processTime[] = { 7, 1, 4, 4 };
  
    int N = sizeof(arrivalTime)
            / sizeof(int);
  
    int M = 2;
  
    // Function Call
    loadBalancing(N, M, arrivalTime,
                  processTime);
  
    return 0;
}
chevron_right

Output:
1st Server -> 1.
2st Server -> 2.

Time Complexity: O(N*log M)
Auxiliary Space: O(M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :