fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define CACHE_ZONE_SIZE 100 // 캐시 존의 블록 수
  5.  
  6. typedef struct CacheBlock {
  7. int lba_chunk_id; // LBA에서 매핑된 chunk ID
  8. int block_id; // Chunk 내의 블록 ID
  9. } CacheBlock;
  10.  
  11. typedef struct FIFOQueue {
  12. CacheBlock blocks[CACHE_ZONE_SIZE];
  13. int front;
  14. int rear;
  15. int size;
  16. } FIFOQueue;
  17.  
  18. // 큐 초기화
  19. void init_queue(FIFOQueue *queue) {
  20. queue->front = 0;
  21. queue->rear = -1;
  22. queue->size = 0;
  23. }
  24.  
  25. // 큐가 비었는지 확인
  26. int is_empty(FIFOQueue *queue) {
  27. return queue->size == 0;
  28. }
  29.  
  30. // 큐가 가득 찼는지 확인
  31. int is_full(FIFOQueue *queue) {
  32. return queue->size == CACHE_ZONE_SIZE;
  33. }
  34.  
  35. // 큐에 블록 추가 (FIFO 방식)
  36. int enqueue(FIFOQueue *queue, CacheBlock block) {
  37. if (is_full(queue)) {
  38. printf("Cache zone is full, cannot add block (chunk: %d, block: %d)\n", block.lba_chunk_id, block.block_id);
  39. return -1; // 큐가 가득 찼을 때 에러 반환
  40. }
  41. queue->rear = (queue->rear + 1) % CACHE_ZONE_SIZE;
  42. queue->blocks[queue->rear] = block;
  43. queue->size++;
  44. return 0;
  45. }
  46.  
  47. // 큐에서 블록 제거
  48. CacheBlock dequeue(FIFOQueue *queue) {
  49. CacheBlock empty_block = {-1, -1};
  50. if (is_empty(queue)) {
  51. printf("Cache zone is empty, cannot dequeue block\n");
  52. return empty_block; // 큐가 비었을 때 반환할 빈 블록
  53. }
  54. CacheBlock block = queue->blocks[queue->front];
  55. queue->front = (queue->front + 1) % CACHE_ZONE_SIZE;
  56. queue->size--;
  57. return block;
  58. }
  59.  
  60. int main() {
  61. FIFOQueue cache_zone;
  62. init_queue(&cache_zone);
  63.  
  64. // LBA 영역에서 블록을 받아와 캐시 존에 매핑 (FIFO 방식으로 추가)
  65. for (int i = 0; i < 105; i++) {
  66. CacheBlock new_block = {i / 10, i % 10}; // 예시로 chunk ID와 block ID 설정
  67. if (enqueue(&cache_zone, new_block) == 0) {
  68. printf("Enqueued block (chunk: %d, block: %d)\n", new_block.lba_chunk_id, new_block.block_id);
  69. }
  70. }
  71.  
  72. // 캐시 존에서 블록 제거
  73. for (int i = 0; i < 5; i++) {
  74. CacheBlock removed_block = dequeue(&cache_zone);
  75. if (removed_block.lba_chunk_id != -1) {
  76. printf("Dequeued block (chunk: %d, block: %d)\n", removed_block.lba_chunk_id, removed_block.block_id);
  77. }
  78. }
  79.  
  80. return 0;
  81. }
  82.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
Enqueued block (chunk: 0, block: 0)
Enqueued block (chunk: 0, block: 1)
Enqueued block (chunk: 0, block: 2)
Enqueued block (chunk: 0, block: 3)
Enqueued block (chunk: 0, block: 4)
Enqueued block (chunk: 0, block: 5)
Enqueued block (chunk: 0, block: 6)
Enqueued block (chunk: 0, block: 7)
Enqueued block (chunk: 0, block: 8)
Enqueued block (chunk: 0, block: 9)
Enqueued block (chunk: 1, block: 0)
Enqueued block (chunk: 1, block: 1)
Enqueued block (chunk: 1, block: 2)
Enqueued block (chunk: 1, block: 3)
Enqueued block (chunk: 1, block: 4)
Enqueued block (chunk: 1, block: 5)
Enqueued block (chunk: 1, block: 6)
Enqueued block (chunk: 1, block: 7)
Enqueued block (chunk: 1, block: 8)
Enqueued block (chunk: 1, block: 9)
Enqueued block (chunk: 2, block: 0)
Enqueued block (chunk: 2, block: 1)
Enqueued block (chunk: 2, block: 2)
Enqueued block (chunk: 2, block: 3)
Enqueued block (chunk: 2, block: 4)
Enqueued block (chunk: 2, block: 5)
Enqueued block (chunk: 2, block: 6)
Enqueued block (chunk: 2, block: 7)
Enqueued block (chunk: 2, block: 8)
Enqueued block (chunk: 2, block: 9)
Enqueued block (chunk: 3, block: 0)
Enqueued block (chunk: 3, block: 1)
Enqueued block (chunk: 3, block: 2)
Enqueued block (chunk: 3, block: 3)
Enqueued block (chunk: 3, block: 4)
Enqueued block (chunk: 3, block: 5)
Enqueued block (chunk: 3, block: 6)
Enqueued block (chunk: 3, block: 7)
Enqueued block (chunk: 3, block: 8)
Enqueued block (chunk: 3, block: 9)
Enqueued block (chunk: 4, block: 0)
Enqueued block (chunk: 4, block: 1)
Enqueued block (chunk: 4, block: 2)
Enqueued block (chunk: 4, block: 3)
Enqueued block (chunk: 4, block: 4)
Enqueued block (chunk: 4, block: 5)
Enqueued block (chunk: 4, block: 6)
Enqueued block (chunk: 4, block: 7)
Enqueued block (chunk: 4, block: 8)
Enqueued block (chunk: 4, block: 9)
Enqueued block (chunk: 5, block: 0)
Enqueued block (chunk: 5, block: 1)
Enqueued block (chunk: 5, block: 2)
Enqueued block (chunk: 5, block: 3)
Enqueued block (chunk: 5, block: 4)
Enqueued block (chunk: 5, block: 5)
Enqueued block (chunk: 5, block: 6)
Enqueued block (chunk: 5, block: 7)
Enqueued block (chunk: 5, block: 8)
Enqueued block (chunk: 5, block: 9)
Enqueued block (chunk: 6, block: 0)
Enqueued block (chunk: 6, block: 1)
Enqueued block (chunk: 6, block: 2)
Enqueued block (chunk: 6, block: 3)
Enqueued block (chunk: 6, block: 4)
Enqueued block (chunk: 6, block: 5)
Enqueued block (chunk: 6, block: 6)
Enqueued block (chunk: 6, block: 7)
Enqueued block (chunk: 6, block: 8)
Enqueued block (chunk: 6, block: 9)
Enqueued block (chunk: 7, block: 0)
Enqueued block (chunk: 7, block: 1)
Enqueued block (chunk: 7, block: 2)
Enqueued block (chunk: 7, block: 3)
Enqueued block (chunk: 7, block: 4)
Enqueued block (chunk: 7, block: 5)
Enqueued block (chunk: 7, block: 6)
Enqueued block (chunk: 7, block: 7)
Enqueued block (chunk: 7, block: 8)
Enqueued block (chunk: 7, block: 9)
Enqueued block (chunk: 8, block: 0)
Enqueued block (chunk: 8, block: 1)
Enqueued block (chunk: 8, block: 2)
Enqueued block (chunk: 8, block: 3)
Enqueued block (chunk: 8, block: 4)
Enqueued block (chunk: 8, block: 5)
Enqueued block (chunk: 8, block: 6)
Enqueued block (chunk: 8, block: 7)
Enqueued block (chunk: 8, block: 8)
Enqueued block (chunk: 8, block: 9)
Enqueued block (chunk: 9, block: 0)
Enqueued block (chunk: 9, block: 1)
Enqueued block (chunk: 9, block: 2)
Enqueued block (chunk: 9, block: 3)
Enqueued block (chunk: 9, block: 4)
Enqueued block (chunk: 9, block: 5)
Enqueued block (chunk: 9, block: 6)
Enqueued block (chunk: 9, block: 7)
Enqueued block (chunk: 9, block: 8)
Enqueued block (chunk: 9, block: 9)
Cache zone is full, cannot add block (chunk: 10, block: 0)
Cache zone is full, cannot add block (chunk: 10, block: 1)
Cache zone is full, cannot add block (chunk: 10, block: 2)
Cache zone is full, cannot add block (chunk: 10, block: 3)
Cache zone is full, cannot add block (chunk: 10, block: 4)
Dequeued block (chunk: 0, block: 0)
Dequeued block (chunk: 0, block: 1)
Dequeued block (chunk: 0, block: 2)
Dequeued block (chunk: 0, block: 3)
Dequeued block (chunk: 0, block: 4)