Question

python rhe LRU is a common caching strategy which defines policy to evict elements from a cache to make room for the new elements wh

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here the idea is to implement LRU through queue structure. Queue in FIFO. The least recently used block will appear at the front of queue

code

cachebl=[]
q=[]
def cache(block):
if((block in cachebl)==0):#This will execute when the requested block not in cache
if(len(q)<4): #This will exeucted when there empty spot in cache
q.append(block)
cachebl.append(block)
print("cache status:",cachebl," queue status:",q)
else:# This will execute when there is a block eviction needed
evict=q[0]
for i in range(0,3):#This loop is needed to shift elements to front
q[i]=q[i+1]
idx=cachebl.index(evict)#evicting element from cache
cachebl[idx]=block#inserting new block at evicted spot
q[3]=block#new block inserted at the end of queue
print("cache status:",cachebl," queue status:",q)
else:#This will execute when the requested block is already in cache
idx=q.index(block)
si=len(q)
for i in range(idx,si-1):#This will move all the blocks coming after the requested block to frot by on step
q[i]=q[i+1]
q[si-1]=block#inserting the requested block at end
print("cache status:",cachebl," queue status:",q)
  
#This part is for taking input from user. Press 'q' for exiting
c='c'
while(c!='q'):
c=input("Enter block number or press q to exit")
if(c!='q'):
cache((int)(c))
  
  
  


cachebl-[] def cache (block) if ((block in cacheb1)==0 ):#This will execute when the requested block not in cache if (len (q)

Output

RESTART: C:/Users/SIDHARTH/Documents/Python Scripts/queue Enter block number or press q to exitl cache status: [ ueue status:

Add a comment
Know the answer?
Add Answer to:
Rhe LRU is a common caching strategy which defines policy to evict elements from a cache to make ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1 Overview The goal of this assignment is to help you understand caches better. You are...

    1 Overview The goal of this assignment is to help you understand caches better. You are required to write a cache simulator using the C programming language. The programs have to run on iLab machines. We are providing real program memory traces as input to your cache simulator. The format and structure of the memory traces are described below. We will not give you improperly formatted files. You can assume all your input files will be in proper format as...

  • Description An array in C++ is a collection of items stored at contiguous memory locations and...

    Description An array in C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar type of elements as in the data type must be the same for all elements. One advantage of arrays is easy data manipulation and accessibility of elements stored in consecutive locations. The Problem: One teaching assistant of a computer science department in Engineering University got a simple question...

  • An array of 100 elements is to be sorted using the bubble sort in the modules (the one that tests...

    An array of 100 elements is to be sorted using the bubble sort in the modules (the one that tests the return value of floatLargestToTop0 to see if it can return "early".) Check all the true statements about the sort algorithm, i.e., the sort method and its support methods. (Check all that apply.) It will sometimes return (completely sorted) after only 99 data comparisons. It will always require at least one swap. It will always require at least 99 comparisons...

  • In this assignment, you will use a Hashtable to determine the common elements in all the...

    In this assignment, you will use a Hashtable to determine the common elements in all the lists. If an element appears more than once in one or more lists, the algorithm should capture the instances the element is present in all the lists. You are given a code that implements a Hashtable as an array of linked lists. You are also given a main function that will create an array of Lists using the input variable values that you enter....

  • Deletion of List Elements The del statement removes an element or slice from a list by position....

    Deletion of List Elements The del statement removes an element or slice from a list by position. The list method list.remove(item) removes an element from a list by it value (deletes the first occurance of item) For example: a = ['one', 'two', 'three'] del a[1] for s in a: print(s) b = ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b'] del b[1:5] print(b) b.remove('a') print(b) # Output: ''' one three ['a', 'f', 'a', 'b'] ['f', 'a', 'b'] ''' Your textbook...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • HTML / CSS Content Requirements Create a home page with the following elements and settings: Set...

    HTML / CSS Content Requirements Create a home page with the following elements and settings: Set the background, font colors, and “theme” to match those used in lab assignment #1 using an external css file; inline styling should be used only where necessary to override styling from the external css H1 element, centered at the top of the page, with “Thank you for choosing Your Company Name. . . “ text* Div or other container with at least 5 sentences...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT