Homework #6

Assigned: November 11
Due: November 18, 2:00pm, in class


(32 points) [OSCJ] pp. 302-303. Implement Banker's algorithm in Java. Several customers request and release resources from the bank. The banker grants a request only if it leaves the bank in a safe state. A request is denied if it leaves the bank in an unsafe state.

Your definition of the BankImpl class must implement the Bank interface. Your implementation of this interface requires adding a constructor which is passed the number of resources initially available. For instance, suppose we have 3 resource types with 10, 5, and 7 instances initially available. In this case, we create an instance of the bank as follows:

int[] initialResources = new int[3];

initialResources[0] = 10;
initialResources[1] = 5;
initialResources[2] = 7;

Bank bank = new BankImpl(initialResources);

The bank will grant a request if the request satisfies the safety algorithm presented in class. If granting the request will result in an unsafe state, the request is denied.

Use the TestHarness class (with the Customer and SleepUtilities classes) to test your implementation of the Bank interface. This program requires an input file containing the maximum demand of each resource type for each customer. For instance, if there are 5 customers and 3 resource types, the input stream might appear as follows:

7,5,3
3,2,2
9,0,2
2,2,2
4,3,3
The indicates that the maximum demand for customer 0 is 7,5,3; for customer 1 is 3,2,2, and so on. Since each line of the input file represents a separate customer, the addCustomer() method must be invoked as each line is read in, initializing the value of maximum for each customer. In the above example, the value of maximum[0][] is initialized to 7,5,3 for customer 0, maximum[1][] is initialized to 3,2,2, and so forth.

Furthermore, TestHarness requires the initial number of resources available in the bank. For example, if there are initially 10,5, and 7 resources available, we invoke TestHarness as follows:

java TestHarness infile.txt 10 5 7
where infile.txt refers to a file containing the maximum demand for each customer followed by the number of resources initially available. The available array must be initialized to the values passed on the command line.

Do not modify any of the given classes at all. Only complete provide defintions for the constructor and the 4 methods in the BankImpl class.

How to submit

Note: All directory and filenames below are case-sensitive. You must use the directory and filenames exactly as shown below, i.e., all lower case.

Prepare your source code files as /home/<logname>/homeworks/hw6/BankImpl.java, where <logname> is your login name, e.g., cps346-01.01. Only this source files will be electronically collected from this location in your UNIX account on the deadline.

Submit a pretty-printed listing of BankImpl.java using a2ps in class.

Failure to follow these submission requirements will result in a 10% penalty.


Return Home