register1 = counter
{register1 = 5}register1 = register1 + 1
{register1 = 6}register2 = counter
{register2 = 5}register2 = register2 - 1
{register2 = 4}counter = register1
{counter = 6}counter = register2
{counter = 4}The structure of process in Peterson's solution:
考點:哪三個方法及各自的做法(可能是程式碼填空)
test_and_set()
The definition of the atomic test_and_set()
instruction.
Mutual-exclusion implementation with test_and_set()
.
compare_and_swap()
The definition of the atomic compare_and_swap()
instruction.
Mutual exclusion with the compare_and_swap()
instruction.
Bounded-waiting mutual exclusion with compare_and_swap()
.
The increment()
function is implemented using CAS instruction:
wait()
signal()
wait()
and signal()
operations must be executed atomically.
wait()
signal()
wakeup()
wait(mutex)
, or the signal(mutex)
, or both.x.wait()
means that the process invoking this operation is suspended until another process invokes x.signal()
x.signal()
resumes exactly one suspended processx.wait(c)
CAS protection | Traditional synchronization | |
---|---|---|
Uncontended | Faster | |
Moderate contention | Faster |