02152  Concurrent Systems - CP Mini Lab 3: Critical Region with Semaphores
Technical University of Denmark DTU
02152 Concurrent Systems        Fall 2008
CP Mini Lab 3: Critical Region with Semaphores
Home Plan Material  

Purpose

To use semaphores to protect shared variables.

Prerequisites

Read about semaphores in [Andrews], chapter 4.

Instructions

The synchronization problem from CP Mini Lab 2 is now to be solved using semaphores.

  1. Fetch the program Counting.java from CP Mini Lab 2.
  2. Compile and run the program a couple of times. If you don't get varying results, you should use the class VIP as decribed in Mini Lab 2.
  3. Fetch Semaphore.class. This is the Java byte-code for a class of general semaphores with the usual operations:

      Semaphore(int s0)       // Constructor (sets the initial value, s0>=0)
      void P() 
      void V()
    
    

    [The operation P() may throw an InterruptedException. This should just be caught and ignored.]

  4. Use a semaphore to make a critical region around x++, such that the increment becomes atomic.

    Run the program again and find:

  5. When you have learned about Java monitors, you may study the source code.

Enjoy!

Hans Henrik Løvengreen, Sep 19, 2008