Professional Documents
Culture Documents
l(T) m(T)
L(T)
W(T)
• A(T) : number of customers arrived
• D(T) : number of departures
• l(T) : average arrival rate
• m (T) : average departure rate
• L(T) : average number of customers
• Wk: witing time of k-th customer in the system
• 1
AT
W T
A T k 1
Wk average witing time
Stability of the queueing system
l(T) m(T)
Queueing
system
l
Poisson
arrivals
Exponentially
distributed service time
Little's law
L = l×W
where
• L : average number of customers in the system
• W : average response time
• TH : average throughput rate
Queueing system
l m
L
W
Stability condition of M/M/1 queue
Poisson arrival, exponential service, 1 server
state 0 : 1m 0 l
state 0-1: 2 m 1l
Balance equations
state 0-1-...-n: n 1 m n l
Normalization equations: n 1
n=0
With r l/m,
p0 1 - p
p n = rn p 0
Performance measures of M/M/1 queue
Exponentially distributed
service tim
l
Poisson
N(t) : number of customers
arrivals
in the system
m m m m
0 1 2 3
l l l l
w = Waiting probability
= C + C+1 + ...
= C/(1-r/C)
M/M/C with impatient customers
Steady state distribution :
r l/m
Pn = rn/n! P0, " 0 < n C
-1
C rn
P0
n 0 n!
p m2
m1
1-p m3
p = 0.5, m1 = 4, m2 = 1, m3 = 2
N= 2, 3, 4
Printing task queue
job1 job2 job3 job4 job5 job6
^ ^
| |
| |_________
| |
Head:
Tail:
All items are
All new items
deleted from
are added on
this end
this end
Operations on Queues
• Insert(item): (also called enqueue)
– It adds a new item to the tail of the queue
• Remove( ): (also called delete or dequeue)
– It deletes the head item of the queue, and returns to the caller. If the queue
is already empty, this operation returns NULL
• getHead( ):
– Returns the value in the head element of the queue
• getTail( ):
– Returns the value in the tail element of the queue
• isEmpty( )
– Returns true if the queue has no items
• size( )
– Returns the number of items in the queue
Different types of queue
1. Circular queue
2. Double Ended Queue
3. Priority queue
Applications of Queues
• Direct applications
– Waiting lines
– Access to shared resources (e.g., printer)
– Multiprogramming
• Indirect applications
– Auxiliary data structure for algorithms
– Component of other data structures
Priority Queues
Some data structures you have (probably) seen so far:
- Linked lists
- Hash tables
- Trees
- Graphs
- Stacks
- Queues
In a priority queue items must leave from the front, but they
enter on the basis of a priority (key) value. The queue is kept
sorted by this value.
Priority Queue Operation Design
void Queue::DisplayQueue() {
cout << "front -->";
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << values[(front + i) % maxSize];
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
}
}
Using Queue
int main(void) {
Queue queue(5);
cout << "Enqueue 5 items." << endl;
for (int x = 0; x < 5; x++)
queue.Enqueue(x);
cout << "Now attempting to enqueue again..." << endl;
queue.Enqueue(5);
queue.DisplayQueue();
double value;
queue.Dequeue(value);
cout << "Retrieved element = " << value << endl;
queue.DisplayQueue();
queue.Enqueue(7);
queue.DisplayQueue();
return 0;
}
Enqueue
void Queue::Enqueue(double x) {
Node* newNode = new Node;
newNode->data = x;
newNode->next = NULL;
if (IsEmpty()) {
front = newNode;
rear = newNode;
} rear
else {
rear->next = newNode; 8 5
rear = newNode;
} rear
counter++;
8 5
}
newNode
Dequeue
bool Queue::Dequeue(double & x) {
if (IsEmpty()) {
cout << "Error: the queue is empty." << endl;
return false;
}
else {
x = front->data;
Node* nextNode = front->next;
delete front;
front = nextNode;
counter--;
}
} front
3 8 5
front
8 5
Printing all the elements
void Queue::DisplayQueue() {
cout << "front -->";
Node* currNode = front;
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << currNode->data;
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
currNode = currNode->next;
}
}
Queues
• Collection with access only to the item that has
been present the longest
• Last in last out or first in first out
• enqueue, dequeue, front, rear
• priority queues and deques
Front Rear
Deletion
Insertion
Enqueue (3) 1 3 2 4
Rear Front
Dequeue, which 1 3 2 4
returns 2 Rear Front
Dequeue, which 1 3 2 4
returns 4 Front Rear
Dequeue, which
returns 1
1 3 2 4
Rear
Front
Dequeue, which
returns 3 and makes 1 3 2 4
Rear Front
the Queue empty
Implementation of Queues
Front Header
……
Rear /
Linked List Implementation of Queues
Front
Empty Queue Reart /
Enqueue x Front
Reart x /
Enqueue y Front
Reart x y /
Dequeue x Front
Reart x y /
A node in a doubly linked list: A compound object that
stores a reference to an element and two references, called
next and prev, to the next and previous nodes, respectively.
Element
Node
Reference to an
element
Reference to next
next node
prev Reference to
previous node
For convenience, a doubly linked list has a header node and a
trailer node. They are also called sentinel nodes, indicating
both the ends of a list.
header trailer
Front Rear
Operation Output D
insertFirst(3) - (3)
insertFirst(5) - (5,3)
removeFirst() 5 (3)
insertLast(7) - (3,7)
removeFirst() 3 (7)
removeLast() 7 ()
removeFirst() “error” ()
isEmpty() true ()
Class MyDeque
public class MyDeque implements Deque {
DLNode header, trailer;
int size;
public MyDeque() {
header = new DLNode();
trailer = new DLNode();
header.setNext( trailer );
trailer.setPrev( header );
size = 0; }
return }
public void insertFirst( Object o ) {
DLNode second = header.getNext();
DLNode first = new DLNode( o, header, second );
second.setPrev( first );
header.setNext( first );
size++;
}
Data Data
In Out
scheduling
• Let set of tasks to be processed by 1 server, find
the average completion time, average service
time, average waiting time, using batch schedule.
• Task 1 2 3 4 5 6 7 8 9 10 11 12 13
• Time 25 22 12 20 2 50 30 10 2 20 40 10 5
solution
• Using FCFS (first come first serviced)
• Task 1 2 3 4 5 6 7 8 9 10 11 12 13
Service
• time 25 22 12 20 2 50 30 10 2 20 40 10 5
Wait
• time 0 25 47 59 79 81 131 161 171 173 193 233 243
• Av= 122.8
Complet.
Time 25 47 59 79 81 131 161 171 173 193 233 243 248
Av.= 141.8
av. Completion time=av. Wait time + av. Service time
Let set of tasks to be processed by 1 server, find the
average completion time, average service time, average
waiting time, using time sharing with slot time =10 time
units
av. Completion time= av. Wait time + av. Service time, as in case of batch
Av. Completion time < case of batch – for 1 processor
av. Wait time < case of batch – for 1 processor
Using 2 serial processors, find the av.
completion time, av. service time, av.
waiting time
Gantt time diagram:
av. Completion time for 3 servers< av. Completion time for 2 servers < av. Completion time for 1 servers
av. Wait time for 3 servers< av. Wait time for 2 servers < av. Wait time for 1 servers
exercise
• Write instructions to Delete and insert in
queue
• For an empty queue , what is its content, its
length:
Insert 4, del , insert 5, insert 7, insert 12, del,
del,