Sunday, June 16, 2013
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation of predetermined maximum possible amount of all resources, and then makes a check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue. Here we have 3 input matrix where
max[][] - denotes maximum resources that will be required by processes
allocate[][] - denotes currently allocated resources to processes
avail[][] - denotes resources available in the system
We will calculate need matrix and then try to allocate. I f we can allocate safely then give a suitable message.
--------------------------------------------------------------------------------------------------------------------------
max[][] - denotes maximum resources that will be required by processes
allocate[][] - denotes currently allocated resources to processes
avail[][] - denotes resources available in the system
We will calculate need matrix and then try to allocate. I f we can allocate safely then give a suitable message.
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner; public class Bankers{ private int need[][],allocate[][],max[][],avail[][],np,nr; private void input(){ Scanner sc=new Scanner(System.in); System.out.print("Enter no. of processes and resources : "); np=sc.nextInt(); //no. of process nr=sc.nextInt(); //no. of resources need=new int[np][nr]; //initializing arrays max=new int[np][nr]; allocate=new int[np][nr]; avail=new int[1][nr]; System.out.println("Enter allocation matrix -->"); for(int i=0;i<np;i++) for(int j=0;j<nr;j++) allocate[i][j]=sc.nextInt(); //allocation matrix System.out.println("Enter max matrix -->"); for(int i=0;i<np;i++) for(int j=0;j<nr;j++) max[i][j]=sc.nextInt(); //max matrix System.out.println("Enter available matrix -->"); for(int j=0;j<nr;j++) avail[0][j]=sc.nextInt(); //available matrix sc.close(); } private int[][] calc_need(){ for(int i=0;i<np;i++) for(int j=0;j<nr;j++) //calculating need matrix need[i][j]=max[i][j]-allocate[i][j]; return need; } private boolean check(int i){ //checking if all resources for ith process can be allocated for(int j=0;j<nr;j++) if(avail[0][j]<need[i][j]) return false; return true; } public void isSafe(){ input(); calc_need(); boolean done[]=new boolean[np]; int j=0; while(j<np){ //until all process allocated boolean allocated=false; for(int i=0;i<np;i++) if(!done[i] && check(i)){ //trying to allocate for(int k=0;k<nr;k++) avail[0][k]=avail[0][k]-need[i][k]+max[i][k]; System.out.println("Allocated process : "+i); allocated=done[i]=true; j++; } if(!allocated) break; //if no allocation } if(j==np) //if all processes are allocated System.out.println("\nSafely allocated"); else System.out.println("All proceess cant be allocated safely"); } public static void main(String[] args) { new Bankers().isSafe(); } }
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------
Enter no. of processes and resources : 3 4
Enter allocation matrix -->
1 2 2 1
1 0 3 3
1 2 1 0
Enter max matrix -->
3 3 2 2
1 1 3 4
1 3 5 0
Enter available matrix -->
3 1 1 2
Allocated process : 0
Allocated process : 1
Allocated process : 2
Safely allocated
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
Labels:Algorithms
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
thank you!!!
ReplyDeleteThanks alot!
ReplyDeleteThank you very much
ReplyDeletethanks :)
ReplyDeletei need code for deadlock prevention
ReplyDeleteIs it any different if you have to read the input from a text file instead of the concole??
ReplyDeletePlease help.
This comment has been removed by the author.
Deleteu can add this
Deletepublic MaTrix(String matran) throws FileNotFoundException, IOException {
Path path = Paths.get(new File("src/client/").getAbsolutePath(), matrix);
String temp, token[];
File file = path.toFile();
if (Files.exists(path)) {
BufferedReader in = new BufferedReader(new FileReader(file));
temp = in.readLine();
token = temp.split(" ");
m = Integer.parseInt(token[0]);
n = Integer.parseInt(token[1]);
maTran = new double[m][n];
for (int i = 0; i < m && (temp = in.readLine()) != null; i++) {
token = temp.split(" ");
for (int j = 0; j < token.length && j < n; j++) {
maTrix[i][j] = Double.parseDouble(token[j]);
}
}
} else {
System.out.println("File " + matrix+ " khong ton tai");
}
}
how to display sequence of processes being executed?
DeleteThis comment has been removed by the author.
DeleteI need help too. to read a file. The coding in comment have errors.
DeleteUmm, Can you please add more sample output? Preferably, one that has a deadlock or unsafe?
ReplyDeleteThank u
ReplyDeleteTHANK YOU SO MUCH
ReplyDeletehow would you implement this algorithm if you have a text file as an input like this
ReplyDelete5
3
10 5 7
0 1 0 7 5 3
2 0 0 3 2 2
3 0 2 9 0 2
2 1 1 2 2 2
0 0 2 4 3 3
Which means there are 5 processes, 3 resource types. The first resource type has 10 units, second one has 5 unites, third one has 7 units. The following line means the first process has a allocation of resource (0, 1, 0) and the max need is (7, 5, 3). Then following lines are the similar settings for process 2 unitl to process m.
TEA-80 Saurabh
ReplyDeleteThankyou sir for the readymade code
avail [0] [ k] = lịch phát sóng [0] [k] -need [i] [k] + max [i] [k];
ReplyDelete-->>> I do not understand this it please explain to me
avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
ReplyDeleteI do not understand this it please explain to me
This comment has been removed by the author.
DeleteReally a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.
ReplyDeleteJava编程代写
Sir ,If we want to calculate available matrix in program itself then how it can be done .
ReplyDeleteThis comment has been removed by the author.
DeleteSir ,If we want to calculate available matrix in program itself then how it can be done .
ReplyDeleteavail[0][k]=avail[0][k]-need[i][k]+max[i][k];
ReplyDeleteI do not understand this it please explain to me