matheus__serpa

page_simulator

Sep 21st, 2017
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct __page{
  6.     int size, pSize, pid;
  7. }page;
  8.  
  9. enum stat{unallocated, fragmentation, freeP};
  10.  
  11. int main(int argc, char **argv){
  12.     if(argc != 3){
  13.         fprintf(stderr, "Usage: %s <slices> <algorithm>\n", argv[0]);
  14.         exit(EXIT_FAILURE);
  15.     }
  16.     time_t t;
  17.     srand((unsigned) time(&t));
  18.  
  19.     int i, pSize, pid = 0, v, v_id;
  20.     int slices = atoi(argv[1]);
  21.     char fit = argv[2][0];
  22.     page *memory = (page *) malloc(slices * sizeof(page));
  23.     int stats[3] = {0, 0, 0};
  24.     stats[2] = slices;
  25.  
  26.     switch(fit){
  27.             case 'f':
  28.                 printf("First fit algorithm\n\n");
  29.             break;
  30.             case 'b':
  31.                 printf("Best fit algorithm\n\n");
  32.             break;
  33.             case 'w':
  34.                 printf("Worst fit algorithm\n\n");
  35.             break;
  36.             default:
  37.                 printf("algorithm not found!\n");
  38.             exit(EXIT_FAILURE);        
  39.     }
  40.  
  41.     for(i = 0; i < slices; i++){
  42.         memory[i].size = 100 * (rand() % 8 + 1);
  43.         memory[i].pSize = -1;
  44.         memory[i].pid = -1;
  45.         printf("page %d - size: %d\n", i, memory[i].size);
  46.     }
  47.     printf("\n");
  48.  
  49.     while(1){
  50.         v_id = -1;
  51.         v = -1;
  52.         printf("process %d size: ", pid);
  53.         scanf("%d", &pSize);
  54.         if(pSize == 0)
  55.             break;
  56.         switch(fit){
  57.             case 'f':
  58.                 for(v_id = 0; v_id < slices; v_id++)
  59.                     if(memory[v_id].pSize == -1 && memory[v_id].size >= pSize){
  60.                         break;
  61.                     }
  62.             break;
  63.  
  64.             case 'b':
  65.                 for(i = 0; i < slices; i++)
  66.                     if(memory[i].pSize == -1 && memory[i].size >= pSize){
  67.                         v_id = i;
  68.                         v = memory[i].size;
  69.                         break;
  70.                     }
  71.                 for(; i < slices; i++)
  72.                     if(memory[i].pSize == -1 && memory[i].size >= pSize && memory[i].size < v){
  73.                         v_id = i;
  74.                         v = memory[i].size;
  75.                     }
  76.             break;
  77.  
  78.             case 'w':
  79.                 for(i = 0; i < slices; i++)
  80.                     if(memory[i].pSize == -1 && memory[i].size >= pSize){
  81.                         printf("a:%d\n", i);
  82.                         v_id = i;
  83.                         v = memory[i].size;
  84.                         break;
  85.                     }
  86.                 for(; i < slices; i++)
  87.                     if(memory[i].pSize == -1 && memory[i].size >= pSize && memory[i].size > v){
  88.                         printf("b:%d\n", i);
  89.                         v_id = i;
  90.                         v = memory[i].size;
  91.                     }
  92.             break;
  93.         }
  94.         if(v_id == slices){
  95.             printf("process %d could not be allocated\n", pid);
  96.             printf("\tprocess size: %d\n\n",pSize);
  97.             stats[0]++;
  98.         }
  99.         else{
  100.             memory[v_id].pSize = pSize;
  101.             memory[v_id].pid = pid;
  102.             printf("process %d allocated in page %d\n", pid, v_id);
  103.             printf("\tprocess size: %d\n", pSize);
  104.             printf("\tpage size: %d\n", memory[v_id].size);
  105.             printf("\tinternal fragmentation: %d\n\n", memory[v_id].size - memory[v_id].pSize);
  106.             stats[1] += memory[v_id].size - memory[v_id].pSize;
  107.             stats[2]--;
  108.         }
  109.         pid++;
  110.     }
  111.  
  112.     printf("Unallocated processes: %d\n", stats[unallocated]);
  113.     printf("Total internal fragmentation: %d\n", stats[fragmentation]);
  114.     printf("free pages: %d\n", stats[freeP]);
  115.  
  116.     free(memory);
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment