Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this C code, please use comments. We are to ONLY edit the two f

ID: 3734203 • Letter: I

Question

I need help with this C code, please use comments.

We are to ONLY edit the two files given (lru_cache.h and lru_cache.c) to implement a least-recently used replacement policy with cache. All the other files stay the same. If you run the Makefile given, it will give an output implementing a cache with random replacement policy using the files 'random_cache.h' and 'random_cache.c' . Thank You!

You can either get all the code files from here or copy and paste them:https://drive.google.com/file/d/1ZRCxFktbCJezhjbFI2aH6mStsMFXH1vm/view?usp=sharing

These are the instructions given:

It contains code that models storage with a cache, as in our last homework. You should be familiar with most of the code from last week’s homework. If you type ‘make rand’ in bash, a simulator will be created that uses a cache with a random replacement policy. The code that implements a cache with a random replacement policy is in random_cache.h and random_cache.c.

Your job is to edit files lru_cache.h and lru_cache.c such that they implement a least-recently-used replacement policy. Do not modify any other files!

I will test your code by running the simulator, and also by running unit tests. When I run the simulator that uses the random replacement policy, I get:

hits: 73548; misses: 26452

When I run the simulator using my cache implementation with a least-recently-used replacement policy, I get (and you should get):

hits: 78399; misses: 21601

Files to edit:

lru_cache.c:

lru_cache.h:

The following files are for reference and should NOT be edited.

Makefile:

random_cache.c:

random_cache.h:

cache_sim.c:

Explanation / Answer

here is your file : --------->>>>>>>>>

lru_cache.c : --------->>>>>>>

#include <stdlib.h>
#include "lru_cache.h"

//
// Your implementations of cache_new, cache_lookup, cache_insert,
// and cache_clear go in this file. You must use a least-recently-used
// cache replacement policy.
//

// return a new cache object
CACHE* cache_new(int size) {
// your code here
CACHE *cache = malloc(sizeof(CACHE));
cache->size = size;
cache->addr = malloc(size * sizeof(int));
cache->data = malloc(size * sizeof(float));

// initialize cache
int i;
for (i = 0; i < cache->size; i++) {
    cache->addr[i] = -1;
    cache->data[i] = 0.0;
}

return cache;

}

// return data element i if it is cached; else return -1
float cache_lookup(CACHE *cache, int i) {
// your code here
   int j;
   int k;
   float temp;
for (j = 0; j < cache->size; j++) {
     if (cache->addr[j] == i) {
       // yes, cache hit
       temp = cache->data[j];
       for(k = j;k > 0;k--){
         cache->addr[k] = cache->addr[k-1];
         cache->data[k] = cache->data[k-1];
    }
    cache->addr[0] = i;
    cache->data[0] = temp;
       return temp;
     }
}
// cache miss
return -1.0;
}

// record in the cache that the ith data element has value x
// LRU replacement policy is used
void cache_insert(CACHE *cache, int i, float x) {
// your code here
int j;
int k;
for(j = 0;j<cache->size;j++){
   if(cache->addr[j] == -1){
   for(k = j;k>0;k--){
     cache->addr[k] = cache->addr[k-1];
         cache->data[k] = cache->data[k-1];
  }
   cache->addr[0] = i;
   cache->data[0] = x;
   return;
   }
}

for(j = cache->size-1;j > 0;j--){
  cache->addr[j] = cache->addr[j-1];
   cache->data[j] = cache->data[j-1];
}

cache->addr[0] = i;
cache->data[0] = x;
}

// clear the ith element of the cache
void cache_clear(CACHE *cache, int i) {
// your code here
   int j;
   int k;
for (j = 0; j < cache->size; j++) {
     if (cache->addr[j] == i) {
        for(k = j;k<cache->size-1;k++){
        if(cache->addr[k+1] == -1){
          break;
   }
        cache->addr[k] = cache->addr[k+1];
        cache->data[k] = cache->data[k+1];
  }
  cache->addr[k] = -1;
        break;
     }
}
}

lru_cache.h : -------->>>>>>>>

typedef struct cache {
//
// your code here
   int size;     // number of cache elements
int *addr;    // index of element
float *data; // value of element
//
}CACHE;

CACHE *cache_new(int size);
float cache_lookup(CACHE *cache, int i);
void   cache_insert(CACHE *cache, int i, float x);
void   cache_clear(CACHE *cache, int i);