Homework #5Assigned: March 11
Due: March 18, 3:00pm, in class
- (5 points) The HardwareData
class abstracts of the idea of the get-and-set and
swap hardware instructions. However, this class is
not thread-safe because multiple threads may
concurrently access its methods and thread safety
requires that each method be performed atomically.
Rewrite the HardwareData class using Java
synchronization so that it is thread safe. See
Thread1
and Thread2
classes which employ the get-and-set and swap
hardware instructions, respectively.
- (15 points) Consider only 3 thread types (A, B,
and C) with the following constraints:
- mutual exclusion among the A's,
- mutual exclusion between the B's and
C's,
- mutual exclusion between the A's and C's,
and
- priority to the A's.
Using only the Factory, Thread1, and
SleepUtilities
classes, complete the definition of the Java
monitor Monitor class to
solve this problem. Do not modify these classes
at all. Complete only the data member section and
provide defintions for the constructor and the 6
entry and exit methods.
- (12 points) The Sleeping-Barber Problem:
a barbershop consists of a waiting room and
n chairs and a barber room with one barber
chair. If there are no customers to be serviced,
the barber goes to sleep. If a customer enters the
barbershop and all chairs are occupied, then the
customer leaves the shop. If the barber is busy but
chairs are available, then the customer sits in one
of the free chairs. If the barber is asleep, the
customer wakes up the barber.
Write a Java program using semaphores to
coordinate the barber and the customers.
Specificially, using only the Customer,
Factory,
and SleepUtilities
classes, complete the definition of the BarberShop
class. Do not modify these classes at all. Only
complete the data member section and provide
definitions for the constructor and the
customerEnters(int id) method.
How to submitNote: 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/hw5/HardwareData.java,
/home/<logname>/homeworks/hw5/Monitor.java
and
/home/<logname>/homeworks/hw5/BarberShop.java,
where <logname> is your login name,
e.g., cps346-01.01. Only these source
files will be electronically collected from this
location in your UNIX account on the deadline.
- Convert your source code files into a
pretty-printed pdf file by executing the following
command lines on our system from the directory in
your UNIX account containing your source files
HardwareData.java, Monitor.java,
and BarberShop.java.
a2ps HardwareData.java -o HardwareData.ps
a2ps Monitor.java -o Monitor.ps
a2ps BarberShop.java -o BarberShop.ps
ps2pdf HardwareData.ps
ps2pdf Monitor.ps
ps2pdf BarberShop.ps
or simply
a2ps HardwareData.java
a2ps Monitor.java
a2ps BarberShop.java
which will cause
HardwareData.java,
Monitor.java, and
BarberShop.java to be printed to the
printer in the AN 131 lab.
- Submit a printouts of
HardwareData.pdf, Monitor.pdf,
and BarberShop.pdf in class at 3:00pm.
Failure to follow these submission requirements
will result in a 10% penalty.
|