Tuesday, April 12, 2011

Java Implementation of Bankers Algorithm

import java.io.*;
class Bankersalgorithm
{
void disp(int m,int n,int alloc[][],int max[][],int avble[],int need[][])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
// BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
int i,j;
System.out.println("\n\tALLOCATION\tMAX\tNEED\tAVAILABLE");
System.out.println("\n\t\t");
for(i=0;i<4;i++)
{
for(j=0;j<m;j++)
System.out.println(" "+res[j]);
System.out.println(" ");
}


for(i=0;i<n;i++)
{
System.out.println("\n\t\t"+i);
for(j=0;j<m;j++)
System.out.println(" "+alloc[i][j]);
System.out.println(" ");
for(j=0;j<m;j++)
System.out.println(" "+max[i][j]);
System.out.println("\t");
for(j=0;j<m;j++)
System.out.println(" "+need[i][j]);
System.out.println(" ");
if(i==0)
{
for(j=0;j<m;j++)
System.out.println(" "+avble[j]);
}
}
}
void safesq(int m,int n,int alloc[][],int avble[],int need[][])throws Exception
{
int i,j,k=0,l,flag=0,flag1=0;
int work[]=new int[5];
int work1[]=new int[5];
int fin[]=new int[5];
int safesq[]=new int[6];
for(i=0;i<m;i++)
work[i]=avble[i];
for(i=0;i<n;i++)
fin[i]=0;
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
flag1=0;
if(fin[i]==0)
{
for(j=0; j<m; j++)
{
if(need[i][j] > work[j])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(j=0;j<m;j++)
work[j]=work[j]+alloc[i][j];
fin[i]=1;
safesq[k]=i;
k++;
}
}
}
}
for(i=0;i<n;i++)
{
if(fin[i]==0)
{
System.out.println("\n\tFOR THE GIVEN REQUIREMENT THE SYSTEM IS");
System.out.println(" NOT IN A SAFE STATE.\n");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("\n\tTHE SAFE SEQUENCE IS:\t");
for(i=0;i<n;i++)
System.out.println(" "+safesq[i]);
}
}
void resreq(int m,int n,int alloc[][],int avble[],int need[][])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
int i,j,num,flag=0,flag1=0;
int alloc1[][]=new int[5][5];
int avble1[]=new int[5];
int req[]=new int[5];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
alloc1[i][j] = alloc[i][j];
for(j=0;j<m;j++)
avble1[j]=avble[j];
System.out.println("\n\tENTER THE PROCESS NO. THAT REQUIRES EXTRA RESOURCES:\t");
num=Integer.parseInt(d.readLine());
System.out.println("\n\tENTER THE INSTANCE REQUIREMENT OF PROCESS \n"+num);
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE :\t"+res[i]);
req[i]=Integer.parseInt(d.readLine());
}
for(i=0;i<m;i++)
{
if(req[i]>need[num][i])
{
flag=1;
break;
}
}
if(flag==0)
{
for(i=0;i<m;i++)
{
if(req[i] > avble1[i])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
avble1[i]=avble1[i]-req[i];
alloc1[num][i]=alloc1[num][i]+req[i];
need[num][i]=need[num][i]-req[i];
}
safesq(m,n,alloc1,avble1,need);
}
else
{
System.out.println("\n\tTHE PROCESS P%d HAS TO WAIT AS RESOURCES ");
System.out.println("ARE NOT AVAILABLE.\n");
}
}
else
System.out.println("\n\t REQUIREMENT EXCEEDS MAXIMUM CLAIM.\n");
}
}
class Result
{
public static void main(String args[])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
int i,j,ch=0,n,m;
int work1[]=new int[5];
int avble[]=new int[5];
int max[][]=new int[5][];
int alloc[][]=new int[5][5];
int need[][]=new int[5][5];
Bankersalgorithm b=new Bankersalgorithm();
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nENTER THE NUMBER OF PROCESSES : \t");
n=Integer.parseInt(d.readLine());
System.out.println("\nENTER THE NUMBER OF RESOURCES : \t");
m=Integer.parseInt(d.readLine());
System.out.println("\nENTER THE MAXIMUM INSTANCES OF EACH RESOURCE\n");
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE %c:\t"+res[i]);
avble[i]=Integer.parseInt(d.readLine());
}
System.out.println("\nENTER THE MAXIMUM DEMAND OF EACH PROCESS FOR A RESOURCE\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
max[i][j]=Integer.parseInt(d.readLine());
}
}
System.out.println("\nENTER THE MAX NO. OF INSTANCES OF A RESOURCE ALLOCATED TO");
System.out.println(" A PROCESS.\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
alloc[i][j]=Integer.parseInt(d.readLine());
}
}
for(i=0;i<m;i++)
{
work1[i]=0;
for(j=0;j<n;j++)
work1[i]+=alloc[j][i];
avble[i]=avble[i] - work1[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(ch<5)
{
System.out.println("\n\n\tMENU:\n\t1]DISPLAY DATA\n\t2]GENERATE SAFE SEQUENCE");
System.out.println("\n\t3]RESOURCE REQUEST\n\t4]EXIT\n\tENTER YOUR CHOICE:\t");
ch=Integer.parseInt(d.readLine());
switch(ch)
{
case 1:
b.disp(m,n,alloc,max,avble,need);
break;
case 2:
b.safesq(m,n,alloc,avble,need);
break;
case 3:
b.resreq(m,n,alloc,avble,need);
break;
case 4:
default:
System.out.println("\n\tINVALID CHOICE ENTERED.\n");
}
}
System.out.println("\n\t\tThank You");
//return true;
}
}

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of First-Fit, Best-Fit and Worst-Fit
3. C implementation of CPU Scheduling Algorithm Shortest Remaining Time
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin