200 likes | 313 Views
Day 11: Intro to Queues. 0. Admin. 1. Queues. 2. Abstract level. 3. Application level. 4. Implementation level. 0. Admin. Return Exam 1. Go over objective questions. Hand-out code for compulsory question. Comment on other questions. 1. Basic Idea of a Queue. Why do queues form?
E N D
Day 11:Intro to Queues. 0. Admin. 1. Queues. 2. Abstract level. 3. Application level. 4. Implementation level.
0. Admin. Return Exam 1. Go over objective questions. Hand-out code for compulsory question. Comment on other questions.
1. Basic Idea of a Queue. Why do queues form? • 1) People are Russian or British. • 2) SCARCITY: A scarce service / resource exists (like a scholarship, not like air). • 3) EXCLUSIVITY: The resource is non-shareable (unlike a park or a “bouncy castle”). • 4) BUSY-NESS: There are more requests for the resource than can be served at one time.
The problem: Since it is impossible to serve everyone at once, the choices are: 1) Have a big, ugly fight; 2) Turn some people away; 3) Give everyone a slice of time (time-sharing, round robin); 4) Form an orderly queue, and process each item in turn on a FCFS basis.
The problem: But… Conflict is bad, Turning people away is bad service, And time-sharing is not always practicable, e.g. printers, grocery-store, theatre, boarding a plane! And so, the British invented the queue! (And Americans have never forgiven them.)
Examples of queues. Queues are all over the place, inside and outside computer science. Discuss… Are any queues managed better than others? Discuss…
The 3 levels of data structures. Recall that any data structure can be viewed at 3 levels: 1) The Abstract level (ADT): what is the logical concept? 2) The Application level: what is it used for? 3) The implementation level: how can it be simulated / coded in a programming language?
2. The Abstract level. The abstract data type (ADT) includes: (1) the basic structure (organization); (2) the logical features; and (3) the access method.
(1) The basic structure. See diagram. Note: a queue is relational. It presupposes the existence of something else: If there is a parent, there is a child; If there is a queue, there is a server.
(2) Logical features. 1) Elements are ordered by time. The element in the rear has been in the queue the shortest time: The annoyed business man at the back of the line has waited less time than anyone else when he joins the queue! This can be represented by the idea of a queue of wait-timers. 9 8 7 6 5 4 3 2 F R
Logical features. 2) Elements are homogeneous. 3) Access is restricted to 2 points: the front and the rear: In a strict, well-ordered queue, one can only join the queue at the rear, and only leave it from the front. 4) Queues are FIFO, FCFS processors: They process things in order of arrival.
Logical features. Queues are theologically unsound! Those who push themselves ahead are rewarded. But God exalts the humble and humiliates the proud.
(3) Access mechanism. An item not in the queue, joins the queue by “enq” at the rear. An item leaves the queue to be served by “deq” from the front. Note: time being served is not counted as time in the queue.
3. The Application Level. Automated queuing systems are found in: • Customer service systems. • Traffic control. • Systems S/W, especially operating systems, managing scarce, non-shareable H/W resources, e.g.? • Simulations.
Simulations. Why do an electronic simulation and not video coverage? What kinds of things could be simulated? Why do simulations? What could one discover by varying the parameters?
Simulations. What features of the items in queue really matter? Often the only thing that matters is the amount of time items wait: can use a queue of wait-timers (integers that represent the amount of time waited). Enq 0, then update all items in the queue each time the clock “ticks.”
Simulations. When we deq an item, we learn how long it waited. Can add to TotalWait, and add 1 to a counter of the items served. At the end of the simulation, AverageWait = TotalWait / ItemsServed;
4. Implementation level. • As before, we can choose either a static implementation (e.g. an array) or a dynamic one (such as a dynamic linked list). • Array based implementations include: • 1) Fixed Front; • 2) Moveable Front; • 3) Circular queue. • See diagrams.