Facebook
From lab, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 172
  1. #include<stdio.h>
  2.  
  3. #define max 25
  4.  
  5. void main() {
  6.     int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0, lowest = 10000;
  7.     static int bf[max], ff[max], bf2[max], ff2[max], bf3[max], ff3[max];
  8.  
  9.  
  10.     printf("\nEnter the number of blocks:");
  11.     scanf("%d", &nb;);
  12.     printf("Enter the number of files:");
  13.     scanf("%d", &nf;);
  14.  
  15.     printf("\nEnter the size of the blocks:-\n");
  16.     for (i = 1; i <= nb; i++) {
  17.         printf("Block %d:", i);
  18.         scanf("%d", &b[i]);
  19.     }
  20.     printf("Enter the size of the files :-\n");
  21.     for (i = 1; i <= nf; i++) {
  22.         printf("File %d:", i);
  23.         scanf("%d", &f[i]);
  24.     }
  25.  
  26.     // First Fit
  27.     printf("\n\tMemory Management Scheme - First Fit");
  28.     for (i = 1; i <= nf; i++) {
  29.         for (j = 1; j <= nb; j++) {
  30.             if (bf[j] != 1) {
  31.                 temp = b[j] - f[i];
  32.                 if (temp >= 0) {
  33.                     ff[i] = j;
  34.                     break;
  35.                 }
  36.             }
  37.         }
  38.         frag[i] = temp;
  39.         bf[ff[i]] = 1;
  40.     }
  41.     printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
  42.     for (i = 1; i <= nf; i++)
  43.         printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]], frag[i]);
  44.  
  45.     // Best Fit
  46.     printf("\n\n\tMemory Management Scheme - Best Fit");
  47.     for (i = 1; i <= nf; i++) {
  48.         for (j = 1; j <= nb; j++) {
  49.             if (bf2[j] != 1) {
  50.                 temp = b[j] - f[i];
  51.                 if (temp >= 0)
  52.                     if (lowest > temp) {
  53.                         ff2[i] = j;
  54.                         lowest = temp;
  55.                     }
  56.             }
  57.         }
  58.         frag[i] = lowest;
  59.         bf2[ff2[i]] = 1;
  60.         lowest = 10000;
  61.     }
  62.     printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
  63.     for (i = 1; i <= nf && ff2[i] != 0; i++)
  64.         printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff2[i], b[ff2[i]], frag[i]);
  65.  
  66.     // Worst Fit
  67.     printf("\n\n\tMemory Management Scheme - Worst Fit");
  68.     for (i = 1; i <= nf; i++) {
  69.         for (j = 1; j <= nb; j++) {
  70.             if (bf3[j] != 1) {
  71.                 temp = b[j] - f[i];
  72.                 if (temp >= 0)
  73.                     if (highest < temp) {
  74.                         ff3[i] = j;
  75.                         highest = temp;
  76.                     }
  77.             }
  78.         }
  79.         frag[i] = highest;
  80.         bf3[ff3[i]] = 1;
  81.         highest = 0;
  82.     }
  83.     printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
  84.     for (i = 1; i <= nf; i++)
  85.         printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff3[i], b[ff3[i]], frag[i]);
  86.  
  87.  
  88. }
  89.