|
UNIVERSITY OF WISCONSIN-MADISON
Computer Sciences Department | ||
|
CS 537
Spring 2001 | A. Arpaci-Dusseau | |
| Quiz #3: Feb 21th -- Synchronization |
| Name: Solutions | Student ID #: Solutions |
For each code sample, answer the following questions:
class Lock {
private int turn = 0;
public void acquire(int tid) {
while (turn == (1 - tid));
}
public void release(int tid) {
turn = (1 - tid);
}
}
|
class Lock {
public void acquire() {
disableInterrupts();
}
public void release() {
enableInterrupts();
}
}
|
class Lock {
private int turn = 0;
private boolean lock[2] = {false, false};
public void acquire(int tid) {
lock[tid] = true;
turn = 1 - tid;
while (lock[1-tid] && turn == (1 - tid));
}
public void release(int tid) {
lock[tid] = false;
}
}
This is the solution presented in class.
|
class Lock {
private boolean lock = true;
public void acquire() {
while (TestAndSet(lock, true);
}
public void release() {
lock = false;
}
}
Note that the lock was initialized to the wrong value.
|
Advantages of maintaining a list of waiting processes include: