Docsity

Solved Homework 1 - Programming Language Technologies and Paradigms | CMSC 433, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 1
CMSC 433
Programming Language Technologies and Paradigms
Fall 2003
Due Wednesday November 26, 2003 at 6pm
Submission instructions are in Section 3.
The following are exercises to tighten your knowledge of concurrent pro-
gramming in Java. Write the answer to each problem in a separate text file; e.g.
for problem 1 you will write 1.txt, etc. Any java code you write should also be
included in the text file for the problem you write it for. If you don’t want to
use “ASCII graphics” for problems 2 and 3, you can include separate .jpg files
for your wait-graphs. Use the name of the problem as the title, e.g. 2.jpg, or
3.1.jpg,3.2.jpg, etc.
1 Background
Threads can be in one of four thread states:created (after creation, but before
calling start), runnable (either running, or eligible to be run), blocked (waiting
for some event before execution can resume), and terminated (the thread is
finished). Threads can be blocked for many reasons, including waiting to acquire
a lock, waiting to be awakened with notify, waiting on I/O, and sleeping for a
fixed time.
All objects in Java can behave as locks. Each lock has three conceptual
fields: the current thread owner of the lock (if any), the count of the times the
owner has re-acquired the lock (0 if there is no owner), and the wait-set, which
is a list of threads waiting on the lock, each paired with a count indicating the
number of times a thread had reacquired a lock before it started waiting. The
values of these three fields are called the lock state of a particular lock.
2 Problems
Problem 1 Consider the classes BoundedBuffer and Prob1. The latter class
constructs two threads that insert strings into a shared bounded buffer.
Run the code to see what the output looks like. Only one output sequence
is possible (why?).
Answer:
pf3
pf4
pf5

Partial preview of the text

Download Solved Homework 1 - Programming Language Technologies and Paradigms | CMSC 433 and more Assignments Programming Languages in PDF only on Docsity!

Homework 1

CMSC 433

Programming Language Technologies and Paradigms

Fall 2003

Due Wednesday November 26, 2003 at 6pm

Submission instructions are in Section 3. The following are exercises to tighten your knowledge of concurrent pro- gramming in Java. Write the answer to each problem in a separate text file; e.g. for problem 1 you will write 1.txt, etc. Any java code you write should also be included in the text file for the problem you write it for. If you don’t want to use “ASCII graphics” for problems 2 and 3, you can include separate .jpg files for your wait-graphs. Use the name of the problem as the title, e.g. 2.jpg, or 3.1.jpg, 3.2.jpg, etc.

1 Background

Threads can be in one of four thread states: created (after creation, but before calling start), runnable (either running, or eligible to be run), blocked (waiting for some event before execution can resume), and terminated (the thread is finished). Threads can be blocked for many reasons, including waiting to acquire a lock, waiting to be awakened with notify, waiting on I/O, and sleeping for a fixed time. All objects in Java can behave as locks. Each lock has three conceptual fields: the current thread owner of the lock (if any), the count of the times the owner has re-acquired the lock (0 if there is no owner), and the wait-set, which is a list of threads waiting on the lock, each paired with a count indicating the number of times a thread had reacquired a lock before it started waiting. The values of these three fields are called the lock state of a particular lock.

2 Problems

Problem 1 Consider the classes BoundedBuffer and Prob1. The latter class constructs two threads that insert strings into a shared bounded buffer. Run the code to see what the output looks like. Only one output sequence is possible (why?).

Answer:

Each get result is printed after the corresponding put in the other thread. This means that t1 must print its gets first, since t2 cannot print its gets until t1 has done its puts, which always happens after its gets.

Assume that (1) the threads are run on a single processor, (2) t1 is sched- uled first, and (3) the scheduler runs each thread until it blocks before switching to the other thread. For each of the labelled program points t1-1, t2-1, t1-2, etc. in Prob1, describe (1) the thread state of each of the threads t1 and t2, and (2) the lock state of the BoundedBuffer objects buf and buf2.

Answer: For all program points, perhaps surprisingly, the lock state of both buf and buf2 is always that (1) the lock is not held (owner and count are empty), and (2) no threads are on the wait set. This is because (1) no locks are ever held within Prob1, only within BoundedBuffer, and (2) for each thread to get to the current point, it invokes notifyAll after waking up (at the conclusion of the get or put it was blocked in), which clears the wait sets that had any waiters. Moreover, it is always the case that the current thread is running, and the other thread is still blocked (waiting to reacquire its lock after being awakened by notifyAll). That is, at points t1-X, thread t1 is Runnable and t2 is Blocked, while at points t2-X, thread t2 is Runnable and t1 is Blocked.

Problem 2 Consider the interface Number which defines integer-like numbers and a way to add them, and the classes MyNatural and MyInteger, which implement Number. The class Prob2 creates two threads that add some shared Number objects. Feel free to run the program to see its output. This program can exhibit a deadlock. (1) Draw a wait-graph of the pro- gram showing this deadlock. (2) Indicate which statements in the given classes caused each thread to be blocked in this deadlocked state.

Answer:

t

t

n

n

int thisHc = hashCode(); if (nHc < thisHc) { synchronized (n) { synchronized (this) { doAdd(n); } } } else { synchronized (this) { synchronized (n) { doAdd(n); } } } }

Problem 3 The class BoundedBuffer2 is the same as the BoundedBuffer class, except that (1) it replaces occurrences of notifyAll with notify; and (2) it replaces put with putArray, which inserts the contents of an array into the buffer, rather than a single item. The class Prob3 has four variables, set by the command-line flags: p is the number of producer threads, which insert a group of items of size b into a single shared buffer; c is the number of consumer threads, which each remove a single item of the shared buffer; and s is the size of the buffer. For simplicity, you may assume a single processor, but any possible legal schedule. For each of the following values of these parameters, indicate either (a) all of the tokens added to the buffer will be removed, and all the threads will exit normally; or (b) that some number of the threads are stuck in a blocking state before all tokens are removed from the buffer (e.g., this can happen because of deadlock). For (a), be as precise as possible— e.g. describe an invariant that the program maintains the implies constant progress. For (b), write an execution trace that that shows how threads will fail to exit normally. Refer to the threads as C 1 ...Cc for the consumers, and P 1 ...Pp for the producers.

  1. p = 1, c = 1, s = 1, b = 1.
  2. p > 0, c = p, s = 1, b = 1.
  3. p = 1, c = 4, s = 4, b = 4.
  4. p = 2, c = 4, s = 5, b = 2.

Hint: think about the kinds of threads that can be on the buffer’s wait-set, and what that implies with regard to deadlock.

Answer:

In all cases, there is only ever one lock to acquire, so there is no possibility of deadlock due to circularities in wait-graphs. The only possibility of deadlock is due to a producer or consumer being on a wait-set forever (missed by a notify operation).

  1. Will complete normally. It is only possible for the consumer to be on the buffer’s wait-set, and in this case, we are sure that the waiter will be awakened by the notify of the other thread.
  2. Will not complete normally. Consider when p = 2 and c = 2, where P1 and P2 are the producers and C1 and C are the consumers. Here’s a trace: - C1: buffer.get() blocks on buffer - C2: buffer.get() blocks on buffer - P1: buffer.put() fills buffer and calls buffer.notify(); C1 is taken off buffer’s wait-set - P2: buffer.put() blocks on buffer (since it’s full) - C1: reacquires buffer’s lock, removes item, and calls buffer.notify() - C2: gets lock, buffer.size() is 0, so waits again. At this point C2 are and P2 are on buffer’s wait-set, never to wake up.
  3. Will complete normally. There can only ever be consumers on the wait-set, since there is always space for the sole pro- ducer to put his items. In the case that there are multiple consumers, the notify will wake one of them, which when it removes its item from the buffer will wake the next with its notify, etc. until all waiting threads are awakened.
  4. Will complete normally. Once again, there can only be con- sumers on the wait-set, since the producers will always be able to put their items in the buffer. Each time a producer does this, one consumer on the wait set will be awakened, which will awaken another, etc. as above.

Problem 4 The Eater class defines a variant of the famous “dining philoso- phers problem” (if you don’t know what that is, look it up on the web for fun). The idea is that in order to eat, each eater must acquire a chair, then a plate, and then the food. The food is put on the table by a separate chef thread (the main method of the Eater class). After an Eater eats, it consumes the food and gives up the chair and the plate. But: eating is all or nothing: if an eater grabs the plate, and then the chair, but there is no food on the table, it must release both and give someone else a chance. The class that we have provided defines the chair and the plate as Resources; to grab a resource, you set its gotIt flag to true. Since resources are

" says, "Who hid my food?""); try { chair.release(); plate.release(); food.wait(); } catch (InterruptedException e) {} } else { System.out.println(this.getName() + " says, " That’s right. Crunch Crunch""); food.ready = false; break; } } } chair.release(); plate.release(); } The result is that access to the food will be fair among the dif- ferent eaters, since as soon as one thread fails to get the food, a different thread will be able to get the chair and the plate. There are other solutions.

3 Submission Instructions

To submit your homework, put all of the relevant files (1.txt, 2.txt, etc.) into a directory hw1/ and then tar and gzip this directory into the file hw1.tgz. On Linuxlab, starting from the parent directory of hw1/ you would do

tar zcvf hw1.tgz hw

Now, submit this file, hw1.tgz, using the submit system, using project number ”1a” as in

java -jar Submit.jar 1a hw1.tgz

(assuming your personal Submit.jar is in the same directory as your hw1.tgz file).