- They have a section that looks like this
-
while (lock != 0)
no-op
lock = 0
balance = balance + transaction
lock = 1
- Will this assure that two processes running the same code will not be able to access balance?
- What does the while loop look like?
-
0 locktop:
1 lw $t1, lock($zero)
2 bne $t1, $zero locktop
3 sw $t2, lock($zero) # assume $t2 has a non zero value
4 lw $t3, balance($zero)
5 add $t3, $t4, $t4 # assume $t4 has the transaction
6 sw $t3, balance($zero)
7 sw $zero, lock($zero)
- What happens inf p1 is at line 2, the value of lock is 0 but the process gets interrupted?
- Then what if p2, comes along, executes through line 4 and is interrupted?
- Could this occur in other orders?
- Lines 4-6 are known as a critical section.
- The condition we have described requires mutual exclusion, or the ability to make sure that only one process can be in the critical section at any time.
- This requires special hardware to accomplish this.
- In a sense, it requires a load and and write to memory in a single, uninterruptable, or atomic, operation.
- ll $t1,lock($t2) , load linked
- R[$t1] = M[R[$t2]+lock]
- This doesn't look special, but it is.
- sc $t1, lock($t2)
- M[R[$t0]+ lock] = R[$t1] , R[$t1] = 1,if the previous instruction was ll on this processor, and no other processor accessed this memory location.
- Otherwise: M[$t1] = 0
- The code becomes
try:
1 ll $t1,lock($t2)
2 sc $t1, lock($t2)
3 beq $t1, $zero, try
4 lw $t3, balance($zero)
5 add $t3, $t4, $t4 # assume $t4 has the transaction
6 sw $t3, balance($zero)
7 sw $zero, lock($zero)