Coursework (bankers algorithm)

BankImpl.java Bank.java

Trying to fix the error in the BankImpl code. I have tried most that I know how to do at this point as you'll see in the commented out areas. Please help!
Who is Participating?

Commented:
you've gor a few problems which I've fixed up in the attached.
main ones were you needed to wait until the respource was available, and have something notify when it was available
also the return type needed to be fixed up

let me know if you have any questions

``````/**
* A solution to the Bankers Algorithm
*
* @author Gagne, Galvin, Silberschatz
* Operating System Concepts with Java - Eighth Edition
* Copyright John Wiley & Sons - 2010.
*/

import java.io.*;
import java.util.*;

public class BankImpl implements Bank {

private int n;

// the number of threads in the system

private int m;
// the number of resources private
int[] available;
// the amount available of eah resource

private int[][] maximum; // the maximum demand of each thread
private int[][] allocation; // the amount currently allocated to each thread
private int[][] need; // the remaining needs of each thread

/* Create a new bank with resources. */

public BankImpl(int[] resources) {

// m is the number of resources
m = resources.length;
n = Customer.COUNT;

// initialize the resources array

available = new int[m];
System.arraycopy(resources, 0, available, 0, m);

// create the array for storing the maximum demand by each thread

maximum = new int[Customer.COUNT][];
allocation = new int[Customer.COUNT][];
need = new int[Customer.COUNT][];
}

/*
* This method is invoked by a thread when it enters the system. It records
* * its maximum demand with the bank.
*/

}

/* Outputs the state for each thread */

public void getState() {

System.out.print("Available = [");
for (int i = 0; i < m - 1; i++)
System.out.print(available[i] + " ");
System.out.println(available[m - 1] + "]");
System.out.print("\nAllocation = ");

for (int i = 0; i < n; i++) {
System.out.print("[");

for (int j = 0; j < m - 1; j++)
System.out.print(allocation[i][j] + " ");
System.out.print(allocation[i][m - 1] + "]");

}

System.out.print("\nMax = ");

for (int i = 0; i < n; i++) {
System.out.print("[");

for (int j = 0; j < m - 1; j++)
System.out.print(maximum[i][j] + " ");
System.out.print(maximum[i][m - 1] + "]");
}

System.out.print("\nNeed = ");

for (int i = 0; i < n; i++) {
System.out.print("[");

for (int j = 0; j < m - 1; j++)
System.out.print(need[i][j] + " ");
System.out.print(need[i][m - 1] + "]");

}
System.out.println();

}

/*
* Determines whether granting a request results in leaving * the system in
* a safe state or not. * * @return true - the system is in a safe state. *
* @return false - the system is NOT in a safe state.
*/

private boolean isSafeState(int threadNum, int[] request) {

System.out.print("\n Customer # " + threadNum + " requesting ");

for (int i = 0; i < m; i++)
System.out.print(request[i] + " ");
System.out.print("Available = ");
for (int i = 0; i < m; i++)
System.out.print(available[i] + " ");

// first check if there are sufficient resources available

for (int i = 0; i < m; i++)

if (request[i] > available[i]) {
System.err.println("INSUFFICIENT RESOURCES");
return false;
}

// ok, they're are. Now let's see if we can find an ordering of threads
// to finish

boolean[] canFinish = new boolean[n];
for (int i = 0; i < n; i++)
canFinish[i] = false;

// copy the available matrix to avail

int[] avail = new int[m];
System.arraycopy(available, 0, avail, 0, available.length);

// Now decrement avail by the request. // Temporarily adjust the value
// of need for this thread.

for (int i = 0; i < m; i++) {

avail[i] -= request[i];

}

/**
* * Now try to find an ordering of threads so that * each thread can
* finish.
*/

for (int i = 0; i < n; i++) {

// first find a thread that can finish

for (int j = 0; j < n; j++) {

if (!canFinish[j]) {
boolean temp = true;

for (int k = 0; k < m; k++) {

if (need[j][k] > avail[k])
temp = false;
}
if (temp)

{
// if this thread can finish

canFinish[j] = true;

for (int x = 0; x < m; x++)
avail[x] += allocation[j][x];
}
}
}
}

// restore the value of need and allocation for this thread

for (int i = 0; i < m; i++) {

}

// now go through the boolean array and see if all threads could
// complete

boolean returnValue = true;

for (int i = 0; i < n; i++)

if (!canFinish[i]) {
returnValue = false;
break;
}
return returnValue;
}

/**
* * Make a request for resources. This is a blocking method that returns *
* only when the request can safely be satisfied. * * @return true - the
* request is granted. * @return false - the request is not granted.
*/

public synchronized boolean requestResources(int threadNum, int[] request)  {

try {
wait();
} catch (InterruptedException ex) {
System.out.println("Interrupted");
return false;
}
}

for (int i = 0; i < m; i++) {

available[i] -= request[i];

}

System.out.println("Customer # " + threadNum + " using resources.");
System.out.print("Available = ");

for (int i = 0; i < m; i++)
System.out.print(available[i] + " ");

return true;

}

/**
* * Release resources * * @param int[] release - the resources to be
* released.
*/

public synchronized void releaseResources(int threadNum, int[] release) {

System.out.print("\n Customer # " + threadNum + " releasing ");

for (int i = 0; i < m; i++)

System.out.print(release[i] + " ");

for (int i = 0; i < m; i++) {

available[i] += release[i];

}

System.out.print("Available = ");

for (int i = 0; i < m; i++)

System.out.print(available[i] + " ");
System.out.print("Allocated = [");

for (int i = 0; i < m; i++)
System.out.print("]"); // there may be some threads that can now proceed
notifyAll();

}

}
``````
0

Commented:
Get rid of your duplicate releaseResources method. You can't have two methods with the same name and same signature
0

Author Commented:
I understand It at least needs to change but I can't figure out HOW to implement that
0

Commented:
You need to delete one of the methods - did you do that?
0

Author Commented:
Probably but no beans. Can you tell me which one you suggest?
0

Commented:
There's no 'probably' about it - if you didn't then it wouldn't have compiled ;) At a quick glance they both look pretty similar
0

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.