0% found this document useful (0 votes)
38 views

4 - I - Simulation of Queue

The document discusses simulating M/M/1 and M/M/1/N queueing models in MATLAB. It provides the theory behind the models and MATLAB code to simulate them. Key outputs are graphs of mean delay, number of clients, and utilization versus load for M/M/1 and a graph of mean queue length versus time for M/M/1/N.

Uploaded by

Aman Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

4 - I - Simulation of Queue

The document discusses simulating M/M/1 and M/M/1/N queueing models in MATLAB. It provides the theory behind the models and MATLAB code to simulate them. Key outputs are graphs of mean delay, number of clients, and utilization versus load for M/M/1 and a graph of mean queue length versus time for M/M/1/N.

Uploaded by

Aman Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

EXPERIMENT No.

-4 (A)

AIM:
Write a program for Simulation of M/M/1 and M/M/1/N queues Network Protocols.

SOFTWARE REQUIRED:
MATLAB

THEORY:
In queueing theory, a discipline within the mathematical theory of probability, an M/M/1 queue
represents the queue length in a system having a single server, where arrivals are determined by
a Poisson process and job service times have an exponential distribution. The model name is
written in Kendall's notation.
An M/M/1 queue is a stochastic process whose state space is the set {0,1,2,3,...} where the
value corresponds to the number of customers in the system, including any currently in service.
 Arrivals occur at rate λ according to a Poisson process and move the process from state i to
i+1.
 Service times have an exponential distribution with rate parameter μ in the M/M/1 queue,
where 1/μ is the mean service time.
 A single server serves customers one at a time from the front of the queue, according to a
first-come, first-served discipline. When the service is complete the customer leaves the
queue and the number of customers in the system reduces by one.
 The buffer is of infinite size, so there is no limit on the number of customers it can contain.

 In fig 4.1 l represent the birth (Creation) of new process and m represent the death
(Completion) of old processes.

Fig. 1: Representation of M/M/1 queues

MATLAB Code for simulation of M/M/1 Queue


function e=exponential(mean)
e=exprnd(mean);
%implementation of a simple M/M/1
queue_lim=200000; % system limit
arrival_mean_time(1:65)=0.01;
service_mean_time=0.01;
sim_packets=750; %number of clients to be simulated
util(1:65) = 0;
avg_num_in_queue(1:65) = 0;
avg_delay(1:65) = 0;
P(1:65) = 1;
for j=1:64 %loop for increasing the mean arrrival time
arrival_mean_time(j+1)=arrival_mean_time(j) + 0.001;
num_events=2;
% initialization
sim_time = 0.0;
server_status=0;
queue_size=0;
time_last_event=0.0;
num_pack_insys=0;
total_delays=0.0;
time_in_queue=0.0;
time_in_server=0.0;
delay = 0.0;
time_next_event(1) = sim_time + exponential(arrival_mean_time(j+1));
time_next_event(2) = exp(30);
disp(['Launching Simulation...',num2str(j)]);
while(num_pack_insys<sim_packets)
min_time_next_event = exp(29);
type_of_event=0;
fori=1:num_events
if(time_next_event(i)<min_time_next_event)
min_time_next_event = time_next_event(i);
type_of_event = i;
end;
end
if(type_of_event == 0)
disp(['no event in time ',num2str(sim_time)]);
end
sim_time = min_time_next_event;
time_since_last_event = sim_time - time_last_event;
time_last_event = sim_time;
time_in_queue = time_in_queue + queue_size * time_since_last_event ;
time_in_server = time_in_server + server_status * time_since_last_event;
if (type_of_event==1)
%disp(['packet arrived']);
% -------------------------larrival-------------------------
time_next_event(1) = sim_time + exponential(arrival_mean_time(j+1));
% epomenosxronosafiksis
if(server_status == 1)
num_pack_insys = num_pack_insys + 1;
queue_size = queue_size + 1 ;
if(queue_size>queue_lim)
disp(['queue size = ', num2str(queue_size)]);
disp(['System Crash at ',num2str(sim_time)]);
pause
end
arr_time(queue_size) = sim_time;
else
server_status = 1;
time_next_event(2) = sim_time + exponential(service_mean_time);
end
elseif (type_of_event==2)
% ---------------service and departure---------------
if(queue_size == 0)
server_status = 0;
time_next_event(2) = exp(30);
else
queue_size = queue_size - 1;

delay = sim_time - arr_time(1);


total_delays = total_delays + delay;
time_next_event(2) = sim_time + exponential(service_mean_time);
fori = 1:queue_size
arr_time(i)=arr_time(i+1);
end
end
end
end

%results output
util(j+1) = time_in_server/sim_time;
avg_num_in_queue(j+1) = time_in_queue/sim_time;
avg_delay(j+1) = total_delays/num_pack_insys;
P(j+1) = service_mean_time./arrival_mean_time(j+1);
end

%----------------------graphs--------------------------------
figure('name','mean number of clients in system diagram(simulated)');
plot(P,avg_num_in_queue,'r');
xlabel('P');
ylabel('mean number of clients');
axis([0 0.92 0 15]);

figure('name','mean delay in system diagram (simulated)');


plot(P,avg_delay,'m');
xlabel('P');
ylabel('mean delay (hrs)');
axis([0 0.92 0 0.15]);

figure('name', 'UTILISATION DIAGRAM');


plot(P,util,'b');
xlabel('P');
ylabel('Utilisation');
axis([0 0.92 0 1]);

Method 2:
M/M/1 Queuing System: This example shows how to model a single-queue single-server
system with a single trafficsource and an infinite storage capacity. In the notation, the M stands
for Markovian; M/M/1means that the system has a Poisson arrival process, an exponential
service time distribution,and one server. Queuing theory provides exact theoretical results for
some performancemeasures of an M/M/1 queuing system and this model makes it easy to
compare empiricalresults with the corresponding theoretical results.
Structure
The model includes the components listed below:
Entity Generator block: Models a Poisson arrival process by generating entities(also known as
in queuing theory).
Simulink Function exponentialArrivalTime(): Returns data representing theinterarrival times
for the generated entities. The interarrival time of a Poisson arrivalprocess is an exponential
random variable.
Entity Queue block: Stores entities that have yet to be served in FIFO order
Entity Server block: Models a server whose service time has an exponentialdistribution.
Fig. 2: Simulink representation of M/M/1 Queuing model

OUTPUT: (From Matlab Code)

Fig. 3: Mean number of clients versus ratio of service time and arrival time
Fig. 4: Mean delay versus ratio of service time and arrival time

Fig. 5: Utilisation versus ratio of service time and arrival time

Fig. 6: Server Utilization number of process


Fig. 7: Waiting time(theoretical) v/s number of process

Fig. 8: Waiting time (Simulation) v/s number of process

M/M/1/N Queue:

In this queuing model, the arrivals occur one by one in accordance to Poisson process with
parameter (1+𝜂), where ‘𝜂’ represents the percentage change in number of customers calculated
from past or observed data. For instance, if in past an organization offered discounts and the
percentage change in number of customers was observed + 50% or 1 20% then 𝜂 =0.5 or 𝜂
=1.2respectively.

In M/M/1/N model there are following assumptions:


(i)There is a single server through which the service is provided.
(ii)The capacity of the system is finite say, N.
(iii)Customers are serviced in order of their arrival i.e. the queue discipline is first come first
served.
(iv) Service times are exponentially distributed with parameter𝜇.
Program

function [meanlen] = MM1K(lambda, mu,k,nv)


%init
clc
clear all
close all
lambda = 3;
mu = 5;
k = 3;
nv = 500;%number of variable

%arrive time stamp


at = zeros(1,nv);
%crossing time stamp
ct = zeros(1,nv);
%leave time stamp
lt = zeros(1,nv);

%%
gapt = exprnd(lambda,1,nv);
st = exprnd(mu,1,nv);
st = [st; exprnd(mu,1,nv)];
st = [st; exprnd(mu,1,nv)];
%%
%gapt = zeros(1,nv) + 10;
%st = zeros(3,nv) + 1;

%%
%init the index of three service

at(1) = 0;
fori =2 :nv
at(i) = at(i-1) + gapt(i-1);
end

%the index number on the three workstation


index = zeros(1,k);
index(1) = 1;

%the pointer record the serviced number of each service station


pointer = zeros(1,3) + 1;
ct(1) = 0;
lt(1) = ct(1) + st(1,1);

%value and position of the vehicle which will leave


v = -1;
pos = -1;

%%
fori = 2 : k
ct(i) = at(i);
lt(i) = ct(i) + st(i,pointer(i));
pointer(i) = pointer(i) + 1;
index(i) = i;
end

%%
fori = (k+1):nv

[v, pos] = min([lt(index(1)), lt(index(2)), lt(index(3))]);

if at(i) >= v
ct(i) = at(i);
else
prev = index(pos);
ct(i) = lt(prev);
end

lt(i) = ct(i) + st(pos,pointer(pos));


pointer(pos) = pointer(pos)+1;
index(pos) = i;
end

%%
%legnth of the queue
lenat = zeros(nv,2);
lenlt = zeros(nv,2);

fori = 1:nv
lenat(i,1) = at(1,i);
lenat(i,2) = 1;

lenlt(i,1) = lt(1,i);
lenlt(i,2) = -1;

end
%%
len = [lenat; lenlt];
len = sortrows(len,1);

%%
leng = zeros(1,size(len,1));
fori = 2:size(len,1)
iflen(i,2) > 0
leng(1,i) = leng(1,i-1) + 1;
else
ifleng(1,i-1) > 0
leng(1,i) = leng(1,i-1)-1 ;
else
leng(1,i) = 0;
end
end
end

figure
hold on
subplot(2,1,1)
x = 1:size(len,1);

plot(x, leng);
title('queue length vs time');
xlabel('Time(ms)');
ylabel('Queue Length');

meanlen(x) = mean(leng);
subplot(2,1,2)
plot(x, meanlen)
title('Mean queue length');
xlabel('Time(ms)');
ylabel('Mean Queue Length');
end
Fig. 9: Queue Length v/s time graph for M/M/1/N queuing model

CONCLUSION:
M/M/1 queuing theory is simulated in MATLAB environment. This experiment is done using
two methods (i) Matlab code (ii) Simulink representation. The outcome of this experiment is to
calculate the mean delay, mean number of clients and utilization of processor, when the
processes is continuously arriving and execution. This calculation is useful to the finding the
number of severs required to execute the processes without delay.
M/M/1/N queuing model is simulated in Matlab environment. In this model one server is used
for execution of process. The mean Queue length is higher than the other Queuing model.

DISCUSSION:
Q1. How is queuing delay calculated?
Q2. What is utilization factor in Queuing theory?
Q3. What is queuing model in simulation?
Q4. What is steady state in Queuing theory?

You might also like