Friday, May 4, 2012

24. DYNAMIC PARTITIONING IN JAVA


import java.util.*;
class Dynamic
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int mem,np,i,j,mp;
System.out.println("Enter the available memory:");
mem=sc.nextInt();
System.out.println("Enter no of processes:");
np=sc.nextInt();
i=0;
do

{
System.out.println("Available memory="+mem);
System.out.println("Enter the memory required for process P"+(i+1)+":");
mp=sc.nextInt();
if(mp>mem)
{
System.out.println("Not enough memory...The program will Exit
now.");
i=np;
}
else
{
System.out.println("Allocated memory to Process P"+(i+1)+"="+mp);
mem=mem-mp;
i++;
}
}while(i<np);
}
}


SAMPLE OUTPUT:
Enter the available memory:
20
Enter no of processes:
5
Available memory=20
Enter the memory required for process P1:
4
Allocated memory to Process P1=4
Available memory=16
Enter the memory required for process P2:
5
Allocated memory to Process P2=5
Available memory=11
Enter the memory required for process P3:
6
Allocated memory to Process P3=6
Available memory=5
Enter the memory required for process P4:
4
Allocated memory to Process P4=4
Available memory=1
Enter the memory required for process P5:
3
Not enough memory...The program will Exit now.