Clear Filters
Clear Filters

Allocation algorithm by "if-else structure"?

3 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Feb 2017
Edited: Walter Roberson on 21 Feb 2017
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

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!