该处理具有无限的计算能力和数组arrivalTime []和大小的processTime [] n的表示的到达时间和以下面的方式N个请求的加载时间的多个请求给定的m个服务器:
- 每个服务器的编号从0到(M – 1) ,并且请求是按照严格增加的时间顺序给出的。
- 通过以下方式将每个请求i分配给其中一台服务器:
- 选择第(i%m)个服务器。如果所选服务器是免费的,则将请求分配给该服务器。
- 否则,请选择下一个可用的服务器。如果没有可用的服务器,则该请求将被丢弃。
考虑到每个服务器一次只能处理一个请求,因此任务是在处理所有传入请求之后查找每个服务器上的负载,因为每个服务器上的负载就是它处理的请求数。
例子:
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 | – |
方法:想法是使用最小优先级队列和一组。优先级队列可保留繁忙服务器的数量,并有助于在空闲服务器时将其释放。 Set用于维护可用服务器的数据,以将它们分配给传入的请求。步骤如下:
- 初始化辅助数组loadOnServer [] ,该数组将在每个服务器上存储负载。
- 遍历传入的请求,并通过添加每个请求的到达时间和处理时间来找到每个请求的结束时间。
- 从结束时间已经超过当前结束时间的优先级队列中弹出繁忙的服务器。
- 如果可用服务器集为空,请删除当前请求。
- 现在,使用下界函数搜索集合中的第(i%m)个服务器,如果下界迭代器指向集合的末尾,则选择集合中的第一个服务器。
- 完成上述步骤后,增加所选服务器上的负载计数器。
- 完成上述步骤后,在loadOnServer []中打印所有负载的存储。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
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,
vector >,
greater > >
busyServers;
// Set to store available Servers
set 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 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;
}
1st Server -> 1.
2st Server -> 2.
时间复杂度: O(N * log M)
辅助空间: O(M)