Advertisement
EBobkunov

w7 ex 1

Oct 24th, 2023 (edited)
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define MEMORY_SIZE 10000000
  7.  
  8. unsigned int memory[MEMORY_SIZE];
  9.  
  10. void initialize_memory() {
  11.     memset(memory, 0, sizeof(memory));
  12. }
  13.  
  14. int allocate_first_fit(unsigned int adrs, int size) {
  15.     for (int i = 0; i < MEMORY_SIZE; i++) {
  16.         int contiguous = 0;
  17.         while (i < MEMORY_SIZE && memory[i] == 0) {
  18.             contiguous++;
  19.             if (contiguous == size) {
  20.                 for (int j = i - size + 1; j <= i; j++) {
  21.                     memory[j] = adrs;
  22.                 }
  23.                 return i - size + 1;
  24.             }
  25.             i++;
  26.         }
  27.     }
  28.     return -1;
  29. }
  30.  
  31.  
  32. int allocate_best_fit(unsigned int adrs, int size) {
  33.     int best_fit_start = -1;
  34.     int best_fit_size = MEMORY_SIZE + 1;
  35.  
  36.     int current_start = -1;
  37.     int current_size = 0;
  38.  
  39.     for (int i = 0; i < MEMORY_SIZE; i++) {
  40.         if (memory[i] == 0) {
  41.             if (current_start == -1) {
  42.                 current_start = i;
  43.             }
  44.             current_size++;
  45.         } else {
  46.             if (current_size >= size && current_size < best_fit_size) {
  47.                 best_fit_start = current_start;
  48.                 best_fit_size = current_size;
  49.             }
  50.             current_start = -1;
  51.             current_size = 0;
  52.         }
  53.     }
  54.  
  55.     if (current_size >= size && current_size < best_fit_size) {
  56.         best_fit_start = current_start;
  57.     }
  58.  
  59.     if (best_fit_start != -1) {
  60.         for (int i = best_fit_start; i < best_fit_start + size; i++) {
  61.             memory[i] = adrs;
  62.         }
  63.         return best_fit_start;
  64.     }
  65.    
  66.     return -1;
  67. }
  68.  
  69.  
  70. int allocate_worst_fit(unsigned int adrs, int size) {
  71.     int worst_fit_start = -1;
  72.     int worst_fit_size = -1;
  73.  
  74.     int current_start = -1;
  75.     int current_size = 0;
  76.  
  77.     for (int i = 0; i < MEMORY_SIZE; i++) {
  78.         if (memory[i] == 0) {
  79.             if (current_start == -1) {
  80.                 current_start = i;
  81.             }
  82.             current_size++;
  83.         } else {
  84.             if (current_size > worst_fit_size) {
  85.                 worst_fit_start = current_start;
  86.                 worst_fit_size = current_size;
  87.             }
  88.             current_start = -1;
  89.             current_size = 0;
  90.         }
  91.     }
  92.  
  93.     if (current_size > worst_fit_size) {
  94.         worst_fit_start = current_start;
  95.     }
  96.  
  97.     if (worst_fit_start != -1) {
  98.         for (int i = worst_fit_start; i < worst_fit_start + size; i++) {
  99.             memory[i] = adrs;
  100.         }
  101.         return worst_fit_start;
  102.     }
  103.  
  104.     return -1;
  105. }
  106.  
  107.  
  108. void clear(unsigned int adrs) {
  109.     for (int i = 0; i < MEMORY_SIZE; i++) {
  110.         if (memory[i] == adrs) {
  111.             memory[i] = 0;
  112.         }
  113.     }
  114. }
  115.  
  116. int main() {
  117.     initialize_memory();
  118.  
  119.     FILE* file = fopen("queries.txt", "r");
  120.     if (file == NULL) {
  121.         perror("Error opening file");
  122.         return 1;
  123.     }
  124.  
  125.     clock_t start_time = clock();
  126.  
  127.     char line[100];
  128.     while (fgets(line, sizeof(line), file)) {
  129.         if (strcmp(line, "end\n") == 0) {
  130.             break;
  131.         }
  132.  
  133.         unsigned int adrs;
  134.         int size;
  135.  
  136.         if (sscanf(line, "allocate %u %d", &adrs, &size) == 2) {
  137.             int result = allocate_first_fit(adrs, size);
  138.             if (result != -1) {
  139.                 printf("Allocated %d cells starting at address %u\n", size, result);
  140.             } else {
  141.                 printf("Allocation failed for %d cells\n", size);
  142.             }
  143.         } else if (sscanf(line, "clear %u", &adrs) == 1) {
  144.             clear(adrs);
  145.         }
  146.     }
  147.  
  148.     clock_t end_time = clock();
  149.  
  150.  
  151.     double total_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  152.     int total_queries = 0;
  153.  
  154.     rewind(file);
  155.     while (fgets(line, sizeof(line), file)) {
  156.         if (strcmp(line, "end\n") == 0) {
  157.             break;
  158.         }
  159.         total_queries++;
  160.     }
  161.  
  162.     double throughput = total_queries / total_time;
  163.  
  164.     printf("Throughput: %lf\n", throughput);
  165.  
  166.     fclose(file);
  167.  
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement