📜  门| GATE-CS-2004 |第 48 题

📅  最后修改于: 2021-09-25 07:34:17             🧑  作者: Mango

考虑两个进程 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 提供。
这个问题的测验