Advertisement
Sri27119

fit

Nov 25th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void firstFit(int blockSize[], int m, int processSize[], int n)
  4. {
  5.     int allocation[n];
  6.     for (int i = 0; i < n; i++)
  7.         allocation[i] = -1;
  8.  
  9.     for (int i = 0; i < n; i++)
  10.     {
  11.         for (int j = 0; j < m; j++)
  12.         {
  13.             if (blockSize[j] >= processSize[i])
  14.             {
  15.                 allocation[i] = j;
  16.                 blockSize[j] -= processSize[i];
  17.                 break;
  18.             }
  19.         }
  20.     }
  21.  
  22.     printf("\nFirst Fit Allocation:\nProcess No.\tProcess Size\tBlock No.\n");
  23.     for (int i = 0; i < n; i++)
  24.     {
  25.         printf("%d\t\t%d\t\t", i + 1, processSize[i]);
  26.         if (allocation[i] != -1)
  27.             printf("%d\n", allocation[i] + 1);
  28.         else
  29.             printf("Not Allocated\n");
  30.     }
  31. }
  32.  
  33. void bestFit(int blockSize[], int m, int processSize[], int n)
  34. {
  35.     int allocation[n];
  36.     for (int i = 0; i < n; i++)
  37.         allocation[i] = -1;
  38.  
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         int bestIdx = -1;
  42.         for (int j = 0; j < m; j++)
  43.         {
  44.             if (blockSize[j] >= processSize[i])
  45.             {
  46.                 if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx])
  47.                     bestIdx = j;
  48.             }
  49.         }
  50.  
  51.         if (bestIdx != -1)
  52.         {
  53.             allocation[i] = bestIdx;
  54.             blockSize[bestIdx] -= processSize[i];
  55.         }
  56.     }
  57.  
  58.     printf("\nBest Fit Allocation:\nProcess No.\tProcess Size\tBlock No.\n");
  59.     for (int i = 0; i < n; i++)
  60.     {
  61.         printf("%d\t\t%d\t\t", i + 1, processSize[i]);
  62.         if (allocation[i] != -1)
  63.             printf("%d\n", allocation[i] + 1);
  64.         else
  65.             printf("Not Allocated\n");
  66.     }
  67. }
  68.  
  69. void worstFit(int blockSize[], int m, int processSize[], int n)
  70. {
  71.     int allocation[n];
  72.     for (int i = 0; i < n; i++)
  73.         allocation[i] = -1;
  74.  
  75.     for (int i = 0; i < n; i++)
  76.     {
  77.         int worstIdx = -1;
  78.         for (int j = 0; j < m; j++)
  79.         {
  80.             if (blockSize[j] >= processSize[i])
  81.             {
  82.                 if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx])
  83.                     worstIdx = j;
  84.             }
  85.         }
  86.  
  87.         if (worstIdx != -1)
  88.         {
  89.             allocation[i] = worstIdx;
  90.             blockSize[worstIdx] -= processSize[i];
  91.         }
  92.     }
  93.  
  94.     printf("\nWorst Fit Allocation:\nProcess No.\tProcess Size\tBlock No.\n");
  95.     for (int i = 0; i < n; i++)
  96.     {
  97.         printf("%d\t\t%d\t\t", i + 1, processSize[i]);
  98.         if (allocation[i] != -1)
  99.             printf("%d\n", allocation[i] + 1);
  100.         else
  101.             printf("Not Allocated\n");
  102.     }
  103. }
  104.  
  105. int main()
  106. {
  107.     int m, n;
  108.  
  109.     printf("Enter the number of blocks: ");
  110.     scanf("%d", &m);
  111.     int blockSize[m];
  112.     printf("Enter the sizes of the blocks:\n");
  113.     for (int i = 0; i < m; i++)
  114.         scanf("%d", &blockSize[i]);
  115.  
  116.     printf("Enter the number of processes: ");
  117.     scanf("%d", &n);
  118.     int processSize[n];
  119.     printf("Enter the sizes of the processes:\n");
  120.     for (int i = 0; i < n; i++)
  121.         scanf("%d", &processSize[i]);
  122.  
  123.     int blockSize1[m], blockSize2[m], blockSize3[m];
  124.     for (int i = 0; i < m; i++)
  125.     {
  126.         blockSize1[i] = blockSize[i];
  127.         blockSize2[i] = blockSize[i];
  128.         blockSize3[i] = blockSize[i];
  129.     }
  130.  
  131.     firstFit(blockSize1, m, processSize, n);
  132.     bestFit(blockSize2, m, processSize, n);
  133.     worstFit(blockSize3, m, processSize, n);
  134.  
  135.     return 0;
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement