设T1,T2是两个事务如下:
T1:
A
:
=
A
+
2
,
B
=
B
×
2
A:=A+2,B=B\times2
A:=A+2,B=B×2; T2:
A
:
=
A
×
2
,
B
=
B
+
2
A:=A\times2,B=B+2
A:=A×2,B=B+2;
设A,B的初值都为0
(1)若这两个事务容许并发执行,则有多少种可能的正确结果?请一一例举出来; 有两种可能: T1???T2 ——
A
=
4
,
B
=
2
A=4,B=2
A=4,B=2 T2???T1 ——
A
=
2
,
B
=
4
A=2,B=4
A=2,B=4
(2)请给出一个可串行化的调度,并给出执行结果;
T1 | T2 |
---|
SlockA | | Y=Read(A)=0 | | Unlock A | | Xlock A | | A=Y+2 | | Write(A) | | Unlock A | | SlockB | | Z=Read(B)=0 | | Unlock B | | Xlock B | | B=Z*2 | | Write(B) | | Unlock B | | ? | SlockA | ? | Y=Read(A)=0 | ? | Unlock A | ? | Xlock A | ? | A=Y*2 | ? | Write(A) | ? | Unlock A | ? | SlockB | ? | Z=Read(B)=0 | ? | Unlock B | ? | Xlock B | ? | B=Z+2 | ? | Write(B) | ? | Unlock B |
结果:
A
=
4
,
B
=
2
A=4,B=2
A=4,B=2
(3)请给出一个非串行化的调度,并给出执行结果;
T1 | T2 |
---|
SlockA | | Y=Read(A)=0 | | Unlock A | | Xlock A | | ? | SlockA | A=Y+2 | 等待 | Write(A) | 等待 | Unlock A | 等待 | ? | Y=Read(A)=0 | ? | Unlock A | ? | Xlock A | SlockB | | 等待 | A=Y*2 | 等待 | Write(A) | 等待 | Unlock A | Z=Read(B)=0 | | Unlock B | | Xlock B | | ? | SlockB | B=Z*2 | 等待 | Write(B) | 等待 | Unlock B | 等待 | ? | Z=Read(B)=0 | ? | Unlock B | ? | Xlock B | ? | B=Z+2 | ? | Write(B) | ? | Unlock B |
结果:
A
=
0
,
B
=
2
A=0,B=2
A=0,B=2
(4)若这两个事务都遵守两段锁协议,请给出一个不产生死锁的可串行化的调度;
T1 | T2 |
---|
SlockA | | Y=Read(A)=0 | | Xlock A | | A=Y+2 | | Write(A) | | SlockB | | Z=Read(B)=0 | | Xlock B | | B=Z*2 | | Write(B) | | Unlock A | | Unlock A | | Unlock B | | Unlock B | | ? | SlockA | ? | Y=Read(A)=0 | ? | Xlock A | ? | A=Y*2 | ? | Write(A) | ? | SlockB | ? | Z=Read(B)=0 | ? | Xlock B | ? | B=Z+2 | ? | Write(B) | ? | Unlock A | ? | Unlock A | ? | Unlock B | ? | Unlock B |
(5)若这两个事务都遵守两段锁协议,请给出一个产生死锁的可串行化的调度;
T1 | T2 |
---|
SlockA | | ? | SlockA | Xlock A | | 等待 | | 等待 | Xlock A | 等待 | 等待 |
|