Top Banner
Queues Thispicture show s12 tram s, each w ith a carrying capacity of w ellover100 people w hen 'crush loaded'.
33

Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

QueuesThis picture shows 12 trams, each with a carrying capacity of well over 100 people when 'crush loaded'.

Page 2: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Definition: A queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle. rear: Objects are inserted here front: Objects are removed from here Two fundamental operations: enqueue: Insert an object to the container. dequeue: Remove an object from the container.

… …

front rear

dequeue enqueue

Page 3: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

T h e e n q u e u e o p e r a t i o n :

F r o n t R e a rI n t h e q u e u e t h e l o n g e s t

F r o n t R e a rI n t h e q u e u e t h e l o n g e s t

F r o n t R e a rI n t h e q u e u e t h e l o n g e s t

Page 4: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

T h e d e q u e u e o p e r a t i o n :

F r o n t R e a rI n t h e q u e u e t h e l o n g e s t

F r o n t R e a rI n t h e q u e u e t h e l o n g e s t

F r o n t R e a rI n t h e q u e u e t h e l o n g e s t

Page 5: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Comparison with a stack:

1. A queue is also a container of objects 2. In a stack, objects are inserted and removed according to

the LIFO principle. 3. In a queue, objects are inserted and removed according to

the FIFO principle. 4. A stack has a top. 5. A queue has a rear and a front. 6. Both stacks and queues have two fundamental operations

for inserting and removing objects.

Page 6: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Examples: The ride "Indiana Jones and the Temple of the Forbidden Eye".

Page 7: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Queue of Cars at Peace Arch Crossing in Seattle.

Page 8: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Message queues in an operating system There are times that programs need to communicate with eachother. Unix operating system provides message queue as oneof the mechanisms to facilitate communication.

Program #1

Program #2front

rear

Send a message to the queue

Take a message fromthe queue

Page 9: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Printing in a computer network is organized as print jobs. Prnt jobs are placed in a queue before they are sent to a printer.

Printerfront

rear

Computer 3

Computer 1

Computer 2

Print jobs

Page 10: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Non-queue examples: a place for parking bicycles is not a queue.

Page 11: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

A c o l l e c t i o n o f r e c o r d s i n a f i l e

R e c o r d s

0 1 2 3 4 5 6 7

Page 12: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

A group of numbers

21 5

76

1004

Page 13: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

A n a r r a y o f i n t e g e r s

2 1 5 7 61 0 04

I s i t a q u e u e o r n o t a q u e u e ?

Page 14: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

The Queue Abstract Data Type

A queue Q is an abstract data type that supports the following two fundamental methods: enqueue(o): Insert object o at the rear of the queue

Input: Object; Output: None. dequeue( ): Remove and return from the queue the object at the

front; an error occurs if the queue is empty. Input: None; Output: Object

Page 15: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Other supporting methods:

size(): Return the number of objects in the queue. Input: None; Output: Integer

isEmpty(): Return a Boolean indicating if the queue is empty. Input: None; Output: Boolean

front(): Return, but do not remove the front object in the queue; an error occurs if the queue is empty. Input: None; Output: Object

Page 16: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

This table shows a series of queue operations and their effects.The queue is empty initially.

Operation Output front<- Q<- rear

enqueue(5) - (5) enqueue(3) - (5,3) dequeue() 5 (3) enqueue(7) - (3,7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) - (9) enqueue(7) - (9,7) size() 2 (9,7) enqueue(3) - (9,7,3) enqueue(5) - (9,7,3,5) dequeue() 9 (7,3,5)

55 3

3

Page 17: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Operation Output front Q rear

enqueue(5) - (5) enqueue(3) - (5,3) dequeue() 5 (3) enqueue(7) - (3,7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) - (9) enqueue(7) - (9,7) size() 2 (9,7) enqueue(3) - (9,7,3) enqueue(5) - (9,7,3,5) dequeue() 9 (7,3,5)

Page 18: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

A Queue Interface in Java

public interface Queue { public void enqueue( Object element );

throws QueueFullException; public Object dequeue() throws QueueEmptyException; public int size(); public boolean isEmpty(); public Object front() throws QueueEmptyException; } When we define an interface, we just indicate that a class which implements it should provide all the methods specified in it.

Page 19: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

A Simple Array-Based Implementation

To implement a queue with an array, we need: 1. An array of the size N2. An index f for the front element3. An index r for next empty slot or cell

Q:

0 1 2

r N - 1f

Page 20: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

As objects are enqueued and dequeued, the queue moves alongin the array. For example:

f rAfter enqueue:

f rAfter dequeue:

f r

Page 21: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

W h e n t h e q u e u e r e a c h e s t h e e n d o f t h e a r r a y , i t “ w r a p s a r o u n d ” a n d t h e r e a r o f t h e q u e u e s t a r t s f r o m i n d e x 0 . T h e f i g u r e b e l o w d e m o n s t r a t e s t h e s i t u a t i o n . Q : 0 1 N - 1

. . .f

2

r

H o w t o c a l c u l a t e r a n d f w h e n t h e q u e u e w r a p s a r o u n d ?

Page 22: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

When we increment f or r, we compute the result as(f + 1) mod N or (r + 1) mod N .In Java, the modulo operator is % (remainder operator). For example, if r = N - 1, then (r + 1) = N , therefore,(r + 1) mod N = 0  The value of r wraps around from N to 0.

Q:

0 1 2

r f

N - 1

Page 23: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Initially, we assign r = f = 0, indicating that the queue is empty.During the process, we may meet a situation where

r = f = i (0 < i < N),which also indicates the queue is empty.Now we assume that we enqueue N objects into Q withoutdequeueing any of them:

Q:

0 1 2

rf

N - 1

Then, we have r = f = 0. But in this case, we have a full queue.In general, when r = f = i (0 < i < N), it is possible that we havea full queue.

Page 24: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Problem: r = f may indicate that the queue is empty or that thequeue is full.

Solution: when |r – f| = N – 1, report that the queue is full.

Page 25: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Algorithms: size():

return the number (N - f + r) mod N

Qfr

0 1 2 N-1 N+r-1

If r f, then r - f 0 N + (r - f ) N

(N - f + r) mod N = - f + r = r - f

If r < f, then f - r > 0 N - f + r = N – (f - r ) < N

(N - f + r) mod N = N - f + r

Page 26: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Algorithms: isEmpty( ):

return the result of the evaluation of the relationship f = r 

f

r

Page 27: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Algorithms: front( ):

if the queue is emptythrow a QueueEmptyException

else return element Q[f]

Q:

0 1 2

r N - 1f

Page 28: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

enqueue(o):if queue size is N - 1

throw a QueueFullExceptionelse

store the object to Q[r]assign (r + 1) mod N to r

Q:

0 1 2

r N - 1f

(r + 1) mod N

Page 29: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

dequeue( ):if queue size is empty

throw a QueueEmptyExceptionelse

save the element Q[f] to a variable temp make the element Q[f] a null objectassign (f + 1) mod N to f

 

Q:

0 1 2

r N - 1f

(f + 1) mod N

Page 30: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Memory Allocation in JavaR e c a l l th a t J a v a p ro g ra m s u s e s ta c k fo r fu n c t io n a rg u m e n ts a n d lo c a l v a r ia b le s . T h e J a v a v ir tu a l m a c h in e p ro v id e s a n o th e r a re a o f s to ra g e fo r J a v a p ro g ra m s . E a c h J a v a p ro g ra m h a s a m e m o ry h e a p . A ty p ic a l m e m o ry s p a c e fo r a J a v a p ro g ra m is s h o w n h e re .

B y te c o d e

G r o w sin toh ig h e rm e m o r y

H e a pF r e em e m o r y

F ix e d s iz e

S ta c k

G r o w sin tolo w e rm e m o r y

Page 31: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

In Java, when a new object is created, the Java virtual machine allocates a memory block for the object from the heap. For simplicity, assume that blocks have a fixed size. The Java virtual machine places all available memory blocks in a queue. When the Java program needs a memory block, the Java virtual machine dequeues a memory block from the queue. When the Java virtual machine determines that a memory block is no longer in use, it places the block in the queue (garbage collection).

Page 32: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

T h e m e m o r y b l o c k q u e u e :

B l o c k 1B l o c k 2 B l o c k 4B l o c k 3

F r o n t R e a r

W h e n a n e w o b j e c t i s c r e a t e d , f o r e x a m p l e , a n e w 1 2 - e l e m e n t

V e c t o r o b j e c t i s g e n e r a t e d :

V e c t o r i t e m s = n e w V e c t o r ( 1 2 ) ;

A b l o c k o f M e m o r y - b l o c k # 3 i s a l l o c a t e d f o r t h e v a r i a b l e i t e m s a s s h o w n b e l o w .

B l o c k 1B l o c k 2 B l o c k 4

B l o c k 3

F r o n t R e a r

i t e m s

Page 33: Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Data Structure Exercises 4.1