Advertisement
wowonline

Untitled

Dec 12th, 2021
467
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. enum
  5. {
  6.     DECIMAL_BASE = 10,
  7.     INIT_VALUE = -1,
  8.     WRITE = 'W',
  9.     READ = 'R'
  10. };
  11.  
  12. int
  13. main (int argc, char *argv[])
  14. {
  15.     int cache_size = strtol(argv[2], NULL, DECIMAL_BASE);
  16.     int block_size = strtol(argv[3], NULL, DECIMAL_BASE);
  17.     int miss = 0;
  18.  
  19.     int block_amount = cache_size / block_size;
  20.     int *block_array = malloc(block_amount * sizeof(block_amount));
  21.     if (block_array == NULL) {
  22.         return 1;
  23.     }
  24.  
  25.     for (int i = 0; i < block_amount; ++i) {
  26.         block_array[i] = INIT_VALUE;
  27.     }
  28.  
  29.     char rdwr, datainstruction;
  30.     int addr, size, value;
  31.     while (scanf("%c%c%x%d%d\n", &rdwr,
  32.            &datainstruction, &addr, &size, &value) == 1) {
  33.         int block_index = addr / block_size;
  34.         if (rdwr == READ) {
  35.             if ((block_index != block_array[block_index % block_amount])
  36.                 && (block_array[block_index % block_amount] != -1)) {
  37.                 miss++;
  38.             }
  39.             block_array[block_index % block_amount] = block_index;
  40.         } else if (rdwr == WRITE) {
  41.             block_array[block_index % block_amount] = block_index;
  42.         }
  43.     }
  44.     printf("%d", miss);
  45.     fflush(stdout);
  46.     return 0;
  47. }
Advertisement
RAW Paste Data Copied
Advertisement