Abstract:
A search mechanism improves the performance of a queue system including a queue for storing a plurality of data items and search mechanism by maintaining a key cache data structure having an array of entries, each of which have a key field and a pointer field. The key and pointer fields respectively of each cache entry are used for storing a key value of a different one of the enqueued data items of the queue and a pointer to that enqueued item. The key of each data item to be enqueued is used to generate an index value for accessing a location of the key cache array to obtain immediate access to the corresponding enqueued data item thereby reducing the search time for determining the proper point within the queue for inserting the data item to be added.

Description:
This patent application claims the benefit of Provisional Application Ser. No. 60/033,834, filed on Dec. 23, 1996. 
    
    
     BACKGROUND OF THE INVENTION 
     1. Field of Use 
     The present invention relates to data processing system and more particularly to queuing systems. 
     2. Related Art 
     A frequent activity in data processing systems is the enqueueing of a data item. Typically, these systems maintain many queues internally. Often, the queues correspond to waiting-lines of work to be done by the data processing system so that when an item is to be added to the queue, the queue system simply appends the item to the end of the queue and when an item is to be removed, the queue system removes the item from the front of the queue. 
     Sometimes the items in a queue system must be held in the queue in a particular order which is dependent upon a particular field or fields contained in the item. An example is where the queue system functions as a scheduler and represents the tasks to be executed by the data processing system. In such cases, each of the data items normally contains a field representing the priority of the task and the data items representing low priority tasks are enqueued after the data items representing higher priority tasks. To add such a low priority data item to the queue system, the simplest implementation requires that the queue insertion mechanism perform a sequential search of the queue beginning with the data item at the front of the queue system and examining each enqueued data item in turn until the insertion mechanism locates a data item having a priority which is the same or lower than the priority of the data item to be inserted into the queue system. 
     When the queue system contains many data items, the sequential search can require a substantial amount of time thereby detracting from the overall efficiency of the data processing system. In general, the search time grows linearly with the number of data items in the queue system. 
     A queue is generally represented in a data processing system by a data structure to implement the queue itself along with multiple data structures for each of the data items. That is, each data item contains fields for its data in addition to extra fields for implementing the queue structure. Generally, a simple queue implements the queue structure as a single data item which corresponds to the memory address of the first queued data item and each data item has one field dedicated to the queue structure which contains the memory address of the next data item in the queue. 
     Alternative queue data structures are well known to those skilled in the art. A variation of the simple queue structure is the double linked list organization which contains two fields per data item, one field containing the memory address of the next enqueued data item and the other field containing the memory address of the preceding data item in the queue. This organization simplifies the addition and removal of data items from the middle of the queue by being able to search for data items in either a forward or backward direction. Thus, while a simple ordered queue has very efficient insertion and removal of data items, there is still a substantial search burden when the size of the queue is substantial. 
     Another known queue data structure is one representing the ordered queue as a tree-shaped data structure with the branching of the tree being dependent on the value of the key field or fields so that search time can grow as the logarithm base two of the number of items in the tree provided that the tree is balanced. Unfortunately, maintaining the tree in a balanced state is expensive when there are frequent additions and removals of data items. 
     Another known queue data structure is a &#34;skip list&#34; which holds multiple copies of a data item in multiple queues. The lowest level queue contains all of the items to be enqueued while the higher level queues hold just a subset of items chosen at random from the immediate lower queue. In this arrangement, a search proceeds from the highest level queue, migrating down to the next lower level queue when a pseudo match is obtained. This queue structure is less expensive to maintain than a balanced tree queue structure and provides probabilistic speedups in search time. But, the structure pays a penalty in terms of memory space (i.e. multiple copies of the data items are needed) and its insertion and deletion mechanisms are more expensive as compared to the simple queue structure. 
     Accordingly, it is a primary object of the present invention to provide an efficient, probabilistic search mechanism for ordered queues. 
     It is a further object of the present invention to provide an efficient search mechanism which provides for efficient insertion and removal of data items. 
     SUMMARY OF THE INVENTION 
     The above objects are achieved in a preferred embodiment of the search mechanism of the present invention for queues holding or containing data items ordered according to the value of one or more fields contained in the data item. The queue can be considered conventional in design. For example, the queue contains data items, each of which has a pair of queue specific data fields which indicate the locations of preceding data item and succeeding data item in addition to containing a further data structure containing two pointer fields which indicate the first data item and last data item in the queue. Additionally, in the preferred embodiment, the queue always maintains a first item and a last item which may not be removed. The first item contains a key value which is less than any real key value and the last item contains a key value which is larger than any real key value. In this embodiment, the items in the queue contain key values which are in ascending order; that is, the item following any item in the queue has a key which is the same as or greater than the given item. 
     It will be clear that an equivalent embodiment of the present invention using key values in descending order and with the key values in the first and last items respectively being set to a value larger than any real key value and a value smaller than any real key value is practical and will operate in a manner analogous to the present embodiment. 
     The search mechanism of the present invention includes one or more data structures contained in a key cache. The key cache is an array of data item entries, each of which contains two fields, a key field used for storing a unique key value and a pointer field used for designating a data item in the queue. The key cache may have as many such data item entries as convenient. In the preferred embodiment, the data item entries are numbered consecutively from 0 to some number n when the key cache contains n+1 items. 
     In operation, initially, the key cache and queue are empty and this condition is represented by having the pointer field of each key cache entry contain an invalid value which is typically zero. When a data item it to be added to the queue, the queue system generates an index by performing a computation operation using the key value of the new data item. The result of the computation operation yields an index value between 0 and n which is used to index into the key cache. For example, if the computation operation result yields the value x, then the xth entry in the key cache is accessed. 
     Next, the search mechanism determines if the key cache entry is valid or not valid (i.e. has a value=0). If invalid, the search mechanism updates the key cache entry so that the key field contains the key value of the new data item. Next, the search mechanism searches the queue from the front and inserts the new data item into the queue in a conventional manner. If the key cache entry is valid, then the search mechanism examines the key field of the entry as follows. If the key field of the key cache entry contains a value which is the same as the key of the data item to be inserted, the search mechanism inserts the new data item into the queue at a point immediately before the data item designated by a search mechanism pointer field and updates the key cache pointer field to point at the new data item. If the key field of the key cache entry contains a value which is smaller than key of the data item to be inserted, then the search mechanism searches the entries of the queue &#34;backwards&#34; from the data item designated by the search mechanism pointer field until the search mechanism finds an insertion point. If the key field of the key cache entry is larger than the key of the data item to be inserted, the search mechanism searches the entries of the queue &#34;forwards&#34; until the search mechanism finds an insertion point. In either of these last two cases, the search mechanism updates both the key and pointer fields of the appropriate key cache entry to contain the key value of the new data item and to point to it. 
     The search mechanism of the present invention removes queue data items in an expeditious manner. When a data item is removed from the front of the queue, the search mechanism examines the corresponding entry location (key cache slot) in the key cache. If the key value of the key cache entry designates this data item, the search mechanism marks the contents of the location as invalid. 
     The search mechanism of the present invention reduces the number of queue entries required to be searched by an amount which is a function of the distribution of key values and the size of the key cache data structure. When used in different applications, the present invention is able to reduce search time and improve overall system performance. 
    
    
     The above objects and advantages of the present invention will be better understood from the following description when taken in conjunction with the accompanying drawings. 
     BRIEF DESCRIPTION OF THE DRAWINGS 
     FIG. 1 is an overall block diagram of a data processing system which utilizes the search mechanism of the present invention. 
     FIG. 2 shows in greater detail, the organization of the queuing system of FIG. 1 which incorporates the search mechanism of the present invention. 
     FIGS. 3a and 3b are diagrams used in describing the steps of performing an insertion operation for an empty queue according to the teachings of the present invention. 
     FIGS. 4a and 4b are diagrams used in describing the steps of performing an insertion operation for a queue according to the teachings of the present invention. 
     FIGS. 5a and 5b are diagrams used in describing the steps of performing a removal operation for a queue according to the teachings of the present invention. 
     FIGS. 6a through 6e are flow charts used in explaining the operation of the search mechanism of the present invention. 
    
    
     DESCRIPTION OF THE PREFERRED EMBODIMENT 
     FIG. 1 
     FIG. 1 is a block diagram of a conventional data processing system 10 which includes a processor 12 which couples to a memory system 20. The processor 12 operatively couples to a queue system 22 which includes the queue search mechanism 22-2 constructed according to the teachings of the present invention to manage a conventional queue structure 22-1. As shown, the queue search mechanism 22-2 includes a key cache structure 22-40 and a control mechanism 22-20 which contains procedures for performing queue insertion and removal operations for queue 22-1 according to the teachings of the present invention. 
     FIG. 2 
     FIG. 2 shows in greater detail, the organization of queue system 22. As shown, the key cache 22-40 is an array which contains a plurality of entry locations for storing items, each item in the form of a data structure containing a key field for storing a key value and a pointer field for storing a queue address pointer value. 
     The key value may represent a particular time or priority allocation. For example, when the queue system 22 being used to process events in a discrete event simulator, each event represents some important activity of the system being modeled and each event contains an indication of the time the action is supposed to occur and the details of what action is to be taken. This time value is used as the key field. 
     In this application, the intended time of the new item is used to select an entry in the key cache structure 22-40. In this example, the intended time of the new item is used to select an entry in the key cache structure 22-40. In such an application, the simulation may be modeling the behavior of a microprocessor in which increments of time of one nanosecond are used as the time value. When the simulation is handling the simulation of the 100,0000th instruction to be executed, the time and hence the key value could have a value of 200,000. In such an application, the computation of Index from Key value and the size of the cache are jointly chosen so as to distribute the index value approximately uniformly over the size of the key cache. 
     In implementing the queue system to accommodate this application, each item in the queue is represented by a data structure which contains a key field for the intended time, a pointer field to represent the type of action needed implemented as the address of a procedure which can carry out the specified action in addition to backward and forward pointer fields as discussed herein. In this application, the search mechanism 22-2 performs a mathematical function on the time contained in the key field of the item which produces a result having a range of 0 through n where there are n+1 entry locations in the key cache structure 22-40. Typically, the function computes the remainder when the key value (i.e. the time) is divided by n+1. 
     To insert a new data item, &#34;NewItem&#34; for time T, the following general series of operations are performed: 
     
         ______________________________________set Ptr, a pointer, equal to the value of Headset Searching to TRUEwhile Searching is TRUEif Ptr.Time is greater than Tinsert NewItem before Ptrset Searching to FALSEotherwise set Ptr to Ptr.Next______________________________________ 
    
     wherein &#34;insert&#34; means to perform all of the required operations to put the NewItem into the queue. 
     The search mechanism of the present invention reduces the number of times required to execute the while loop by also maintaining the key cache structure 22-40 of FIG. 2 as described herein. 
     In addition to the key cache structure 22-40, FIG. 2 shows in greater detail, the organization of the queue 22-1. As indicated, queue 22-1 includes head and tail pointers within a queue header structure and a number of enqueued items. Each item has a next and previous pointer fields (forward and backward linked pointers), a key field and a number of data fields. In FIG. 2, four enqueued items of the queue 22-1 are shown with their respective next and previous pointer fields being set to values such that the fifth key cache entry points to the third enqueued item of queue 22-1. In the preferred embodiment, the queue 22-1 is structured so that the queue always contains a First item and a Last item which have respectively, a key value smaller than any to be used by the application and a key value larger than any to be used by the application. This simplifies the insertion and deletion mechanisms, since they then do not need to check for insertion into an empty queue, the head of the queue or the tail of the queue, nor for deletion from such sites. 
     DESCRIPTION OF OPERATION 
     With reference to FIGS. 1 through 5b, the operation of the queue search mechanism 22-2 constructed according to the present invention will now be described in conjunction with the flow charts of FIGS. 6a through 6e. 
     FIG. 3a 
     First, the queue system 22 is initialized by system 10 of FIG. 1. More specifically, key cache structure 22-40 is an array containing n number of entries, each having a key field and a pointer field. After being initialized, the key field of each entry contains a different key value derived as discussed above while the pointer field contains a &#34;0&#34; value as indicated. The head and tail pointers of queue 22-1 also contain &#34;0&#34; values as shown. 
     FIG. 3b illustrates the state of the queue system 22 after the first data item (NewItem) has been enqueued properly. Since this is the first data item, its next and previous pointers are both set to &#34;0&#34; as indicated. The control mechanism 22-20 computes the cache index value from the key value. In the case of the time event simulator application, this is done by performing the operation of T % N wherein % denotes a remainder operation as discussed herein. The result &#34;x&#34; defines the entry location within the key cache structure 22-40 into which the new item is to be inserted as indicated in FIG. 3b. 
     In an application in which the behavior of a microprocessor is being simulated through the means of a discrete event simulation, the key value may be some multiple of the base clock interval of the simulated processor. In an example where the clock interval is 10 nanoseconds and the execution of the 10,000th instruction is being simulated, the key value might plausibly have a value such as 20,000. In that case, if the key cache contained 24 entries the index would be 20,000% 24, or 8 wherein the symbol &#34;%&#34; designates the remainder obtained by dividing 20,000 by 24 which results in the remainder of 8. 
     FIGS. 4a and 4b 
     FIG. 4a illustrates the state of the queue system 22 during normal operation wherein all of the queue and cache key structure locations have been filled with entries as indicated. In FIG. 4a, it is assumed that the fifth key cache item points to the second enqueued item in queue 22-1. 
     Item Insertion 
     The steps for performing an insertion operation by the search mechanism 22-2 are illustrated in the flow chart of FIG. 6a. From FIG. 6a, it is seen that first, the control mechanism 22-20 reads the key from the data item to be inserted (i.e., block 600). Next, the control mechanism 22-20 computes a key cache index value from the key value contained in the data item to be enqueued (i.e., block 602). As indicated, there are then four cases to consider: 
     The first case is when the key cache entry is not valid (shown by the Pointer field of the entry being zero). In this case, the mechanism sets the queue pointer Ptr to the first real entry in the queue, the item following First. 
     The second case is when the key cache entry is valid, and the key value in the item being inserted is greater than the key value in the key cache entry. In this case, the mechanism searches the queue in a forward direction starting at the item indicated by the pointer field of the key cache entry using the queue next item pointer values to find the appropriate point for insertion. 
     The third case is when the key cache entry is valid and the key value in the item being inserted is less than the key value in the key cache entry. In this case, the mechanism searches the queue in a backward direction starting at the item indicated by the pointer field of the key cache entry using the queue next item pointer values to find the appropriate point for insertion 
     The fourth case is when the key cache entry is valid and the key value in the item is the same as the key value in the key cache entry. In this case, the pointer field of the key cache entry indicates the correct entry point. 
     It will be assumed by way of example that the key value of the data item being added is greater causing control mechanism 22-20 to perform the QBwd backward procedure operation of block 610 (i.e., a procedure to search a queue backwards from the item indicated by PTR until an entry whose key field is less than or equal to the supplied key is found and which returns a pointer to that item). As indicated, this operation is illustrated in greater detail in FIG. 6b. First, the control mechanism procedure sets its pointer Here to the value contained in the pointer Ptr passed to it which corresponds to the previous item pointer value of the key cache designated enqueued item (i.e. block 610-4). The key field contents of the queue entry pointed to by its pointer Here are then compared with the key value (Key) of the item to be inserted (NewItem) as indicated in block 610-6. 
     If the key value of the designated enqueued item entry is equal or less than the NewItem key value, control mechanism 22-20 sets the search mechanism Ptr to equal the contents of its Here pointer representing the location of the previous data item (block 610-8). If the key value of the enqueued item entry is greater than the NewItem key value, control mechanism 22-20 performs the operation of block 610-10. That is, control mechanism 22-20 sets the search mechanism pointer Ptr equal to the contents of the Previous pointer field of that queue item to evaluate the next enqueued item (block 610-12). 
     Control mechanism 22-20 continues to execute the operations of blocks 610-6 and 610-10 until it finds a queue item entry whose key field is less than or equal to the supplied item to be enqueued. When such a queue item is found, control mechanism 22-20 then returns a pointer value designating the location of that enqueued item. Since control mechanism 22-20 carries out a forward search in the same manner as a backward search, using the next pointer values of the enqueued items, no further description of a forward search is deemed necessary. 
     As indicated in FIG. 6a, the control mechanism 22-20 uses the returned pointer value to insert the NewItem into queue 22-1 as indicated in block 612. That is, the control mechanism 22-20 inserts the NewItem in the queue location designated by the returned pointer value which results in the pointer values of the enqueued items being set as indicated in FIG. 4b. 
     When the queue pointer Ptr points to an item in the body of the queue (i.e., does not indicate the first or last item in the queue), control mechanism 22-20 performs the operations of block 612-2 in the flow chart of FIG. 6c that depicts a procedure to insert an item into a queue at the point indicated by PTR. Note the queues in the preferred implementation always have a first entry and a last entry which may not be removed so the insertion procedure always inserts in the `middle` of queue. That is, it sets the next pointer value of the NewItem to the value of the queue pointer, sets the next pointer value of the NewItem to the value of the previous pointer of the designated enqueued item, sets the previous pointer of the previous item pointed to by the queue pointer to point at the NewItem and sets the previous pointer of the previous item to point at the NewItem. 
     Next, control mechanism 22-20 updates the entry in key cache structure 22-40 in the manner indicated in block 614-2 of the flow chart of FIG. 6d to reflect the insertion of this item and its key value. That is, it sets the pointer field of the entry to point to the new item and sets the time field (key field) to be equal to the time of the new item (NewItem). 
     Item Removal 
     FIGS. 5a and 5b are used to describe the operations to remove the first useful item in the queue performed by the procedure getItem illustrated in the flow chart of FIG. 6e (recall that the actual first and last items are not to be removed). As indicated in FIG. 5a, it is assumed that the fifth key cache item points to the item being removed. The choice of item in the keycache is defined by the computation of the cache key index from the key value of the item being removed. This first useful item is the second item in the queue. The procedure uses two pointer values called `first` and `item` to remove the item from the queue. 
     As indicated in block 620, the procedure sets its &#34;first&#34; pointer equal to Head.Next (i.e., the contents of the next field of the data item pointed to by the queue head pointer, indicating the actual first item). It also sets its &#34;item&#34; pointer equal to first.Next (indicating the second item in the queue 22-1, the item to be removed). Next, the procedure removes the item from the queue by performing the operations of setting the first.Next pointer equal to item.Next and the item.Next.Previous equal to first (block 621). This operation (step) removes all mention (identification) of the item to be removed from the queue. 
     Next, the procedure computes the key cache index from the key of the data item to be removed and then points the entry at the Index&#39;th entry in the key cache (blocks 622 and 624). The procedure then compares the pointer Ptr to the Entry.Pointer for determining if the pointer is the same (block 626). If it is the same, the pointer field of the entry is set to a NULL or &#34;0&#34; value invalidating the entry (block 628). If the pointer Ptr is different, then the procedure is done. 
     The queuing mechanism may also be used to remove a specific item known to be in the queue. To remove a particular item the procedure getSpecificItem also illustrated in the flow chart of FIG. 6e may be used. It is provided with the address of the item to be removed. 
     As indicated in block 620a, the procedure sets its &#34;first&#34; pointer equal to Item.Previous (i.e., the contents of the previous field of the item to be removed indicating the item preceding the item to be removed). It also sets its &#34;item&#34; pointer equal to Item (the item to be removed). Next, the procedure removes the item from the queue by performing the operations of setting the first.Next pointer equal to item.Next and the item.Next.Previous equal to first (block 621). This operation (step) removes all mention (identification) of the item to be removed from the queue. The sequence of operations of the remaining blocks of FIG. 6e would be performed in the same manner as described above. 
     To remove the first item whose key was smaller than some value, one would, in principle, first search the queue from the front to identify that item, and then remove it. The removal can be done with getSpecificItem(), just described. The searching could be done using a version of insertItem(), modified only to call getSpecifiItem() instead of insertinQ() and to never call updateCache(). 
     From the above, it is seen how the search mechanism of the present invention can be used to reduce search time from a time dependent on the number of entries in a queue to a time which is essentially constant. This reduction in search time is a function of the distribution of times for new items and size of the key cache. 
     The following results have been found by using the queue mechanism as an ordered event queue wherein each event when serviced creates another event at some time in the future and the time is chosen from a uniform distribution. The results shown below are for a queue using 512 events whose &#34;next&#34; time is set to be the current time plus a value chosen randomly from the range 0 to 63 ticks in the future. 
     An uncached run: 
     Experiment::NumEvents=512, dt=U[0 . . . 63], CacheSize=0 
     Service 512 events. 
     40269 events in 11.12 secs=3622.40 eps; av search distance=334.77 
     80928 events in 22.33 secs=3623.64 eps; av search distance=334.27 
     121364 events in 33.52 secs=3621.00 eps; av search distance=334.36 
     162027 events in 44.77 secs=3619.37 eps; av search distance=334.43 
     A cached run: 
     Experiment::NumEvents=512, dt=U[0 . . . 63], CacheSize=8 
     Service 512 events. 
     40269 events in 5.98 secs=6725.18 eps; av search distance=165.47 
     Hits=5045 [12.54] Slops=17601 [43.74] Misses=17593 [43.72] 
     80928 events in 12.15 secs=6654.49 eps; av search distance=166.14 
     Hits=10020 [12.39] Slops=35411 [43.80] Misses=35421 [43.81] 
     121364 events in 18.22 secs=6660.00 eps; av search distance=165.92 
     Hits=15005 [12.37] Slops=52955 [43.65] Misses=53363 [43.98] 
     162027 events in 24.33 secs=6657.82 eps; av search distance=165.79 
     Hits=20114 [12.42] Slops=70793 [43.70] Misses=[43.89] 
     wherein slops are hits which result in a search. 
     The above results illustrate that a very small cache (8 entries) halves the search distance (from 334 to 166) and also improves the actual event servicing rate from about 3600 eps to about 6600 eps. 
     An executable description of the queuing system embodying the present invention is provided in the enclosed Appendix. 
     APPENDIX 
     It can be simpler to understand how an implementation of the mechanism might work if the description is given in a language more suited to such descriptions than English. The programming language `C` is such a language and to aid comprehension a description of the behavior of a possible implementation of the mechanism is given in C as a collection of C procedures hereafter. The version of the mechanism described employs the preferred queuing model in which the first and last items in the queue are unremovable and unchangeable. To further increase the utility of this description, two further procedures (uniform() and main()) are included so that the complete description may be compiled and executed given an appropriate C compiler and computer system on which to execute the compiled description. The use of C language structures to represent items and queues and the use of C procedures to describe the sequences of desired operations will be familiar to those skilled in the art and familiar with modern software practices. 
     
         ______________________________________/*cqueues.c*/#include &lt;stdlib.h&gt;#include &lt;stdio.h&gt;typedef long int32;// definition of an Itemtypedef struct item{struct item * Next;          // pointer to next item in queuestruct item * Previous;          // pointer to previous item in queueint32 Key;     // this item&#39;s key value} Item;// definition of a KeyCache entrytypedef struct {int32 hits;Item * Pointer;int32 Key;} kCacheEntry;// definition of a KeyCache#define cacheSize (23)kCacheEntry KeyCache[cacheSize];#define minKey (-1L)#define maxKey (0x7fffffffL)// definition of a Queuetypedef struct {Item * Head;Item * Tail;Item First;Item Last;} Queue;static void initCache(void);static void printCache(void);static void insertItem(Queue * q, Item *item);static void insertinQ(Item * Ptr, Item * item);static Item * searchQFwd(Item * Ptr, int32 Key);static Item * searchQBwd(Item * Ptr, int32 Key);static void updateCache(Item * item, int32 Index);static Item * getItem(Queue * theQ);static int32 uniform(int32 base, int32 limit);/* ----------------------- initCache -------------------------- */static void initCache(void) {int32 i;for (i = 0; i &lt; cacheSize; i++) }KeyCache[i].Key = minKey;               // set to arbitrary valueKeyCache[i].Pointer = NULL;                // set to invalid valueKeyCache[i].hits = 0;               // set to 0}/* ----------------------- printCache -------------------------- */static void printCache(void) {int32 i;printf(&#34;\nCache contents:\n&#34;);for (i = 0; i &lt; cacheSize; i++) {printf(&#34;\t%3ld::Key=%6ld, ptr = 0x%08lx, hits = %ld\n&#34;,i, KeyCache[i].Key, KeyCache[i].Pointer,KeyCache[i].hits);}}/* ----------------------- initQ -------------------------- */static void initQ(Queue * q) {/* initialise the indicated queue so that q -&gt; First is firstitem, q -&gt; Last is last item */q -&gt; Head = &amp;(q -&gt; First);q -&gt; Tail = &amp;(q -&gt; Last);q -&gt; First.Key = minKey;q -&gt; Last.Key = maxKey;q -&gt; First.Previous = NULL;q -&gt; First.Next = q -&gt; Tail;q -&gt; Last.Previous = q -&gt; Head;q -&gt; Last.Next = NULL;}/* ----------------------- computeIndex -------------------------- */static int32 computeIndex(int32 Key) {int32 index;index = Key % cacheSize;if (index &lt; 0) index = -index;return index;}/* ----------------------- insertItem -------------------------- */void insertItem(Queue * q, Item * item) {Item * Ptr;int32 Index, miss;int32 Key, cachedKey;Key = item -&gt; Key;Index = computeIndex(Key);cachedKey = KeyCache[Index].Key;Ptr = KeyCache[Index].Pointer;miss = TRUE;// first check to see if entry is validif (Ptr == NULL) {// invalid entry; need to search from startPtr = q -&gt; Head -&gt; Next;Ptr = searchQFwd(Ptr, Key);}else {// yes, valid; now decide what to do. .if (Key &gt; cachedKey) {/* a miss; search the queue forwards until insertion pointis found */Ptr = searchQFwd(Ptr, Key);}else if (Key &lt; cachedKey) {/* a miss; search the queue backwards until insertion pointis found */Ptr = searchQBwd(Ptr, Key);}else {miss = FALSE;KeyCache[Index].hits++;}}// now insert item in queueinsertinQ(Ptr, item);if (miss) updateCache(item, Index);}/* ----------------------- searchQBwd -------------------------- */static void updateCache(Item * item, int32 Index) {// updates the keyCache entry Index to indicate itemkCacheEntry * Entry;Entry = &amp;(KeyCache[Index]);             // point at Index&#39;th entryEntry -&gt; Key = item -&gt; Key;             // update its keyEntry -&gt; Pointer = item;             // update its pointerEntry -&gt; hits = 0;}/* ----------------------- insertinQ -------------------------- */static void insertinQ(Item * Ptr, Item * item) {// insert item in queue in front of item indicated by Ptritem -&gt; Previous = Ptr -&gt; Previous;                 /* item&#39;s previous isPtr&#39;s previous */item -&gt; Next = Ptr;   // item&#39;s next is PtrPtr -&gt; Previous -&gt; Next = item;                 /* what used to pointat Ptr now indicates item */Ptr -&gt; Previous = item;                 /* and Ptr&#39;s previousis item */}/* ----------------------- searchQFwd -------------------------- */static Item * searchQFwd(Item * Ptr, int32 Key) {/* searches the queue containing the item indicated by Ptrforwards (via the Next fields) */// to find correct place to insert an item with key value Key/* there is no need to check for end conditions because thequeue always contains an entry */// with key value maxKeyItem * Here;Here = Ptr;while (Ptr -&gt; Key &lt; Key) {Here = Ptr;Ptr = Ptr -&gt; Next;}return Here; // return the last item which had a smaller key}/* ----------------------- searchQBwd -------------------------- */static Item * searchQBwd(Item * Ptr, int32 Key) {/* searches the queue containing the item indicated by Ptrbackwards (via the Previous fields) */// to find correct place to insert an item with key value Key/* there is no need to check for start conditions because thequeue always contains an entry */// with key value minKeyItem * Here;Here = Ptr;while (Ptr -&gt; Key &gt; Key) {Here = Ptr;Ptr = Ptr -&gt; Previous;}return Here;      // return the last item which had a larger key}/* ----------------------- getItem -------------------------- */static Item * getItem(Queue * theQ) {// removes first real item from theQItem * item, * first;int32 Index;first = theQ -&gt; Head;item = first -&gt; Next;// heal up the queuefirst -&gt; Next = item -&gt; Next;item -&gt; Next -&gt; Previous = first;// fix up cacheIndex = computeIndex(item -&gt; Key);if (KeyCache[Index].Pointer == item) {// we&#39;re in the cache; invalidate the entryKeyCache[Index].Pointer = NULL;KeyCache[Index].Key = -1;KeyCache[Index].hits = 0;}return item;}/* ----------------------- uniform -------------------------- */static int32 uniform(int32 base, int32 limit) {/* returns a uniformly distributed rv in base. .limitthe smallest number returned is basethe largest number returned is base + limit - 1base, limit MUST both be &gt;= 0limit MUST be &gt; basethese are NOT checked*/#define rvM ((1L &lt;&lt; 31) - 1L)#define rvd (16807L)#define rvquotient (rvM/rvd)#define rvremainder (rvM%rvd)static int32 seed = 1;int32 result;result = seed;result = (rvd * (result % rvquotient)) -(rvremainder * (result / rvquotient));if (result &lt;= 0) {result += rvM;}seed = result;/* got a uniform rv in 0. .2**31, convert to uniform rv inbase. .limit */result = result % (limit - base);if (result &lt; 0) result = - result;return (base + result);}/* ----------------------- main -------------------------- */void main(void) {Queue * theQ;int32 numItems, theTime, i;printf(&#34;\nCached Queues Method\n\tstarting. .\n&#34;;// create an empty queuetheQ = malloc(sizeof(Queue));initQ(theQ);// initialise the keyCacheinitCache ();printf(&#34;\tqueue and cache initialised.\n&#34;);// insert 1000 items on the queue with keys all equal to 100theTime = 100;for (numItems = 0; numItems &lt; 1000; numItems++) {Item * item;item = malloc(sizeof(Item));item -&gt; Key = theTime;insertItem(theQ, item);}printf(&#34;\t%ld items placed on queue; starting run\n&#34;, numItems);// take items from the front an re-queue some amount laterfor (i = 0; i &lt; 100000; i++) {Item * item;int32 dt;item = getItem(theQ);theTime = item -&gt; Key;dt = 10 * (uniform(1, 10));              /* increment time in amountsof 10 between 10 and 100 */item -&gt; Key = theTime + dt;insertItem(theQ, item);if (i &lt; 10) printCache();if (i % 10000 == 0) printCache();}printCache ();printf(&#34;\tDone; completed %ld removals and insertions\n&#34;, i);}______________________________________ 
    
     From the above, it is seen how the search mechanism of the present invention improves system performance. It will be appreciated that many changes may be made to the preferred embodiment of the present invention without departing from its teachings. For example, the present invention may be used in different types of data processing systems, such as multiprocessing systems with the addition of appropriate locking mechanisms. The present invention may also be used with other data item formats, etc. 
     While in accordance with the provisions and statutes there has been illustrated and described the best form of the invention, certain changes may be made without departing from the spirit of the invention as set forth in the appended claims and that in some cases, certain features of the invention may be used to advantage without a corresponding use of other features.