This action might not be possible to undo. Are you sure you want to continue?

Basic Concepts

A queue is a pile in which items are added an one end and removed from the other. In this respect, a queue is like the line of customers waiting to be served by a bank teller. As customers arrive, they join the end of the queue while the teller serves the customer at the head of the queue. As a result, a queue is used when a sequence of activities must be done on a first-come, first-served basis.

Basic Concepts

For example, consider the software which manages a printer. In general, it is possible for users to submit documents for printing much more quickly than it is possible to print them. A simple solution is to place the documents in a FIFO queue. In a sense this is fair, because the documents are printed on a first-come, firstserved basis.

a user who has submitted a short document for printing will experience a long delay when much longer documents are already in the queue. An alternative solution is to use a priority queue in which the shorter a document. we reduce the level of frustration experienced by the users. In fact. it can be shown that printing documents in order of their length minimizes the average time a user waits for her document. the higher its priority.Basic Concepts However. By printing the shortest documents first. .

However. Items are inserted into a priority queue in any. arbitrary order. .Basic Concepts A priority queue is essentially a list of items in which each item has associated with it a priority. items are withdrawn from a priority queue in order of their priorities starting with the highest priority item first. Given such a list we can determine which is the highest (or the lowest) priority item in the list. In general. different items may have different priorities and we speak of one item having a higher priority than another.

Typically the problem to be solved consists of a number of subtasks and the solution strategy involves prioritizing the subtasks and then performing those subtasks in the order of their priorities.Basic Concepts Priority queues are often used in the implementation of algorithms. .

Applications of Priority Queues CPU Scheduling .

The next CPU burst of the newly arrived process may be shorter than what is left of the currently executing process.Applications of Priority Queues CPU Scheduling The SJF algorithm can be either preemptive or nonpreemptive. whereas a non-preemptive SJF algorithm will allow the currently running process to finish its CPU burst. Preemptive SJF scheduling is sometimes called shortest-remaining-time-first scheduling. The choice arises when a new process arrives at the ready queue while a previous process is still executing. . A preemptive SJF algorithm will preempt the currently executing process.

preempt.Applications of Priority Queues CPU Scheduling Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time Two schemes: nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process. This scheme is know as the Shortest-Remaining-Time-First (SRTF) SJF is optimal – gives minimum average waiting time for a given set of processes .

0 2.Applications of Priority Queues CPU Scheduling Example of Non-Preemptive SJF Process P1 P2 Arrival Time 0.0 Burst Time 7 4 P3 P4 SJF (non-preemptive) 4.0 1 4 P1 0 3 7 P3 8 P2 12 P4 16 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 .0 5.

0 4.0 P3 P2 5 7 P4 Burst Time 7 4 1 4 P1 11 16 P1 0 2 P2 4 Average waiting time = (9 + 1 + 0 +2)/4 = 3 .0 2.Applications of Priority Queues CPU Scheduling Example of Preemptive SJF Process P1 P2 P3 P4 SJF (preemptive) Arrival Time 0.0 5.

and the CPU is allocated to the process with the highest priority. A priority is associated with each process.Applications of Priority Queues CPU Scheduling The SJF algorithm is a special case of the general priority scheduling algorithm. . and vice versa. The larger the CPU burst. the lower the priority. Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU burst.

Applications of Priority Queues CPU Scheduling Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive nonpreemptive SJF is a priority scheduling where priority is the predicted next CPU burst time Problem Starvation – low priority processes may never execute Solution Aging – as time progresses increase the priority of the process .

0 2.0 2.0 1.0 1.Basic Concepts Priority Scheduling Process P1 P2 P3 P4 P5 Priority (preemptive) Arrival Time (s) 0.8 s .0 Burst Time (s) 4 1 2 8 5 Priority 2 1 3 4 1 P1 0 1 P2 2 P5 7 P1 10 P3 12 P4 20 Average waiting time = 0 + (1-1) + (10-1) + (12 -2) + (2-2) /5 = 3.

Process P1 P2 P3 P4 P5 Priority (preemptive) Arrival Time (s) 0.0 1.0 2.0 Burst Time (s) 4 1 2 8 5 Priority 2 1 3 2 1 P1 0 1 P2 2 P5 7 P1 10 P4 18 P3 20 Average waiting time = 0 + (1-1) + (18-1) + (10 -2) + (2-2) /5 = 5 s .0 2.0 1.

This time we'll describe another structure that can be used to implement a priority queue: the heap. Thus it's not quite as fast for deletion. A heap is a kind of tree. . It's the method of choice for implementing priority queues where speed is important and there will be many insertions. but much faster for insertion. It offers both insertion and deletion in O(logN) time.

The relationship of skew heaps to leftist heaps is analogous to the relation between splay trees and AVL trees. so the worstcase running time of all operations is O(n).Skew Heaps A skew heap is a self-adjusting version of a leftist heap that is incredibly simple to implement. . Unlike leftist heaps. but there is no structural constraint on these trees. The right path of a skew heap can be arbitrarily long at any time. Skew heaps are binary trees with heap order. no information is maintained about the null path length of any node.

The merge routine is once again recursive. The difference is that for leftist heaps. with one exception. the fundamental operation on skew heaps is merging. In Skew Heaps unconditionally swapping all nodes in the merge path Skew Heaps attempt to maintain a short right path from the root. and we perform the exact same operations as before. . we check to see whether the left and right children satisfy the leftist heap order property and swap them if they do not.Skew Heaps As with leftist heaps.

.Skew Heaps The following diagram is a graphical representation of a Skew Heap.

Skew Heaps From the above diagram it can be seen that a Skew Heap can be “heavy” at times on the right side of the tree. As the following diagram shows by inserting the element ‘45’ into the above heap a short right hand side is achieved: . Depending on the order of operations Skew Heaps can have long or short. right hand side path lengths.

Element e) 5. FindMin (Heap h) 3. Union (Heap h1. Heap h2) . DeleteMin (Heap h) 4.Skew Heaps Algorithm Skew Heaps are of interest as they do not maintain any balancing information but still can achieve amortized log n time in the Union Operation. Insert (Heap h. MakeHeap (Element e) 2. The following operations can be executed on Skew Heaps: 1.

Skew Heaps The only difference between a skew heap and a leftist heap is the merging operation is treated differently in skew heaps. The purpose of the swapping is to keep the length of the right path bounded. even though the length of the right path can grow to Omega (n). The swapping of the children of a visited node on the right path is performed unconditionally. it is quite effective. the dist value is not maintained. .

So. . Then by swapping everything unconditionally a relatively “light” right side is created.Skew Heaps The reasoning behind this is that insertions are made on the right side and therefore creating a “heavy” right side. the good behavior of skew heaps is due to always inserting to the right and unconditionally swapping all nodes.

FindMin(Heap h) if (h == null) return null. else return h.key .Skew Heaps MakeHeap (Element e) return new Heap(e).

Element e) z = MakeHeap(e). h = Union (h. Insert (Heap h.right). return e. . h.Skew Heaps DeleteMin(Heap h) Element e = h.key.left. h = Union (h. z).

key > h2. else { // Assure that the key of h1 is smallest if (h1. . if (h1 == null) return h2.Skew Heaps Union (Heap h1. h2 = dummy. h1 = h2.key){ Node dummy = h1. 12 else if (h2 == null) return h1. heap h2) Heap dummy.

left = dummy.right == null) // Hook h2 directly to h1 h1.right = h1.right = Union (h1. h1.right = h2. . // Swap children of h1 dummy = h1. h1. else // Union recursively h1. return h1.right.left.Skew Heaps }} if (h1.right. h2).

but not vice versa .Skew Heaps · Skew heaps have heap order but no structural constraint. but for M consecutive operations. O(M lgN) · recursively merge the heap with larger root with the right subheap of the smaller root. thus simple to implement. and always swap · slight loss of balance information is compensated by the lack of testing · a leftist heap is a skew heap. Self-adjusting and no need to maintain the path lengths. · O(N).

Skew Heaps .

30 Silberschatz.Operating System Concepts – 7th Edition. Galvin and Gagne ©2005 . Feb 2. 2005 5.

Feb 2. 2005 5.31 Silberschatz.Operating System Concepts – 7th Edition. Galvin and Gagne ©2005 .

Feb 2.32 Silberschatz. Galvin and Gagne ©2005 .Operating System Concepts – 7th Edition. 2005 5.

33 Silberschatz. Galvin and Gagne ©2005 .Operating System Concepts – 7th Edition. 2005 5. Feb 2.

34 Silberschatz.Operating System Concepts – 7th Edition. Galvin and Gagne ©2005 . Feb 2. 2005 5.

Operating System Concepts – 7th Edition. Feb 2.35 Silberschatz. Galvin and Gagne ©2005 . 2005 5.

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd