Facebook
From Mk, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 173
  1. #include<stdio.h>
  2. int blocks,process,bl[30],pr[30];
  3. void first()
  4. {
  5.  printf("nntFIRST FIT ALLOCATION n");
  6.  int block[30],proc[30],flag_block[30]={0},flag_proc[30]={0};
  7.  for(int i=0;i<blocks;i++)
  8.  block[i]=bl[i];
  9.  for(int i=0;i<process;i++)
  10.  proc[i]=pr[i];
  11.  for(int i=0;i<process;i++)
  12.  {
  13.  for(int j=0;j<blocks;j++)
  14.  {
  15.  if(block[j]>=proc[i] && flag_block[j]==0)
  16.  {
  17.  printf("n Process P%d has been allocated to Block B%d of Size -> %d",i+1,j+1,block[j]);
  18.  flag_block[j]=1;
  19.  flag_proc[i]=1;
  20.  break;
  21.  }
  22.  }
  23.  if(flag_proc[i]==0)
  24.  printf("n Process P%d cannot be allocated..!",i+1);
  25.  }}
  26. void best()
  27. {
  28.  printf("nntBEST FIT ALLOCATION n");
  29.  int block[30],proc[30],flag_block[30]={0},flag_proc[30]={0},temp,k;
  30.  for(int i=0;i<blocks;i++)
  31.  block[i]=bl[i];
  32.  for(int i=0;i<process;i++)
  33.  proc[i]=pr[i];
  34.  for(int i=1;i<blocks;i++)
  35.  for(int j=0;j<blocks-i;j++)
  36.  if(block[j]>block[j+1])
  37.  {
  38.  temp=block[j];
  39.  block[j]=block[j+1];
  40.  block[j+1]=temp;
  41.  }
  42.  for(int i=0;i<process;i++)
  43.  {
  44.  for(int j=0;j<blocks;j++)
  45.  {
  46.  if(block[j]>=proc[i] && flag_block[j]==0)
  47.  {
  48.  for(k=0;k<blocks;k++)
  49.  {
  50.  if(bl[k]==block[j])
  51.  break;
  52.  }
  53.  printf("n Process P%d has been allocated to Block B%d of Size -> %d",i+1,k+1,block[j]);
  54.  flag_block[j]=1;
  55.  flag_proc[i]=1;
  56.  break;
  57.  }
  58.  }
  59.  if(flag_proc[i]==0)
  60.  printf("n Process P%d cannot be allocated..!",i+1);
  61.  }
  62. }
  63. void worst()
  64. {
  65.  printf("nntWORST FIT ALLOCATION n");
  66.  int block[30],proc[30],flag_block[30]={0},flag_proc[30]={0},temp,k;
  67.  for(int i=0;i<blocks;i++)
  68.  block[i]=bl[i];
  69.  for(int i=0;i<process;i++)
  70.  proc[i]=pr[i];
  71.  for(int i=1;i<blocks;i++)
  72.  for(int j=0;j<blocks-i;j++)
  73.  if(block[j]<block[j+1])
  74.  {
  75.  temp=block[j];
  76.  block[j]=block[j+1];
  77.  block[j+1]=temp;
  78.  }
  79.  for(int i=0;i<process;i++)
  80.  {
  81.  for(int j=0;j<blocks;j++)
  82.  {
  83.  if(block[j]>=proc[i] && flag_block[j]==0)
  84.  {
  85.  for(k=0;k<blocks;k++)
  86.  {
  87.  if(bl[k]==block[j])
  88.  break;
  89.  }
  90.  printf("n Process P%d has been allocated to Block B%d of Size -> %d",i+1,k+1,block[j]);
  91.  flag_block[j]=1;
  92.  flag_proc[i]=1;
  93.  break;
  94.  }
  95.  }
  96.  if(flag_proc[i]==0)
  97.  printf("n Process P%d cannot be allocated..!",i+1);
  98.  }
  99. }
  100. void main()
  101. {
  102.  printf("n Enter the Number of Memory Blocks Available : ");
  103.  scanf("%d",&blocks;);
  104.  printf("n Enter the size of each Block : n");
  105.  for(int i=0;i<blocks;i++)
  106.  {
  107.  printf("nBlock B%d -> ",i+1);
  108.  scanf("%d",&bl;[i]);
  109.  }
  110.  printf("n Enter the Number of Requesting Processes : ");
  111.  scanf("%d",&process;);
  112.  printf("n Enter the size of each Block : n");
  113.  for(int i=0;i<process;i++)
  114.  {
  115.  printf("nProcess P%d -> ",i+1);
  116.  scanf("%d",≺[i]);
  117.  }
  118.  first();
  119.  best();
  120.  worst();
  121. }
  122.