placed onto a stack is the first item removed. The operations for inserting andremoving items from a stack are known as pushand pop, respectively. An operatingsystem often uses a stack when invoking function calls. Parameters, local variables,and the return address are pushed onto the stack when a function is called; returningfrom the function call pops those items off the stack.A queue, in contrast, is a sequentially ordered data structure that uses the firstin, first out (FIFO) principle: items are removed from a queue in the order in whichthey were inserted. There are many everyday examples of queues, includingshoppers waiting in a checkout line at a store and cars waiting in line at a trafficsignal. Queues are also quite common in operating systems — jobs that are sent to aprinter are typically printed in the order in which they were submitted, for example.As we shall see in Chapter 5, tasks that are waiting to be run on an available CPUareoften organized in queues.1.9.2 TreesA treeis a data structure that can be used to represent data hierarchically. Datavalues in a tree structure are linked through parent – child relationships. In ageneral tree, a parent may have an unlimited number of children. In abinarytree, a parent may have at most two children, which we term theleft childand theright child. A binary search treeadditionally requires an ordering between theparent’s two children in which left child<=right child. Figure 1.20provides an example of a binary search tree. When we search for an item in abinary search tree, the worst-case performance is O(n) (consider how this can occur).To remedy this situation, we can use an algorithm to create a balancedbinarysearch tree. Here, a tree containingnitems has at mostlg nlevels, thusensuring worst-case performance of O(lg n). We shall see in Section 5.7.1 that Linuxuses a balanced binary search tree (known as a red-black tree) as partits CPU-scheduling algorithm.1.9.3 Hash Functions and MapsA hash functiontakes data as its input, performs a numeric operation on the data,and returns a numeric value. This numeric value can then be used as an index into atable (typically an array) to quickly retrieve the data. Whereas searching for a dataitem through a list of size ncan require up to O(n) compar-isons, using a hashfunction for retrieving data from a table can be as good as O(1), depending onimplementation details. Because of this performance, hashfunctions are usedextensively in operating systems.One potential difficulty with hash functions is that two unique inputs can resultin the same output value — that is, they can link to the same table
1.9 Kernel Data Structures391712356144038Figure 1.20Binary search tree.location. We can accommodate this hash collisionby having a linked list at the tablelocation that contains all of the items with the same hash value. Of course, the morecollisions there are, the less efficient the hash function is.