考虑两个进程P1和P2访问分别由两个二进制信号量SX和SY保护的共享变量X和Y,它们都初始化为1。P和V表示通常的信号量运算符,其中P减小信号量值,而V增大信号量值。 P1和P2的伪代码如下:
P1:
While true do {
L1 : ................
L2 : ................
X = X + 1;
Y = Y - 1;
V(SX);
V(SY);
}
P2:
While true do {
L3 : ................
L4 : ................
Y = Y + 1;
X = Y - 1;
V(SY);
V(SX);
}
为了避免死锁,分别在L1,L2,L3和L4处使用正确的运算符
(A) P(SY),P(SX); P(SX),P(SY)
(B) P(SX),P(SY); P(SY),P(SX)
(C) P(SX),P(SX); P(SY),P(SY)
(D) P(SX),P(SY); P(SX),P(SY)答案: (D)
解释:
Option A: In line L1 ( p(Sy) ) i.e. process p1 wants lock on Sy that is
held by process p2 and line L3 (p(Sx)) p2 wants lock on Sx which held by p1.
So here circular and wait condition exist means deadlock.
Option B : In line L1 ( p(Sx) ) i.e. process p1 wants lock on Sx that is held
by process p2 and line L3 (p(Sy)) p2 wants lock on Sx which held by p1. So here
circular and wait condition exist means deadlock.
Option C: In line L1 ( p(Sx) ) i.e. process p1 wants lock on Sx and line L3 (p(Sy))
p2 wants lock on Sx . But Sx and Sy can’t be released by its processes p1 and p2.
请阅读以下内容,以了解有关进程同步和信号量的更多信息:
进程同步集1
这种解释是由Dheerendra Singh贡献的。
这个问题的测验