Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS342: Queues1 Professor: Munehiro Fukuda. CSS342: Queues2 Topics Basic concepts of queue Queue implementation Queues used in –Applications –Operating.

Similar presentations


Presentation on theme: "CSS342: Queues1 Professor: Munehiro Fukuda. CSS342: Queues2 Topics Basic concepts of queue Queue implementation Queues used in –Applications –Operating."— Presentation transcript:

1 CSS342: Queues1 Professor: Munehiro Fukuda

2 CSS342: Queues2 Topics Basic concepts of queue Queue implementation Queues used in –Applications –Operating Systems –Network

3 CSS342: Queues3 Concepts of Queue Queue of Bank Customers –First-In First-Out (FIFO) service –How can you estimate the average wait? –Solution: Simulate it using queues Time = 0 Time = 12 Time = 20 Time = 38 Basic Concepts

4 CSS342: Queues4 Queue Specification class Queue { public: Queue( ); Queue( const Queue& Q ); ~Queue( ); bool isEmpty( ) const; bool enqueue( const Object &newItem ); Object dequeue( ); Object getFront( ) const; } retrieve the front item but do not remove it Unlike stacks: (aQueue.enqueue(newItem)).dequeue() is Not equal to aQueue unless it is empty. dequeue( ): remove and get the front item enqueue to the back Basic Concepts

5 CSS342: Queues5 STL Class queue #include queue( ); Creates an empty queue. You can convert a list into a new stack. See your text pages 351 and 353 Example: queue aQueue; bool empty( ) const; Returns if the queue is empty. Example: while ( aQueue.empty( ) == false ) { … } size_type size( ) const; Returns the number of elements. Size_type is an integral type. You can compare it with an integer. Example: while (aQueue.size( ) > 0) { … } T &front( ); Returns a reference to the first element. Example: while( !aQueue.empty() ) { cout << aQueue.front() << endl; aQueue.pop(); } T &back( ); Returns a reference to the last element. Example: if( !aQueue.empty() ) cout << aQueue.front() << endl; void pop( ); Removes the first element from the queue. Example: see the front( ) example void push( const T& value ); Inserts an item with value at the back of the queue. Example: stack aQueue; for( int i=0; i < 10; i++ ) aQueue.push(i); Basic Concepts

6 CSS342: Queues6 STL Class queue Example #include using namespace std; int main( ) { queue aQueue; int item; for ( int j = 0; j < 5; j++ ) aQueue.push( j ); while ( !aQueue.empty( ) ) { cout << aQueue.front( ) << endl; aQueue.pop( ); } Results: 0 1 2 3 4 Basic Concepts

7 CSS342: Queues7 Queue Implementation We learned how to use the queue. Then how can we implement it? –Array-based implementation –Pointer-based implementation –Reuse of linked lists Implementation

8 CSS342: Queues8 An Array-Based Implementation (Naïve Implementation) k…..7 142 back012k theArray.size( )-1 0 front 49107….. back012 theArray.size( )-1 47 front 4 474849 template class Queue { public: Queue( ); …..; private: vector theArray; int front; int back; } Problem: rightward drift Shift array elements to the left is costly. Implementation

9 CSS342: Queues9 An Array-Based Implementation (A Circular Array Implementation) template class Queue { public: Queue( ); bool isEmpty( ) const; const Object getfront( ) const; void flush( ); Object dequeue( ); void enqueue( const Object &newItem ); Private: Vector theArray; int front; int back; int currentSize: // number of items in queue void doubleQueue( ); // make the size of theArray double } 2 4 1 7 0 1 2 3 theArray.size( ) -1 front back Invariant: front should not pass more than 1 item ahead from back. back should not pass front since it has been passed by front To guarantee that, count keeps track of #items in queue: currentSize Implementation

10 CSS342: Queues10 A Circular Array Implementation template void Queue ::flush( ) { currentSize = 0; front = 0; back = theArray.size( ) – 1; } template Queue ::Queue( ) : theArray( 1 ) { flush( ); } template bool Queue ::isEmpty( ) const { return currentSize == 0; } template void Queue ::doubleQueue( ) { theArray.resize( theArray.size( ) * 2 + 1 ); // in case theArray.size( ) was 0 if ( front != 0 ) { for ( i = 0; i < front; i++ ) theArray[i + currentSize ] = theArray[ i ]; back += currentSize; } 0 1 2 3 theArray.size( ) -1 frontback Implementation

11 CSS342: Queues11 A Circular Array Implementation template const Object Queue ::getFront( ) { if ( isEmpty( ) ) throw “empty queue”; return theArray[front]; } template void Queue ::enqueue( const Object &newItem ) { if ( currentSize == theArray.size( ) ) doubleQueue( ); back = ++back % theArray.size( ); items[back] = newItem; ++currentSize; } template Object Queue ::dequeue( ) { Object frontItem = getFront( ); currentSize--; front = ++front % theArray.size( ); return frontItem; } 0 1 2 theArray.size( ) 7 6 front back Implementation

12 CSS342: Queues12 A Circular Array Implementation 0 1 2 3 MAX_Q -1 3 0 1 2 3 0 1 2 7 6 0 1 2 3 7 6 3 8 9 2 4 1 enqueue(7) enqueue(6) enqueue(3) enqueue(8) enqueue(9) enqueue(2) enqueue(4) enqueue(1) dequeue() (1)(2) (3) (4) Initial state count=0 count=2 count=8 count=0 front back front back front backfront Implementation

13 CSS342: Queues13 A Pointer-Based Implementation 2417 frontback NULL template class Queue { public: Queue( ) : front( NULL ), back( NULL ); Queue( const Queue &rhs ); ~Queue( ); bool isEmpty( ) const; const Object getFront( ) const; void flush( ); Object dequeue( ); void enqueue( const Object &newItem ); private: struct QueueNode { Object item; QueueNode *next; }; QueueNode *front; QueueNode *back; } Implementation

14 CSS342: Queues14 A Pointer-Based Implementation (deuque) 2 417 front back NULL template Bool Queue ::isEmpty( ) const { return front == NULL; } template const Queue Queue ::getFront( ) const { if ( isEmpty( ) ) throw “empty queue”; return front->item; } template Object Queue ::dequeue( ) { Object frontItem = getFront( ); Queue *old = frontPtr; if (front == back) { front = NULL; back = NULL; } else front = front->next; delete old; return frontItem; } back NULL front NULL Implementation

15 CSS342: Queues15 2 412 front back NULL template void Queue ::enqueue( const Ojbect& newItem ) { QueueNode *newPtr = new QueueNode; if ( newPtr == NULL ) throw “out of memory”; newPtr->item = newItem; newPtr->next = NULL; if (isEmpty( )) frontPtr = newPtr; else backPtr->next = newPtr; backPtr = newPtr; } back NULL front A Pointer-Based Implementation (enqueue) NULL Implementation

16 CSS342: Queues16 A Pointer-Based Implementation (copy constructor) 2417 frontback NULL template Queue ::Queue( const Queue &rhs ) { front = back = NULL; *this = rhs; } template Queue ::operator=( const Queue &rhs ) { if ( this != &rhs ) { flush( ); for ( QueueNode rptr = rhs.front; rptr != NULL; rptr = rptr->next ) enqueue( rptr->item ); } return *this; } rhs: lhs: 2417 NULL frontback Easier than stack! void Queue ::flush( ) { while ( !isEmpty( ) ) dequeue( ); } Implementation

17 CSS342: Queues17 Implementation by List Reuse Why not reuse the Linked List class we studied? Contain a linked list instance in our stack implementation Implementation #include #include "llist.h“ template class Queue { public: // the same as previous private: LList list; }; template bool Stack ::isEmpty( ) const { return list.isEmpty( ); } template const Object Stack ::getFront( ) const { return list.retrieve( 1 ); // retrieve 1 st list item } template void Stack ::flush( ) { return list.clear( ); } Consider by yourself about an implementation of Queue( const Queue &rhs ), enqueue( Object &newItem ), and dequeue( ) queue.hqueue.cpp

18 CSS342: Queues18 A Summary of Position-Oriented ADTs StackQueueList Check data existenceisEmpty getLength Insert a new data item push to topenqueue to backinsert anywhere Delete a specific data item pop from topdequeue from topdelete from any position Retrieve a data itemgetTopgetFrontRetrieve any item Then, why do we need stacks and queues? Implementation Comparing Implementations Completely the same as Stack.

19 CSS342: Queues19 Queues in Applications

20 CSS342: Queues20 Example 1: Palindromes Check if an input string is a parindrome: abcdedcba 1. Push and enqueue each of them to a stack and a queue. abcdedcba frontback Queue enqueue abcdedcba top Stack push 2. Pop and dequeue each from the stack and the queue, and compare them abcdedcba frontback Queue dequeue abcdedcba top Stack pop Queues in Applications

21 CSS342: Queues21 Example 1: Palindromes int main( int argc, char *argv[] ) { queue cQueue; stack cStack; if ( argc != 2 ) { cerr << "usage: " << argv[0] << " string" << endl; return -1; } for ( int i = 0; argv[1][i] != '\0'; i++ ) { cQueue.push( argv[1][i] ); cStack.push( argv[1][i] ); } while ( !cQueue.empty( ) ) { if ( cQueue.front( ) != cStack.top( ) ) { cout << "not a palindrome" << endl; return -2; } cQueue.pop( ); cStack.pop( ); } if ( !cStack.empty( ) ) { cout << "not a palindrome" << endl; return -2; } cout << "a palindrome" << endl; return 0; } Queues in Applications

22 CSS342: Queues22 Example 2: Computer Simulation Simulating the average waiting time of clients being served by servers –Banking service: clients = customers, servers = bank clerks –Car wash: clients = cars, servers = gas stations –Computer system: clients = processes, servers = CPUs –Network simulation:clients = packets, servers = routers Time = 20 Queues in Applications

23 CSS342: Queues23 Computer Simulation Client.h class Client { public: Client( ) : arrivalTime( 0 ) { }; Client( int time ) : arrivalTime( time ) { }; int getArrivalTime ( ) { return arrivalTime; }; private: int arrivalTime; }; Time = 12 Queues in Applications

24 CSS342: Queues24 Computer Simulation Server.h #include Using namespace std; class Server { public: Server( ) : currentTime( 0 ), numberOfClients( 0 ), sumOfWaitingTime( 0 ), nextDepartureTime( INF ) { } void runSimulation( ); void printResult( ); private: const static int INF = 10000; const static int SERVICE_TIME = 10; int currentTime; int nextDepartureTime; int numberOfClients; int sumOfWaitingTime; queue clientQueue; void newArrival( int newArrivalTime ); void giveService( ); }; Time = 20 Time = 38 Queues in Applications

25 CSS342: Queues25 Computer Simulation Server.cpp #include “Server.h” void Server::newArrival( int newArrivalTime ) { currentTime = newArrivalTime; numberOfClients++;// increments the total number of clients served cout << "Time=" << newArrivalTime << ": a new client has arrived" << endl; if ( nextDepartureTime == INF ) {// no one is being served at the beginning nextDepartureTime = currentTime + SERVICE_TIME; cout << "Time=" << nextDepartureTime << ": a client arrived at " << currentTime << " has served" << endl;// serve him/her immediately } else clientQueue.push( Client( newArrivalTime ) );// someone is being served }// a new client should wait. void Server::giveService( ) { currentTime = nextDepartureTime;// start serving the next person if ( !clientQueue.empty( ) ) {// from the queue Client client = clientQueue.front( ); clientQueue.pop( ); sumOfWaitingTime += currentTime - client.getArrivalTime( ); // calculate the waiting time nextDepartureTime = currentTime + SERVICE_TIME;// schedule the time to get // finished with serving him/her cout << "Time=" << nextDepartureTime << ": a client arrived at " << client.getArrivalTime( ) << " has served" << endl; } else nextDepartureTime = INF; } Queues in Applications

26 CSS342: Queues26 Computer Simulation Server.cpp (Cont’d) void Server::runSimulation( ) { int SENTINEL = 999; int newArrivalTime; cin >> newArrivalTime; while ( newArrivalTime != SENTINEL ) { if ( newArrivalTime < nextDepartureTime ) { newArrival( newArrivalTime ); cin >> newArrivalTime; } else giveService( ); } while ( nextDepartureTime < INF ) giveService( ); } void Server::printResult( ) { if ( numberOfClients == 0 ) cout << "No client requested a service" << endl; else cout << "avarage waiting time = " << double( sumOfWaitingTime ) / double ( numberOfClients ) << endl; } Time = 20 Time = 38 Time = 50 Time = 38 Queues in Applications

27 CSS342: Queues27 Computer Simulation driver.cpp and execution Queues in Applications #include "Server.h" int main( ) { Server s; s.runSimulation( ); } [mfukuda@perseus simulation]$ a.out 10 Time=10: a new client has arrived Time=20: a client arrived at 10 has served 15 Time=15: a new client has arrived 17 Time=17: a new client has arrived 22 Time=30: a client arrived at 15 has served Time=22: a new client has arrived 999 Time=40: a client arrived at 17 has served Time=50: a client arrived at 22 has served avarage waiting time = 9 [mfukuda@perseus simulation]$ New Arrival10151722999 numberOfClients01234 currentTime010151720223040 nextDepartureTimeINF10+10=2020 20 + 10 = 303030 + 10 = 4040 + 10 = 50 Queueempty 1515 171717 2222 sumOfWaitingTime00000 + 20 - 15 = 555 + 30 - 17 = 1818 + 40 - 22 = 36 ave. waiting time36/4 = 9 A larger value A smaller value

28 CSS342: Queues28 1cell/time unit 1cell/2.4 time units 1cell/8 time units Discrete Event Computer Simulation Queues in Applications

29 CSS342: Queues29 f2 f5 f6 f4 f3 f7 f11 f10 f9 f8 f12 f1 s16 attack t2.4 s8 t4.8 t7.2 X deleted X Discrete Event Computer Simulation f1t2.4s8f2t2.4s8f3t2.4s8f3t4.8s8 f4t4.8s8f5t4.8s8f5t7.2s8f6s8 Time = 1 Time = 2Time = 2.4Time = 3 Time = 6Time = 5Time = 4.8Time = 4 Queues in Applications

30 CSS342: Queues30 Priority queues: –The front item is always the smallest. Three types where O(1) search for the front item: –Queues with insertion sort: O(N) search/insertion –Skip lists: O(log N) search/insertion –Binary search trees and heaps: O(log N) search/insertion Discrete Event Computer Simulation Queues in Applications

31 CSS342: Queues31 Very Large Integer Definition RSA (public/private key) encryption –Picks up two prime numbers p and q, each 256 bits. –Finds e whose GCD with (p – 1)(q – 1) is 1. –Encrypts m with m e % (p * q) –Descrypts c with c d % (p * q), where d = e -1 mod ((p - 1) x (q - 1)) How can we represent p, q, e, d, m e, and c d ? –Represent an integer with a queue of digits Queues in Applications 19487 a variable length of digits in a queue

32 CSS342: Queues32 Very Large Integer Arithmetic Operations Addition: + Subtraction: – Queues in Applications 1 5 0 7 8 digit i+1digit i lhs: rhs: + + result: 4 3 + + 8 carry: 1 9 0 7 8 digit i+1digit i lhs: rhs: – – result: 4 3 – – 0 borrow: Enqueue digits to the tail upon a user input. Dequeue digits from the tail upon an arithmetic operation. Enqueue results from the front.

33 CSS342: Queues33 Very Large Integer Deque Queues in Applications 74 template class Deque { public: Deque( ); Deque( const Deque &rhs ); ~Deque( ); bool isEmpty( ) const; // checks if a deque is empty. int size( ) const; // retrieves # deque nodes const Object &getFront( ) const; // retrieve the front node const Object &getBack( ) const; // retrieve the tail node void clear( ); // clean up all deque entries. void addFront( const Object &obj ); // add a new node to the front void addBack( const Object &obj ); // add a new node to the tail Object removeFront( ); // remove the front node Object removeBack( ); // remove the tail node const Deque &operator=( const Deque &rhs ); // assignment private: struct DequeNode { // a deque node Object item; DequeNode *next; DequeNode *prev; }; DequeNode *front; DequeNode *back; }; 9

34 CSS342: Queues34 Queues in OS

35 CSS342: Queues35 Unix Message Queues struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); strcpy( message_body.mtext, “ hello world\n ” ); msgsnd( msgid, &message_body, 512, 0 ); } struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); msgrcv( msgid, &message_body, 512, 0, 0 ); cout << message_body.mtext << endl; } Message queue (id = msgid) 0 1 2 Some other process can enqueue and dequeue a message Queues in OS

36 CSS342: Queues36 Multilevel Queue Scheduling Each queue has its own scheduling algorithm, –foreground (interactive) – RR, 80% CPU time –background (batch) – FCFS, 20% CPU time Queues in OS

37 CSS342: Queues37 Multilevel Feedback-Queue Scheduling A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. Queues in OS

38 CSS342: Queues38 Queues in Network

39 CSS342: Queues39 packet Flow Control Sending applicationReceiving application Send Socket Buffer Receive Socket Buffer packet send ack X read blocked Application is slow to read. X packet droppedpacket retransmitted X packet droppedpacket retransmitted X write blocked Queues in Network

40 CSS342: Queues40 Congestion in Packet-Switched Network Source cannot directly observe the traffic on the slow network Destination 1.5-Mbps T1 link Router Source 2 1 100-Mbps FDDI 10-Mbps Ethernet Flow 1 Flow 2 Flow 3 Flow 4 Round-robin service Queues in Network


Download ppt "CSS342: Queues1 Professor: Munehiro Fukuda. CSS342: Queues2 Topics Basic concepts of queue Queue implementation Queues used in –Applications –Operating."

Similar presentations


Ads by Google