Friday, May 4, 2012

23. UNEQUAL FIXED PARTITIONING IN JAVA


import java.util.*;
class Variable
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int size=0,nb,np,tfrag=0,i,temp=0,count,j,tnb,flag;
int psize[]=new int[10];
int frag[]=new int[10];
int bsize[]=new int[10];
int tempbsize[]=new int[10];
int btemp[]=new int[10];
int bno[]=new int[10];

System.out.println("Enter the no of blocks:");
nb=sc.nextInt();
System.out.println("Enter the memory size for "+nb+" Blocks:");
for(i=0;i<nb;i++)
{
bsize[i]=sc.nextInt();
size=size+bsize[i];
tempbsize[i]=bsize[i];
}
do
{
System.out.println("Enter no of processes(less than "+(nb+1)+"):");
np=sc.nextInt();
}
while(np>nb);
System.out.println("Enter memory size for "+np+" processes:");
for(i=0;i<np;i++)
psize[i]=sc.nextInt();
tnb=nb;
for(i=0;i<np;i++)
{
count=0;
flag=0;
do
{
j=0;
while(psize[i]!=(tempbsize[j]+count)&&j<tnb)
j++;
if(psize[i]==(tempbsize[j]+count))
{
frag[i]=tempbsize[j]-psize[i];
flag=1;
btemp[i]=tempbsize[j];
for(int k=j;k<tnb-1;k++)
tempbsize[k]=tempbsize[k+1];
tnb--;
}
else
count--;
}while(flag!=1);
}
for(i=0;i<np;i++)
{
j=0;
while(btemp[i]!=bsize[j]&&j<np)
j++;
bno[i]=j+1;
}
for(i=0;i<np;i++)
tfrag=tfrag+frag[i];
System.out.println("available memory="+size);
System.out.println("Available blocks="+nb);
System.out.println("Block No.\tBlock Size");
for(i=0;i<nb;i++)
System.out.println((i+1)+"\t\t"+bsize[i]);
System.out.println("No of processes in memory="+np);
System.out.println("Process No\tProcess Size\tAllocated Block\tFragmentation");
for(i=0;i<np;i++)
System.out.println("P"+(i+1)+"\t\t"+psize[i]+"\t\t"+bno[i]+"\t\t"+frag[i]);
System.out.println("Total Fragmentation="+tfrag);
}
}


SAMPLE OUTPUT:
Enter the no of blocks:
4
Enter the memory size for 4 Blocks:
7
5
3
2
Enter no of processes(less than 5):
4
Enter memory size for 4 processes:
5
2
6
1
available memory=17
Available blocks=4
Block No. Block Size
1             7
2             5
3             3
4             2
No of processes in memory=4
Process No Process Size Allocated Block Fragmentation
P1              5               2            0
P2              2               4            0
P3              6               1            1
P4              1               3            2
Total Fragmentation=3