matlab轮询调度算法代码,轮询调度算法(Round-RobinScheduling)

matlab轮询调度算法代码,轮询调度算法(Round-
RobinScheduling)
在多台机器实现负载均衡的时候,经常⽤到轮询调度算法(Round-Robin Scheduling)。
轮询调度算法就是以循环的⽅式依次将请求调度不同的服务器,即每次调度执⾏i = (i + 1) mod n,并选出第i台服务器。
算法的优点是其简洁性,它⽆需记录当前所有连接的状态,所以它是⼀种⽆状态调度。
1、算法流程:
假设有⼀组服务器 S = {S0, S1, …, Sn-1} ,有相应的权重,变量i表⽰上次选择的服务器,权值cw初始化为0,i初始化为-1 ,当第⼀次的时候取权值取最⼤的那个服务器,通过权重的不断递减,寻适合的服务器返回,直到轮询结束,权值返回为0
2、代码实现:
/*
* 权重轮询调度算法(Weighted Round-Robin Scheduling)
* 在多台机器实现负载均衡的时候,存在调度分配的问题.
* 如果服务器的配置的处理能⼒都⼀致的话,平均轮询分配可以直接解决问题,然⽽有些时候机器的处理能⼒是不⼀致的.
* 假如有2台机器 A和B , A的处理能⼒是B的2倍,则A的权重为2,B的权重为1.权值⾼的服务器先收到的连接,
* 权值⾼的服务器⽐权值低的服务器处理更多的连接,相同权值的服务器处理相同数⽬的连接数。
* */
#include
#include
#include
#include
#include
using namespace std;
#define SERVER_COUNT 10
#define RAND_WEIGHT 10
#define BUFFER_SIZE 1024
struct srv_info
{
srv_info()
{
ip = new char[BUFFER_SIZE];
weight = 0;
}
char* ip;
int weight;
};
static vector server;//服务器信息
static int i = -1;//上⼀次选择的服务器
static int cw = 0;//当前调度的权值
int get_gcd();//获得所有服务器权值的最⼤公约数
int get_max_weight();//获得所有服务器中的最⼤权值
int get_server(srv_info* s);//轮询调度
int main(int argc, char **argv)
制作无纺布手提袋{
srand(time(NULL));
char tmp[BUFFER_SIZE];
server.clear();
for (int i = 0; i < SERVER_COUNT; i++)
{
const char* s = "192.168.0.10";
memset(tmp, '\0', BUFFER_SIZE);
sprintf(tmp, "%s%d", s, i);
struct srv_info sinfo;
memcpy(sinfo.ip, tmp, BUFFER_SIZE);
sinfo.weight = rand() % RAND_WEIGHT;
server.push_back(sinfo);
}
printf("server count: %ld\n", server.size());
for (size_t i = 0; i < server.size(); i++)
{
printf("%sweight: %d\n", server[i].ip, server[i].weight);
}
printf("====================================\n"); int used_count[SERVER_COUNT] = {0};
srv_info s;
for (int i = 0; i < 100; i++)
光纤收发器机架{
石墨电极加工int ret = get_server(&s);
if (ret == -1)
continue;
printf("%sweight: %d\n", s.ip, s.weight);
int count = used_count[ret];
used_count[ret] = ++count;
}
printf("====================================\n");
for (int i = 0; i < SERVER_COUNT; i++)
管式反应器{
printf("%s weight:%d called %d times\n", server[i].ip, server[i].weight, used_count[i]); }
return 0;
}
int get_gcd()
{
return 1;
}
int get_max_weight()
{
int max = 0;
for (size_t i = 0; i < server.size(); i++)
{
if (server[i].weight > max)
max = server[i].weight;
}
return max;
}
/**
* 算法流程:
* 假设有⼀组服务器 S = {S0, S1, …, Sn-1} ,有相应的权重,变量i表⽰上次选择的服务器,* 权值cw初始化为0,i初始化为-1 ,当第⼀次的时候取权值取最⼤的那个服务器,
* 通过权重的不断递减,寻适合的服务器返回,直到轮询结束,权值返回为0
*/
int get_server(srv_info* s)
{
static int n = server.size();//服务器个数static int gcd = get_gcd();
static int max = get_max_weight();
while (true)
{
i = (i + 1) % n;
if (i == 0)
{
cw = cw - gcd;
if (cw <= 0)
热压设备
{
cw = max;
if (cw == 0)
压铸机料筒的设计return -1;
}
}
if (server[i].weight >= cw)
{
s->weight = server[i].weight;
memcpy(s->ip, server[i].ip, BUFFER_SIZE); return i;
}
}
return -1;
}
3、结果:
10个服务器为例:
进⾏100次的轮询,其结果为:

本文发布于:2024-09-20 15:18:32,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/97858.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:服务器   轮询   调度   权值   算法   时候   权重
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议