First-fit: Allocate the first hole that is big enough.
Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size.Produces the smallest leftover hole.
Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.
How to satisfy a request of size n from a list of free holes.
First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
import java.util.Scanner;
class Memory
{
int Nop,Noh;
int temp[];
int Psize[];
int Hsize[];
Scanner sc=new Scanner(System.in);
void inputData()
{
System.out.println("enter the total number of processes:");
Nop=sc.nextInt();
System.out.println("enter the total number of Holes");
Noh=sc.nextInt();
Psize=new int[Nop];
Hsize=new int[Noh];
System.out.println("enter the process size of each process:");
for(int i=0;i<Nop;i++)
{
Psize[i]=sc.nextInt();
}
System.out.println("enter the hole size of each hole:");
for(int i=0;i<Noh;i++)
{
Hsize[i]=sc.nextInt();
}
}
void display()
{
System.out.println("process size of each process is :");
for(int i=0;i<Nop;i++)
System.out.print(Psize[i]);
System.out.println("hole size of each hole is:");
for(int i=0;i<Noh;i++)
System.out.print(Hsize[i]);
}
void sort()
{
temp=new int[Noh];
for(int i=0;i<Noh;i++)
temp[i]=Hsize[i];
for(int i=0;i<Noh-1;i++)
{
for(int j=i+1;j<Noh;j++)
{
if(Hsize[i]>Hsize[j])
{
int temp=Hsize[i];
Hsize[i]=Hsize[j];
Hsize[j]=temp;
}
}
}
}
void FirstFit()
{
int i_freg=0,e_freg=0;
int flag[]=new int[Noh];
int j;
for(int i=0;i<Nop;i++)
{
for(j=0;j<Noh;j++)
{
if(flag[j]==0 && Hsize[j]>=Psize[i])
{
flag[j]=1;
i_freg=i_freg+Hsize[j]-Psize[i];
break;
}
}
if(j==Noh)
System.out.printf("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(int i=0;i<Noh;i++)
{
if(flag[i]==0)
e_freg=e_freg+Hsize[i];
}
System.out.format("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",i_freg);
System.out.format("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",e_freg);
}
void bestFit()
{
int i_freg=0,e_freg=0;
int flag[]=new int[Noh];
int j;
for(int i=0;i<Nop;i++)
{
for(j=0;j<Noh;j++)
{
if(flag[j]==0 && Hsize[j]>=Psize[i])
{
flag[j]=1;
i_freg=i_freg+Hsize[j]-Psize[i];
break;
}
}
if(j==Noh)
System.out.format("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(int i=0;i<Noh;i++)
{
if(flag[i]==0)
e_freg=e_freg+Hsize[i];
}
System.out.format("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",i_freg);
System.out.format("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",e_freg);
for(int i=0;i<Noh;i++)
Hsize[i]=temp[i];
}
void worstFit()
{
int i_freg=0,e_freg=0;
int flag[]=new int[Noh];
int j;
for(int i=0;i<Nop;i++)
{
for(j=Noh-1;j>=0;j--)
{
if(flag[j]==0 && Hsize[j]>=Psize[i])
{
flag[j]=1;
i_freg=i_freg+Hsize[j]-Psize[i];
break;
}
}
if(j<0)
System.out.format("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(int i=0;i<Noh;i++)
{
if(flag[i]==0)
e_freg=e_freg+Hsize[i];
}
System.out.format("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",i_freg);
System.out.format("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",e_freg);
for(int i=0;i<Noh;i++)
Hsize[i]=temp[i];
}
}
class MemoryTest
{
public static void main(String args[])
{
Memory m1=new Memory();
Scanner sc=new Scanner(System.in);
m1.inputData();
while(true)
{
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("press 1 for Best Fit:");
System.out.println("press 2 for First Fit:");
System.out.println("press 3 for Worst Fit:");
System.out.println("press 4 for Exit:");
System.out.println("Select the algorithm for memory management:");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("BEST FIT ALGORITHM:");
m1.sort();
m1.bestFit();
break;
case 2: System.out.println("FIRST FIT ALGORITHM:");
m1.FirstFit();
break;
case 3: System.out.println("WORST FIT ALGORITHM:");
m1.sort();
m1.worstFit();
break;
case 4:
System.exit(0);
default:
System.out.println("enter the right choice:");
}
}
}
}