Homework #5

Assigned: March 20
Due: March 27, 3:00pm


  1. (5 points) [OSCJ8] exercise 6.37 on p. 305 (or exercise 6.20 on p. 267 of the 7th ed). 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.

  2. (15 points) Consider only three thread types (A, B, and C) with the following constraints:

    • mutual exclusion among the As,
    • mutual exclusion between the Bs and Cs,
    • mutual exclusion between the As and Cs, and
    • priority to the As (i.e., additional Cs must not enter the <CS> if one or more As are waiting to enter the <CS>, and if an opportunity arises to signal an A or C, signal the A.)

    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 six entry and exit methods.

  3. (12 points) [OSCJ8] exercise 6.44 on p. 308 (or exercise 6.24 on p. 268 of the 7th ed). 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 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/hw5/HardwareData.java, /home/<logname>/homeworks/hw5/Monitor.java and /home/<logname>/homeworks/hw5/BarberShop.java, where <logname> is your login name (e.g., FA_12_CPS346_03). Only these source files will be electronically collected from this location in your UNIX account on the deadline.

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


Return Home