Name (print *CLEARLY*): _________________________________________________


Student ID (print *CLEARLY*; 1 digit per underline): _____ _____ _____ _____ _____ _____

CSE 231s Quiz 21: Dining Philosophers and ReadWriteLocks

Dining Philosophers

The Dining Philosophers problem was first presented by Dijkstra in 1965 as an exam exercise for his students.

Each philosopher alternates between thinking (analagous to CPU computation) and eating with shared chopsticks (analagous to accessing a shared IO device like a disc drive).
In the implementation below uses synchronized to acquire the intrinsic lock to the chopstick to its right followed by acquiring the intrinsic lock to the chopstick to its left before eating.
The intristic locks are automatically released when the philosopher is done eating and continues to the thinking in the next iteration of the loop.

class Philosopher:
  def exist():
    while( true ):
      think()
      synchronized(rightChopstick)
        synchronized(leftChopstick)
          eat()

1a) How can this implementation produce deadlock where all of the philosophers are permanently stuck in a state of waiting?

 

 

1b) What are multiple different approaches which can prevent deadlock?

 

 

ReadWriteLock

The documentation for  java.util.concurrent.locks.ReadWriteLock states:

A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.

For the following methods on a ConcurrentMap, which lock should be acquired, if any:

Which lock for compute (Read, Write, Neither): ___________

V compute(K key, BiFunction<K,V,V> remappingFunction)
 

Which lock for get (Read, Write, Neither): ___________

V get(Object key)
 

Which lock for put (Read, Write, Neither): ___________

V put(K key, V value)

 

2) When implementing a map, consider the approaches below to make accessing it thread safe:

a) locking the entire map:

how do you achieve this?

what are the performance implications?

b) locking the bucket for the specified key:

how do you achieve this?

what are the performance implications?

c) locking the entry

how do you achieve this?

what are the performance implications?