Friday, May 4, 2012

22. EQUAL FIXED PARTITIONING IN JAVA


import java.util.*;
class FIXP
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int size,bsize,nb,np,tfrag=0,i;
int psize[]=new int[10];
int frag[]=new int[10];
System.out.println("Enter the available memory:");
size=sc.nextInt();
System.out.println("Enter the memory size for fixed blocks:");
bsize=sc.nextInt();
nb=size/bsize;

System.out.println("No of available blocks are:"+nb);
do
{
System.out.println("Enter no of processes(less than "+(nb+1)+"):");
np=sc.nextInt();
}
while(np>nb);
for(i=0;i<np;i++)
{
do
{
System.out.println("Enter memory size for processes "+(i+1)+"
(<="+bsize+"):");
psize[i]=sc.nextInt();
}while(psize[i]>bsize);
}
for(i=0;i<np;i++)
frag[i]=bsize-psize[i];
for(i=0;i<np;i++)
tfrag=tfrag+frag[i];
System.out.println("Available memory size="+size);
System.out.println("Size per Block="+bsize);
System.out.println("No of blocks available="+nb);
System.out.println("No of Processes in memory="+np);
System.out.println("Process No\tProcess Size\tFragmentation");
for(i=0;i<np;i++)
{
System.out.print("P"+(i+1)+"\t\t"+psize[i]+"\t\t"+frag[i]);
System.out.println();
}
System.out.println("Total Fragmentation="+tfrag);
}
}


SAMPLE OUTPUT:
Enter the available memory:
30
Enter the memory size for fixed blocks:
6
No of available blocks are:5
Enter no of processes(less than 6):
5
Enter memory size for processes 1 (<=6):
5
Enter memory size for processes 2 (<=6):
2
Enter memory size for processes 3 (<=6):
6
Enter memory size for processes 4 (<=6):
3
Enter memory size for processes 5 (<=6):
4
Available memory size=30
Size per Block=6
No of blocks available=5
No of Processes in memory=5
Process No Process Size Fragmentation
P1               5          1
P2               2          4
P3               6          0
P4               3          3
P5               4          2
Total Fragmentation=10