|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#define L1_CACHE_SIZE 3 |
| 5 | +#define L2_CACHE_SIZE 5 |
| 6 | + |
| 7 | +typedef struct Cache { |
| 8 | + int *data; |
| 9 | + int size; |
| 10 | + int count; |
| 11 | +} Cache; |
| 12 | + |
| 13 | +// Initialize cache |
| 14 | +Cache *initialize_cache(int size) { |
| 15 | + Cache *cache = (Cache *)malloc(sizeof(Cache)); |
| 16 | + cache->data = (int *)malloc(size * sizeof(int)); |
| 17 | + cache->size = size; |
| 18 | + cache->count = 0; |
| 19 | + return cache; |
| 20 | +} |
| 21 | + |
| 22 | +// Check if a value exists in cache and return its position |
| 23 | +int find_in_cache(Cache *cache, int value) { |
| 24 | + for (int i = 0; i < cache->count; i++) { |
| 25 | + if (cache->data[i] == value) { |
| 26 | + return i; |
| 27 | + } |
| 28 | + } |
| 29 | + return -1; |
| 30 | +} |
| 31 | + |
| 32 | +// Add value to cache with FIFO replacement |
| 33 | +void add_to_cache(Cache *cache, int value) { |
| 34 | + if (cache->count < cache->size) { |
| 35 | + cache->data[cache->count++] = value; |
| 36 | + } else { |
| 37 | + // Shift data and add new value at the end |
| 38 | + for (int i = 1; i < cache->size; i++) { |
| 39 | + cache->data[i - 1] = cache->data[i]; |
| 40 | + } |
| 41 | + cache->data[cache->size - 1] = value; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Multi-tiered caching function |
| 46 | +void multi_tiered_cache(Cache *L1, Cache *L2, int value) { |
| 47 | + int pos_in_L1 = find_in_cache(L1, value); |
| 48 | + int pos_in_L2 = find_in_cache(L2, value); |
| 49 | + |
| 50 | + if (pos_in_L1 != -1) { |
| 51 | + printf("Value %d found in L1 cache.\n", value); |
| 52 | + } else if (pos_in_L2 != -1) { |
| 53 | + printf("Value %d found in L2 cache. Moving to L1.\n", value); |
| 54 | + // Move from L2 to L1 cache |
| 55 | + add_to_cache(L1, value); |
| 56 | + // Remove from L2 (by shifting) |
| 57 | + for (int i = pos_in_L2; i < L2->count - 1; i++) { |
| 58 | + L2->data[i] = L2->data[i + 1]; |
| 59 | + } |
| 60 | + L2->count--; |
| 61 | + } else { |
| 62 | + printf("Value %d not found in L1 or L2. Adding to L1 and L2.\n", value); |
| 63 | + add_to_cache(L1, value); |
| 64 | + add_to_cache(L2, value); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Free allocated memory for cache |
| 69 | +void free_cache(Cache *cache) { |
| 70 | + free(cache->data); |
| 71 | + free(cache); |
| 72 | +} |
| 73 | + |
| 74 | +// Main function to test multi-tiered caching |
| 75 | +int main() { |
| 76 | + Cache *L1 = initialize_cache(L1_CACHE_SIZE); |
| 77 | + Cache *L2 = initialize_cache(L2_CACHE_SIZE); |
| 78 | + |
| 79 | + int requests[] = {10, 20, 10, 30, 40, 50, 20, 60, 70, 10}; |
| 80 | + int num_requests = sizeof(requests) / sizeof(requests[0]); |
| 81 | + |
| 82 | + for (int i = 0; i < num_requests; i++) { |
| 83 | + multi_tiered_cache(L1, L2, requests[i]); |
| 84 | + } |
| 85 | + |
| 86 | + // Free memory |
| 87 | + free_cache(L1); |
| 88 | + free_cache(L2); |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
0 commit comments