1function [W, rho] = qsys_mg1_prio(lambda, mu, cs)
2% W=QSYS_MG1_PRIO(LAMBDA,MU,CS)
4% Analyzes an M/G/1 queueing system with non-preemptive (Head-of-Line) priorities.
6% This function computes per-
class mean waiting times for multiple priority
classes
7%
using the Pollaczek-Khinchine formula extended
for priorities.
9% For K priority
classes (
class 1 = highest priority), the waiting time
for class k
is:
10% W_k = (B_0 / (1 - sum_{i=1}^{k-1} rho_i)) * (1 / (1 - sum_{i=1}^{k} rho_i))
13% - rho_i = lambda_i / mu_i (utilization of class i)
14% - B_0 = sum_{i=1}^{K} lambda_i * (1/mu_i^2) * (1 + cs_i^2)
15% - cs_i = coefficient of variation
for class i service time
18% lambda : Vector of arrival rates per priority
class (
class 1 = highest)
19% mu : Vector of service rates per priority
class
20% cs : Vector of coefficients of variation per priority
class
23% W : Vector of mean waiting times per priority
class
24% rho : Overall system utilization (scalar)
28% lambda = [0.2; 0.3]; % Arrival rates
29% mu = [1.0; 1.0]; % Service rates
30% cs = [0.5; 1.0]; % Coefficients of variation
31% [W, rho] = qsys_mg1_prio(lambda, mu, cs);
34% Kleinrock, L.,
"Queueing Systems, Volume I: Theory", Wiley, 1975, Section 3.5
36% Ensure inputs are column vectors
37lambda = reshape(lambda, [], 1);
38mu = reshape(mu, [], 1);
39cs = reshape(cs, [], 1);
41% Validate input lengths
42if ~isequal(length(lambda), length(mu), length(cs))
43 error(
'qsys_mg1_prio:InvalidInput', ...
44 'lambda, mu, and cs must have the same length');
47% Validate positive values
48if any(lambda <= 0) || any(mu <= 0) || any(cs <= 0)
49 error('qsys_mg1_prio:InvalidInput', ...
50 'lambda, mu, and cs must all be positive');
53% Compute per-class utilizations
61 error('qsys_mg1_prio:UnstableSystem', ...
62 sprintf('System
is unstable: utilization rho = %g >= 1', rho));
65% Compute mean second moment of service time (used in waiting time formula)
66% B_0 = sum_i lambda_i * E[S_i^2] / 2
67% where E[S_i^2] = (1 + cs_i^2) / mu_i^2
68B_0 = sum(lambda .* (1 + cs.^2) ./ (mu.^2)) / 2;
70% Compute per-class waiting times (in queue, not including service)
75 % Cumulative sum of utilizations for higher priority
classes (1 to k-1)
79 rho_prev = sum(rho_i(1:k-1));
82 % Cumulative sum including current class (1 to k)
83 rho_curr = sum(rho_i(1:k));
85 % Waiting time (in queue) for class k
86 % W_q_k = B_0 / ((1 - rho_prev) * (1 - rho_curr))
87 W_q(k) = B_0 / ((1 - rho_prev) * (1 - rho_curr));
90% Response time = waiting time + service time
94% Compute rhohat = Q/(1+Q) to match qsys_mg1 convention
95% Q
is the mean number of customers in the system (Little's law)
96% Q = sum_k lambda_k * W_k