-- Lab
#6 --
-
Objectives
- This lab gives you the chance to experiment with
semaphores and monitors in the context of classic concurrent
programming problems such as Bounded Buffer and Readers and
Writers.
-
References
- Sun's Java Tutorial on threads: synchronization
Silberschatz, Galvin & Gagne: Chapter 7
-
Pre-Lab
- Copy the source code
Server.java,
Worker.java, and
Semaphore.java. Read the program and understand
what the program is trying to do. Run the program a few times to
observe the behavior of the program. First identify in the code
of Worker what is intended as the critical region
(CR). The existing program doesn't
actually use the Semaphore to protect the critical
region(s) yet. Add the necessary code to use the
Semaphore to protect the CR. Compile and run the program
again to see if it makes any difference.
Hand in a printout of the code for the Worker
class at the beginning of the lab.
You should also review Sections 7.6.1 (a semaphore solution to
the bounded buffer producers and consumers problem) and 7.8.3 (a
solution to the readers and writers problem using Java
synchronization) before the start of lab. It will be useful to
bring your text to lab.
-
Problems
-
[Note: in each of the following exercises, save and print a
separate copy of your solution as requested.]
- A.
- Copy the following files for the Bounded Buffer
Producers and Consumers problem to your Lab 6 directory:
Server.java,
Producer.java,
Consumer.java,
and
BoundedBuffer.java.
Complete the following activities:
- Compile and run the program (main()
is in Server.java). Let the program run
long enough (e.g. produced and consumed over 100 jobs or
more) to observe a problem. What do you
see? Look at the code to determine what's going
wrong. What causes the error to occur? Does
this correspond to a problem that can occur in a
real system?
- Using the Semaphore class you copied from the pre-lab,
fix the problem in bounded buffer by using a
mutual exclusion semaphore (what is the initial
value of such a semaphore?). Hand in a listing
of you corrected BoundedBuffer.java file
showing your solution (be sure to label the code
in your hand-in file).
- Modify your solution to the previous part (save a copy) by
making the following changes:
- Modify Server to launch three
Producer threads. Make sure you give
them distinct names so you can distinguish them
in the output.
- Add the line
napping(2);
to
BoundedBuffer's
enter() method immediately after the
initial while loop.
- Add a buffer overflow message to
BoundedBuffer's enter()
method immediately after the code that
increments count. The message should
only be printed if count exceeds
BUFFER_SIZE.
What happens when you run the program? Why?
[Note that a similar problem could occur
with buffer underflow if multiple consumers were
active, and the napping() call was
included in remove().]
- Correct the problem the previous changes
have created by adding synchronization (counting)
semaphores to ensure that overflow doesn't
occur. Hand in a listing
of you corrected BoundedBuffer.java file
(again, be sure to label the code in your hand-in
file).
Hand in your answers to the questions listed
above, along with the specified code for your
solutions.
- B.
- Create a new subdirectory in which to work on this
part of the lab. Copy into that directory the following
files for the critical section problem:
Server.java,
Worker.java, and
Semaphore.java.
Complete the following activities (if you completed the pre-lab
activity properly, skip the first two problems):
- Compile and run the programs as is - the main
method is in Server.java. Let the program run
until it generates 15-20 lines of output. Consider the
output in light of the comments in the worker code
indicating the existence of critical sections. If there
seems to be a problem describe the problem and how you
deduce it from the output.
You will handin the output and your answer.
- Now make appropriate use of the semaphore passed to
the workers to solve the problem just discovered.
Run the resulting program and explain how the output
seems to indicate a solution.
- Find three different ways to create a deadlock
among the workers (by modifying the semaphore or its
use). For each answer, turn in your code for the
worker classes and the output which shows a deadlock.
- C.
- In this part of the lab you will experiment with
solutions to the readers and writers problem (see the course
text for relavant code).
Before getting started create a new subdirectory in which
to work on this
part of the lab. Copy into that directory the following
files for the critical section problem:
Server.java,
Database.java,
Reader.java,
and
Writer.java.
You will also want to use the semaphore class from the
previous part - copy its .java file into the
new directory as well.
With this code in hand carry out the following activities.
- The program code you copied contains a semaphore
solution to the readers and writers problem. You are
to run the program and let a couple of screens worth of
output accumulate. Can you identify a problem with the
solution? Does it seem to provide protected access to
the database? Does it seem to provide fair access to the
database? Does increasing the number of writers
change the situation? Justify your answers by refering to the
output
- Java provides a synchronization facility to
manage process synchronization without using the
semaphores. Read the course text about
the subject and the code example. Produce a new solution to
the the readers/writers problem by modifying the
Database class to use
the Java synchronization. When this is working
run the resulting program and answer the questions of the
previous part - contrast the output from this version with
that from the semaphore solution. What is the key difference
in logic between a solution using Semaphore and
that using Synchronized methods?
-
Hand in
- Hand in your answers and/or discussions relative to the
activities above by next Monday.
This document was generated using the
LaTeX2HTML translator Version 2002-2-1 (1.70)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -nonavigation -split 1 lab
The translation was initiated by CS315 on 2004-02-21
CS315
2004-02-21