/*
Experiment: Banker's Algorithm
Name: Chandresh Prasad
*/
import java.util.*;
class bankers_algo
{
static int max [][], alloc[][],need[][],available[];
static int n,m;
static boolean finish[];
private static void calcNeed()
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
need[i][j]=max[i][j]-alloc[i][j];
}
private static boolean isDone()
{
for(int i=0; i<n; i++)
if(finish[i]==false)
return false;
return true;
}
private static boolean check(int i)
{
int j;
for(j=0; j<m; j++)
if(available[j]<need[i][j])
return false;
return true;
}
public static void main(String ... args)
{
Scanner sc=new Scanner(System.in);
//taking inputs :
System.out.print("Enter number of processes : ");
n=sc.nextInt();
System.out.print("Enter number of Resources : ");
m=sc.nextInt();
//allocating memory for required arrays
max= new int[n][m];
alloc = new int[n][m];
need = new int[n][m];
available = new int[m];
finish = new boolean[n];
System.out.println("Enter Max matrix");
for(int i=0; i<n; i++)
for(int j=0;j<m; j++)
max[i][j]=sc.nextInt();
System.out.println("Enter Alloctaion matrix");
for(int i=0; i<n; i++)
for(int j=0;j<m; j++)
alloc[i][j]=sc.nextInt();
System.out.println("Enter Available matrix");
for(int i=0; i<m; i++)
available[i]=sc.nextInt();
//step1 : calculating need matrix
calcNeed();
//step2 : finding safe states
int i=0;
while(!isDone())
{
//condition to select process for safe state
if(!finish[i] && check(i))
{
System.out.println("safe state : " + i);
finish[i]=true;
//adding alloc with available
for(int j=0; j<m; j++)
available[j]+=alloc[i][j];
//start checking for safe state from start
i=0;
}
else
i++;
}
//printing all matrices ----
System.out.print("\nTotal resources : ");
for(i=0; i<m; i++)
System.out.print(available[i]+" ");
System.out.println("");
System.out.println("Max\tAlloc\tNeed");
for(i=0; i<n; i++)
{
for(int j=0; j<m; j++)
System.out.print(max[i][j]+" ");
System.out.print("\t");
for(int j=0; j<m; j++)
System.out.print(alloc[i][j]+" ");
System.out.print("\t");
for(int j=0; j<m; j++)
System.out.print(need[i][j]+" ");
System.out.print("\n");
}
}
}
Experiment: Banker's Algorithm
Name: Chandresh Prasad
*/
import java.util.*;
class bankers_algo
{
static int max [][], alloc[][],need[][],available[];
static int n,m;
static boolean finish[];
private static void calcNeed()
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
need[i][j]=max[i][j]-alloc[i][j];
}
private static boolean isDone()
{
for(int i=0; i<n; i++)
if(finish[i]==false)
return false;
return true;
}
private static boolean check(int i)
{
int j;
for(j=0; j<m; j++)
if(available[j]<need[i][j])
return false;
return true;
}
public static void main(String ... args)
{
Scanner sc=new Scanner(System.in);
//taking inputs :
System.out.print("Enter number of processes : ");
n=sc.nextInt();
System.out.print("Enter number of Resources : ");
m=sc.nextInt();
//allocating memory for required arrays
max= new int[n][m];
alloc = new int[n][m];
need = new int[n][m];
available = new int[m];
finish = new boolean[n];
System.out.println("Enter Max matrix");
for(int i=0; i<n; i++)
for(int j=0;j<m; j++)
max[i][j]=sc.nextInt();
System.out.println("Enter Alloctaion matrix");
for(int i=0; i<n; i++)
for(int j=0;j<m; j++)
alloc[i][j]=sc.nextInt();
System.out.println("Enter Available matrix");
for(int i=0; i<m; i++)
available[i]=sc.nextInt();
//step1 : calculating need matrix
calcNeed();
//step2 : finding safe states
int i=0;
while(!isDone())
{
//condition to select process for safe state
if(!finish[i] && check(i))
{
System.out.println("safe state : " + i);
finish[i]=true;
//adding alloc with available
for(int j=0; j<m; j++)
available[j]+=alloc[i][j];
//start checking for safe state from start
i=0;
}
else
i++;
}
//printing all matrices ----
System.out.print("\nTotal resources : ");
for(i=0; i<m; i++)
System.out.print(available[i]+" ");
System.out.println("");
System.out.println("Max\tAlloc\tNeed");
for(i=0; i<n; i++)
{
for(int j=0; j<m; j++)
System.out.print(max[i][j]+" ");
System.out.print("\t");
for(int j=0; j<m; j++)
System.out.print(alloc[i][j]+" ");
System.out.print("\t");
for(int j=0; j<m; j++)
System.out.print(need[i][j]+" ");
System.out.print("\n");
}
}
}
No comments:
Post a Comment