Sunday, 9 August 2015

Implementation of Best Fit Memory management technique

#include<stdio.h>
#include<conio.h>
#define SIZE 20
void main(){
int np,nm,i,j,p[SIZE],m[SIZE],blocknum[SIZE],allocated[SIZE],remaining[SIZE],flag,swap;
clrscr();
 printf("Enter no. of processes:");
 scanf("%d",&np);
 printf("Enter no. of Memory Blocks");
 scanf("%d",&nm);

 printf("\n");
  printf("Enter size of processes\n");

 for(i=0;i<np;i++){
  printf(" Process P%d: ",i+1);
  scanf("%d",&p[i]);
 }
 printf("\n");

  printf("Enter size of Memory blocks:\n");
 for(i=0;i<nm;i++){
  printf("Block %d: ",i+1);
  scanf("%d",&m[i]);
  blocknum[i]=i+1;
 }
 printf("\nProcess\tProcess_size \tBlock no.\tBlock size\tFragement");


    for(i=0;i<nm-1;i++)
      for(j=0;j<nm-1-i;j++) {
       if(m[j]>m[j+1])
swap=m[j];
m[j]=m[j+1];
m[j+1]=swap;

swap=blocknum[j];
blocknum[j]=blocknum[j+1];
blocknum[j+1]=swap;

      }
      for(i=0;i<np;i++){
 flag=0;
for(j=0;j<nm;j++){
if(m[j]-p[i]>=0 && allocated[j]!=1){
allocated[j]=1;
remaining[j]=m[j]-p[i];
flag=1;
}
if(flag==1)break;

}

      printf("\n%d\t\t%d\t\t",i+1,p[i]);
      if(flag==1)
      printf("%d\t\t%d\t\t%d",j+1,m[j],remaining[j]);
    else
    printf("NA\t\tNA\t\tNA");

 }


getch();

 }


Sample Output:

No comments:

Post a Comment