Effacer les filtres
Effacer les filtres

Allocation algorithm by "if-else structure"?

3 vues (au cours des 30 derniers jours)
linhtuptit
linhtuptit le 21 Fév 2017
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.

Réponse acceptée

Walter Roberson
Walter Roberson le 21 Fév 2017
Modifié(e) : Walter Roberson le 21 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by