Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
3.4 kB
2
Indexable
Never
#include <stdio.h>

void firstFit(int blockSize[],int m,int processSize[],int n)
{
   int allocated[10];

   for(int i = 0; i <n;i++)
   {
      allocated[i] = -1;
   }

   for(int i=0;i<n;i++)
   {
      for(int j = 0; j <m; j++)
      {
         if(blockSize[j] >= processSize[i])
         {
            allocated[i] = j;
            printf("%d - %d = ",blockSize[j],processSize[i]);
            blockSize[j] -= processSize[i];
            printf("%d\n",blockSize[j]);
            break;

         }
      }
   }

   printf("\nProcess\t\tdataSize\t\tallocated\n");
   for(int i = 0; i <n;i++)
   {
      printf("%d\t\t%d\t\t\t",i+1,processSize[i]);
      if(allocated[i]==-1)
      {
         printf("Not allocated\n");
      }
      else{
          printf("%d\n",allocated[i]+1);212
      }
   }
}

void worstFit(int blockSize[],int m,int processSize[],int n)
{
   int allocation[n];

   for(int i=0;i<n;i++)
   {
    allocation[i] = -1;
   }

   for(int i=0;i<n;i++)
   {
      int worstIndex=-1;
      for(int j=0;j<m;j++)
      {
         if(blockSize[j]>=processSize[i])
         {
            if(worstIndex==-1)
            {
                worstIndex=j;
            }
            else if(blockSize[worstIndex]<blockSize[j])
            {
                worstIndex=j;
            }
         }
      }
      if(worstIndex!=-1)
      {
        allocation[i]=worstIndex;
        blockSize[worstIndex] -= processSize[i];
      }
   }

   printf("\nProcess\t\tdataSize\t\tallocated\t\tfragments\n");
   for(int i = 0; i <n;i++)
   {
      printf("%d\t\t%d\t\t\t",i+1,processSize[i]);
      if(allocation[i]==-1)
      {
         printf("Not allocated\n");
      }
      else{
          printf("%d\t\t\t%d\n",allocation[i]+1,blockSize[i]);
      }
   }

}


void BestFit(int blockSize[],int m,int processSize[],int n)
{
    int allocation[n];

    for(int i=0;i<n;i++)// indicate initially no process is allocated
    {
      allocation[i] = -1;
    }


    for(int i=0;i<n;i++)
    {
       int bestIndex = -1;
       for(int j=0;j<m;j++)
       {
          if(blockSize[j]>=processSize[i])
          {
             if(bestIndex==-1)
             {
               bestIndex = j;
             }
             else if(blockSize[bestIndex]>blockSize[j])
             {
                 bestIndex = j;
             }
          }
       }

       if(bestIndex !=-1)// was able to find a block
       {
          allocation[i] = bestIndex;
          blockSize[bestIndex] -= processSize[i];
       }
    }

    printf("\nProcess\t\tdataSize\t\tallocated\t\tFragments\n");
   for(int i = 0; i <n;i++)
   {
      printf("%d\t\t%d\t\t\t",i+1,processSize[i]);
      if(allocation[i]==-1)
      {
         printf("Not allocated\n");
      }
      else
      {
          printf("%d\t\t\t%d\n",allocation[i]+1,blockSize[i]);
      }
   }

}




int main()
{
    int m,n,blockSize[10],ProcessSize[10];
    printf("Enter the number of Blocks: ");
    scanf("%d",&m);
    printf("Enter the Block sizes of each: ");
    for(int i=0; i<m; i++)
    {
      scanf("%d",&blockSize[i]);
    }
    printf("Enter the number of processes: ");
    scanf("%d",&n);
    printf("Enter the Process size of each: ");
    for(int i=0; i<n; i++)
    {
      scanf("%d",&ProcessSize[i]);
    }
    firstFit(blockSize,m,ProcessSize,n);
    printf("\n");
    worstFit(blockSize,m,ProcessSize,n);
    printf("\n");
    BestFit(blockSize,m,ProcessSize,n);


}

Leave a Comment