You are on page 1of 2

BEST FIT #include<stdio.h> #include<conio.h> void main() { int i,j,totinfragm=0,totextfragm=0,temp,p,memloc,count=0,f=0,infragm[20] ,proc[20],mem[20]; clrscr(); printf("\n Enter the no.

of memory location"); scanf(" %d",&memloc); for(i=0;i<memloc;i++) { printf("\n Enter the size of %d memory ",i); scanf(" %d",&mem[i]); } printf("\n Enter the no. of processes"); scanf(" %d",&p); for(i=0;i<p;i++) { printf("\n Enter the size of %d process",i); scanf(" %d",&proc[i]); } for(i=0;i<memloc;i++) infragm[i]=0; for(i=0;i<memloc;i++) for(j=0;j<memloc-i-1;j++) if(mem[j]>mem[j+1]) { temp=mem[j]; mem[j]=mem[j+1]; mem[j+1]=temp; } for(i=0;i<p;i++) { f=0; for(j=0;j<memloc;j++) { if((proc[i]<=mem[j])&&(infragm[j]==0)) { f=1; break; } else f=0; } if(f) { mem[j]=mem[j]-proc[i]; infragm[j]=1; count++; } } for(i=0;i<memloc;i++) { if(infragm[i]==1) totinfragm+=mem[i]; } printf("\n\n The total internal fragments are %d",totinfragm); if(count<p)

{ totextfragm=totinfragm; for(i=0;i<memloc;i++) { if(infragm[i]==0) totextfragm+=mem[i]; } } printf("\n The total external fragments are %d",totextfragm); getch(); } OUTPUT: Enter the no. of memory location 5 Enter the size of 0 memory 10 Enter the size of 1 memory 15 Enter the size of 2 memory 5 Enter the size of 3 memory 32 Enter the size of 4 memory 2 Enter the no. of processes 4 Enter the size of 0 process 2 Enter the size of 1 process 7 Enter the size of 2 process 27 Enter the size of 3 process 1 The total internal fragments are 12 The total external fragments are 0

You might also like