Title: QUEUES WAITING LINE
1QUEUES - WAITING LINE
- A Queue is an ordered collection of data items.
Items are added to one end of the queue called
the back, and removed from the other end called
the front. - Example Check-out lane in a grocery.
- Queue is First-In-First-Out (FIFO) whereas the
Stack is Last-In-First-Out (LIFO)
2 Examples of queue
a. I/O buffers queues, scrolls, deques ?? From
a file (queue)
3?? Interactively (scroll queue on one end,
stack on the other
4?? Screen handling (deque double-ended
queue)
5- b. Scheduling queues in a multi-user computer
system - ?? Printer queue When files are submitted to a
printer, they are placed in the printer queue.
The printer software executes an algorithm
something like - for ()
-
- while (printerQueue.empty())
- sleep 1
- printFile printerQueue.removeQ()
- Print(printFile)
-
6?? Other Queues Resident queue On disk,
waiting for memory Ready queue In memory has
everything it needs to run, except the
CPU Suspended queue Waiting for I/O transfer or
to be reassigned the CPU
7c. CPU Scheduling Probably uses a priority
queue Items with lower priority are behind
all those with higher priority. (Usually a new
item is inserted behind those with the same
priority.)
8Queue ADT
- Data ordered collection of data items
- Basic Operations
- empty()
- enqueue(DataElt) adds DataElt to back of queue
- front() returns item at front of queue
- dequeue() removes item from front of queue
- Display() output the values stored in queue
9Implementation of Queue ADT
- Simplest implementation is to put items into an
array with 0 being the front. This implementation
is not used because it is too inefficient. Every
time an item leaves the queue, every item has to
be moved just like a real waiting line. - A circular array is used instead with a moving
front and back.
10Circular Array Implementation
- Keep a gap (empty element) between front and back
elements let the index of the empty element be
the back. - As you add elements, advance myBack as you
remove elements, advance myFront. (Use
operator to keep indexes within the range 0
through the capacity-1.)
11QUEUE ClassConstants and Data Fields
- Global constants
- typedef type QueueElement
- const int QUEUE_CAPACITY ...
- Private data fields
- QueueElement myArrayQUEUE_CAPACITY
- int myFront, myBack
12QUEUE ClassFunction Members
- //default constructor
- Queue()
- myFront 0 myBack 0
- // empty?
- bool empty() const
- // to add a new element
- void enqueue (const QueueElement value)
13functions continued
- // to retrieve front element
- QueueElement front () const
- // to remove front element
- void dequeue()
14Example of Software Development(A Queue
Application)
- Recall from Chapter 1 --
- Phases of Software Development
- Requirements (Problem analysis and specification)
- Design
- Coding
- Testing
15Check-out Lane SimulationRequirements
- Functional Requirements The program will
- Simulate servicing customers in checkout lanes by
- randomly generating customer arrivals and
expected service time for each - opening and closing lanes as needed
- Summarize results of the simulation
- total customers serviced
- maximum minutes spent in line
- average minutes spent in line
- Design Constraint
- Each checkout lane will be represented as a queue.
16Requirements Analysis
- Many details about the requirements need to be
filled in. - Clarification of terminology
- time
- elapsed time in simulation
- service time for a customer
17Requirements Analysis cont.
- Steps in simulation
- Count off minutes of simulation and for each
minute - randomly determine if a customer arrives
- if so
- if all lanes are full, display error
- else
- randomly generate service time for this customer
- place customer in first lane which is not full,
opening a new lane if necessary
18Requirements Analysis cont.
- Steps in simulation cont.
- Provide 1 minute of service to customer at the
front of each lane. - Determine for each lane whether the customer has
finished checking out, and if so - Remove the customer from the lane.
- If lane is now empty, close it.
- When simulation time is up, finish servicing
remaining customers in all lanes.
19Requirements Analysis cont.
- Outputs
- for each minute
- current time in simulation
- lanes full error message if needed
- message when a lane opens/closes
- status and contents of each lane at end of minute
- at end of simulation, summary report
- total customers serviced
- maximum minutes spent in line by a customer
- average minutes spent in line by customers
20Requirements Analysis cont.
- Simulation parameters supplied by client
- Max num lanes
- Customers per lane
- Minutes for simulation
- Minimum service minutes
- Maximum service minutes
- Number of customers expected per hour
21Design
- What are the objects and how to represent them.
- Customer Struct
- CustId, serviceMinutes, arrivalTime
- Output function
- Lane Queue
- Collection of lanes Array of lanes
- Simulation parameters
- Statistics
- Total Customers
- Total time in line
- Max time in line
22Design cont.
- Types and classes
- RandomInt
- Queue modified as follows
- SetFront
- Count
- Top-down design of functions (begin with main and
work down)
23main function
- GetUserParameters
- Loop to count off minutes
- Process customer arrival
- Service customers at front of lanes
- Display lanes
- Service remaining customers
- Report statistics
24Input User Parameters
25Process Customer Arrivals
26Service Customer at Front of Each Lane
27Display Lanes
28Service Remaining Customers
29Display Report of Statistics