Simulation of a Single-Server Queueing System:
Program Organization and C Implementation
Group # 4
Wasif Khan (6148)
Muhammad Umair (6103)
Muhammad Talha Athar (6101)
Hassan Ramzan (6046)
Mubeen Moosani (6089)
Why Use C for Simulation
Greater Understanding: By simulating in a general-purpose language, like
C, we’re forced to deal with the details. This gives us a better understanding of
how simulations work internally, which reduces the chances of conceptual
errors if we switch to high-level simulation software later on.
Handling Complex Systems: Although high-level simulation tools are
powerful, sometimes parts of a complex system need to be written in a general-
purpose language to faithfully represent specific, detailed logic. In such cases,
general-purpose languages like C provide more flexibility..
Widespread Use: C is universally available. Despite the presence of excellent
simulation software, many simulations are still written entirely in general-
purpose languages due to their availability and portability..
Single-Server Queue Simulation
The next section of the book (Sec. 1.4.4) explains how to simulate a single-
server queueing model in detail. This model differs slightly from the previous
one. Instead of manually processing six customers, the simulation will now
handle 1000 customer delays in the queue to collect more data and showcase
how computers handle large datasets.
The system we're simulating is a queue with one server (commonly called an
M/M/1 queue).
Change in Stopping Rule: In this simulation, the stopping rule is when 1000
delays have been processed (instead of just six). This changes the nature of the
simulation because the output measures are now relative to this stopping rule.
This introduces the notations like d(n), q(n), and u(n) (where "n" represents
the number of processed customers).
Why Not Simulation Software?
It’s important to note that this book doesn’t aim to teach a
particular simulation software. Instead, it aims to provide a
broader understanding of simulation by focusing on general
approaches. In Chapter 3, several simulation packages are
surveyed, but the simulations we discuss here will help make
it easier to learn specialized tools later
Randomness in Interarrival and Service Times
In this simulation, the interarrival times (time between customer arrivals) and
service times (time taken to serve a customer) are modeled as independent
random variables from exponential distributions:The program generates
random numbers uniformly distributed between 0 and 1 (called U(0, 1)
distribution).
Interarrival times: Exponentially distributed with a mean of 1 minute.
Service times: Exponentially distributed with a mean of 0.5 minutes.
M/M/1 Queue
M (Markovian interarrival times): Exponentially distributed
interarrival times.
M (Markovian service times): Exponentially distributed service
times.
1 (Single server): Only one server handling the queue.
Random Variates and Exponential
Distribution
To simulate random customer arrivals and service times, we use random
variates from an exponential distribution.
The program generates random numbers uniformly distributed between 0 and 1
(called U(0, 1) distribution).
Using a mathematical transformation, the program converts these uniform
random numbers into values that follow an exponential distribution. This is
done by using the formula: X=−bln(U) where b is the mean of the distribution,
and U is a random number from the uniform distribution.
Flowchart For Arrival Event
Flowchart For Departure Event
C PROGRAM IMPLEMENTATION
Core Structure of the Simulation
Explanation:
serverStatus is initialized to IDLE, indicating no customer is being served initially.
queueLength tracks the number of customers waiting.
arrivalTime and departureTime are used to handle the next events.
Arrival Event Function
Explanation:
If the server is idle, the customer is served immediately, and the departure time is
scheduled.
If the server is busy, the customer joins the queue.
The next customer arrival time is calculated.
Departure Event Function
Explanation:
If there are customers in the queue, the next one is served.
If the queue is empty, the server is set to idle.
Helper Functions
Explanation:
generateServiceTime: Generates random service times using an exponential
distribution.
generateInterarrivalTime: Generates random interarrival times using an
exponential distribution.
Main Simulation Loop
Explanation:
The main loop processes either an arrival or departure event based on which
occurs next.
The simulation clock is updated with each event, and customers served are tracked.
THE END