- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate server loads using Round Robin Scheduling
Round robins scheduling is used in CPU scheduling, we are given some M number of servers and N number of requests. Each request has some arrival time and processing time. We have to find the load on each server using the Round-Robin Scheduling for which we are going to implement a program in C++ programming language using the priority queue and sets.
Example
Let's understand the problem with the help of an input-output example −
Input
int arrival_time[] = { 1, 2, 4, 6 }; int process_time[] = { 6, 1, 2, 2 }; int servers = 2;
Output
1 Server has load of: 1 2 Server has load of: 3
Explanation
First load will get the first request and it will end at 7th unit until then the second server will get all the request that come as 2nd, 4th, and 6th second.
Approach
In this approach, we are going to define some helper functions which will make some work easy.
First, in the main function, we are going to take the inputs in the arrays and will call the function by passing the inputs to it.
In the called function, we are going to define a priority queue which will define the time when the current server which is busy will be freed.
We will define a set that will store the server's index which is free and an array that will store the load on the servers.
Initially, all the servers are free, so we will add them to the available set.
We will traverse over the request using the for loop and at each iteration we are going to remove all the severe which are free now using the while loop.
For the current request, the end time will be the arrival time plus the processing time so we will check if no available server is present then we will ignore the request.
Otherwise, we will look for the server i % totalServer if it is not present then we will get the first server which is free.
We will mark the chosen server as not available and simply add it to the priority queue with the time equal to the process end time.
Also, we will increase the load of the current server, which we will print by calling the print function.
Let us implement the code −
Example
#include <bits/stdc++.h> using namespace std; // function to get and print of each given server void printLoad(int size, int load[]){ // traversing over the array to get the loads for (int i = 0; i < size; i++) { cout << i + 1 << " Server has load of: " << load[i] << endl; } } // function to get the load which is on each server void getLoad(int reqSize, int arrival_time[], int process_time[], int serversSize){ // array to store the load on each server int load[serversSize]; // initialse it with 0 memset(load, 0, sizeof(load)); // creating priority queue to get the busy servers using the minimum version because we need the one which release early priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >busy_servers; // defining set to get the data of the servers which are available set<int> available_servers; // initially all the servers are free so we are going to add them to set for(int i = 0; i < serversSize; i++){ available_servers.insert(i); } // going through all the requests using the for loop for(int i = 0; i < reqSize; i++){ // end time will be the sum of both arriaval time and process time int end_time = arrival_time[i] + process_time[i]; // remove all the servers that are not busy from the priority queue while (busy_servers.empty() == false and busy_servers.top().first <= arrival_time[i]){ // release the server pair<int, int> cur = busy_servers.top(); busy_servers.pop(); // insert released server to available server available_servers.insert(cur.second); } // if all the severs are busy, current request is ignored if(available_servers.empty() == true){ continue; } int demanded_server = i % serversSize; // search the demanded server auto itrator = available_servers.lower_bound(demanded_server); if(itrator == available_servers.end()){ // if demanded server is busy then choose the first free server itrator = available_servers.begin(); } int assigned_server = *itrator; // increase the load on the assigned server load[assigned_server]++; // remove the assigned server from available servers adding it to the busy servers available_servers.erase(assigned_server); busy_servers.push({ end_time, assigned_server}); } // calling the function to print the load on the server printLoad(serversSize, load); } int main(){ // given inputs int arrival_time[] = { 1, 2, 4, 6 }; int process_time[] = { 6, 1, 2, 2 }; int n = 4; int servers = 2; // calling to the function to get the required data; getLoad(n, arrival_time, process_time, servers); return 0; }
Output
1 Server has load of: 1 2 Server has load of: 3
Time and Space Complexity
The time complexity of the above code is O(N*log(M)) where N is the number of requests and M is the number of servers. We are getting the logarithmic factor due to the priority queue.
The space complexity of the above code is O(M), as we are using some extra space to store the load on the servers and in the form of the priority queue.
Conclusion
In this tutorial, we have implemented a program to find the server load using the round-robin searching algorithm. We have implemented a program that works in the O(N*log(M)) time complexity as we are using the priority queue as well as the hash set to get severe when they are free and to get the free severe respectively which increases the space complexity to O(M).
To Continue Learning Please Login
Login with Google