Monitors, conditions, readers and writers locks Flashcards

1
Q

What are monitor locks

A

only 1 thread can hold a lock
A thread acquires a lock when it starts to hold the lock
releases lock when stops holding lock

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In Monitor locks what happens when a thread cant immediately acquire a lock?

A

Spins and repeatedly tests
Blocks and suspends thread and creates new thread
Spin = short time. Blocking = long time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create a new condition

A

Condition = mutex.newCondition();//mutex = lock that its related to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how do you make a condition suspend

A

condition.await()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how can you notify threads that a change has occurred in a condition?

A

signal() or signalAll()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Does a thread have to retest condition if awaken and compete for the lock?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a monitor?

A

combination of mutual exclusion locks and condition objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is lost wakeup problem?

A

1+ threads wait forever without realizing that the property for which they have been waiting has become true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

enq code for Monitor?

A

public void enq(T x) {
lock.lock();
try {
while (count == items.length)
isFull.await();
items[tail] = x;
++count;
if (count == 1)
isEmpty.signal();
}
} finally {
lock.unlock();
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to avoid lost wakeup

A

ensure all processes are signaled
Specify timeout when waiting

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is allowed in the CS for reader-writer locks?

A

either multiple readers or a single writer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Reader-writer lock interface

A

public interface ReadWriteLock {
Lock readLock();
Lock writeLock();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What properties should the interface satisfy for the reader-writer lock?

A

No thread can acquire write lock while any thread holds either lock or read lock
no thread can acquire read lock while any other thread holds write lock

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Class code for SimpleReadWriteLock

A

public class SimpleReadWriteLock implements ReadWriteLock {
boolean writer;
int readers;
Lock lock;
Condition condition;
Lock readLock, writeLock;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

lock for ReadLock

A

class ReadLock implements Lock {
public void lock() {
lock.lock();
try {
while (writer)
condition.await();
readers++;
} finally {
lock.unlock();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

unlock for readLock

A

public void unlock() {
lock.lock();
try {
readers–;
if (readers == 0)
condition.signalAll();
} finally {
lock.unlock();
}

17
Q

lock for writeLock

A

class WriteLock implements Lock {
public void lock() {
lock.lock();
try {
while (readers > 0)
condition.await();
writer = true;
} finally {
lock.unlock();
}

18
Q

un lock for writeLock

A

public void unlock() {
writer = false;
condition.signalAll();
}

19
Q

Is the simple readWrite lock fair?

A

no as long as readers are “added” it can wait

20
Q

FIFOReadWriteLock

A

public class FIFOReadWriteLock
implements ReadWriteLock {
boolean writer;
int readAcquires, readReleases;
Lock lock;
Condition condition;
Lock readLock, writeLock;

21
Q

FIFOReader lock

A

class ReadLock implements Lock {
public void lock() {
lock.lock();
try {
while (writer)
condition.await();
readAcquires++;
} finally {
lock.unlock();
}

22
Q

FIFOreader unlock

A

public void unlock() {
lock.lock();
try {
readReleases++;
if (readAcquires == readReleases)
condition.signalAll();
} finally {
lock.unlock();
}

23
Q

FIFOWrite lock

A

class WriteLock implements Lock {
public void lock() {
lock.lock();
try {
writer = true;
while (readAcquires !=
readReleases)
condition.await();
} finally {
lock.unlock();
}

24
Q

FIFOWrite unlock

A

public void unlock() {
writer = false;
condition.signalAll();
}