Allocation algorithm by "if-else structure"?

3 次查看(过去 30 天)
I am building an algorithm wherein some servers is setup for serving many users. The algorithm is programmed to control the assignment of server to serve incoming users. During start-up, all servers are available. When the first user arrives, the algorithm assigns the first server to serve him, and then labels that this server as being busy. When busy, this server becomes unavailable for the constant time (t). When subsequent user arrive, the algorithm steps through the status of each server and assigns the first one that is not busy. However, in some cases, users that arrive when all server are currently busy do not get served. I have tried to use "if-else structure" but it isn't a good choice to illustrate this algorithm. Please help me other ways to implement it. Many thank for all.

采纳的回答

Walter Roberson
Walter Roberson 2017-2-21
编辑:Walter Roberson 2017-2-21
num_servo = 5;
servo_hold_time = 8;
timestep = 0;
servo_busy = zeros(1, num_servo);
while true
timestep = timestep + 1;
mask = servo_busy > 0;
servo_busy(mask) = servo_busy(mask) - 1;
if user_request()
servo_to_use = find(servo_busy == 0, 1, 'first');
if isempty(servo_to_use)
fprintf('Tough luck, request dropped\n');
else
servo_busy(servo_to_use) = servo_hold_time;
end
end
biz = sum(servo_busy > 0);
fprintf('%d servers busy\n', biz);
sleep(1);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by