事务 #
Consider the following two transactions:
𝑇34
𝑟𝑒𝑎𝑑(𝐴)
𝑟𝑒𝑎𝑑(𝐵)
𝑖𝑓 𝐴 = 0 𝑡ℎ𝑒𝑛 𝐵 ∶= 𝐵 + 1;
𝑤𝑟𝑖𝑡𝑒(𝐵)
𝑇35
𝑟𝑒𝑎𝑑(𝐵)
𝑟𝑒𝑎𝑑(𝐴)
𝑖𝑓 𝐵 = 0 𝑡ℎ𝑒𝑛 𝐴 ∶= 𝐴 + 1;
𝑤𝑟𝑖𝑡𝑒(𝐴)
Add lock and unlock instructions to transactions T31 and T32 so that they observe the two-phase locking protocol. Can the execution of these transactions result in a deadlock?
答:此时满足两阶段锁协议:
T34:
lock(A) // Acquire a lock on A read(A) lock(B) // Acquire a lock on B read(B) if A = 0 then B := B + 1 write(B) unlock(B) // Release the lock on B unlock(A) // Release the lock on A
T35:
lock(B) // Acquire a lock on B read(B) lock(A) // Acquire a lock on A read(A) if B = 0 then A := A + 1 write(A) unlock(A) // Release the lock on A unlock(B) // Release the lock on B
观察到不存在循环依赖或者获取锁的冲突。所以不存在死锁现象。