Deadlock


12
Question 1
Suppose n processes, P1, …. Pn share m identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process Pi is Si, where Si > 0. Which one of the following is a sufficient condition for ensuring that deadlock does not occur?
A
A
B
B
C
C
D
D
Deadlock    
Discuss it


Question 1 Explanation: 
Question 2
Consider the following snapshot of a system running n processes. Process i is holding Xi instances of a resource R, 1 <= i <= n. currently, all instances of R are occupied. Further, for all i, process i has placed a request for an additional Yi instances while holding the Xi instances it already has. There are exactly two processes p and q such that Yp = Yq = 0. Which one of the following can serve as a necessary condition to guarantee that the system is not approaching a deadlock?
A
min (Xp, Xq) < max (Yk) where k != p and k != q
B
Xp + Xq >= min (Yk) where k != p and k != q
C
max (Xp, Xq) > 1
D
min (Xp, Xq) > 1
Deadlock    
Discuss it


Question 2 Explanation: 
Since both p and q don’t need additional resources, they both can finish and release Xp + Xq resources without asking for any additional resource. If the resources released by p and q are sufficient for another process waiting for Yk resources, then system is not approaching deadlock.
Question 3
Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the P operation on semaphores c, d, and a before entering the respective code segments. After completing the execution of its code segment, each process invokes the V operation (i.e., signal) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one of the following represents a deadlockfree order of invoking the P operations by the processes? (GATE CS 2013)
A
X: P(a)P(b)P(c) Y:P(b)P(c)P(d) Z:P(c)P(d)P(a)
B
X: P(b)P(a)P(c) Y:P(b)P(c)P(d) Z:P(a)P(c)P(d)
C
X: P(b)P(a)P(c) Y:P(c)P(b)P(d) Z:P(a)P(c)P(d)
D
X: P(a)P(b)P(c) Y:P(c)P(b)P(d) Z:P(c)P(d)P(a)
Process Management    Deadlock    
Discuss it


Question 3 Explanation: 
Option A can cause deadlock. Imagine a situation process X has acquired a, process Y has acquired b and process Z has acquired c and d. There is circular wait now. Option C can also cause deadlock. Imagine a situation process X has acquired b, process Y has acquired c and process Z has acquired a. There is circular wait now. Option D can also cause deadlock. Imagine a situation process X has acquired a and b, process Y has acquired c. X and Y circularly waiting for each other. See http://www.eee.metu.edu.tr/~halici/courses/442/Ch5%20Deadlocks.pdf Consider option A) for example here all 3 processes are concurrent so X will get semaphore a, Y will get b and Z will get c, now X is blocked for b, Y is blocked for c, Z gets d and blocked for a. Thus it will lead to deadlock. Similarly one can figure out that for B) completion order is Z,X then Y. This question is duplicate of http://geeksquiz.com/gate-gate-cs-2013-question-16/
Question 4
A system has n resources R0,...,Rn-1,and k processes P0,....Pk-1.The implementation of the resource request logic of each process Pi is as follows: 
 if (i % 2 == 0) {
      if (i < n) request Ri
      if (i+2 < n) request Ri+2
}
else {
      if (i < n) request Rn-i
      if (i+2 < n) request Rn-i-2
}
In which one of the following situations is a deadlock possible?
A
n=40, k=26
B
n=21, k=12
C
n=20, k=10
D
n=41, k=19
GATE CS 2010    Deadlock    
Discuss it


Question 4 Explanation: 
Option B is answer

No. of resources, n = 21
No. of processes, k = 12

Processes {P0, P1....P11}  make the following Resource requests:
{R0, R20, R2, R18, R4, R16, R6, R14, R8, R12, R10, R10}

For example P0 will request R0 (0%2 is = 0 and 0< n=21). 

Similarly, P10 will request R10.

P11 will request R10 as n - i = 21 - 11 = 10.

As different processes are requesting the same resource, deadlock
may occur. 
Question 5
Consider a system with 4 types of resources R1 (3 units), R2 (2 units), R3 (3 units), R4 (2 units). A non-preemptive resource allocation policy is used. At any given instance, a request is not entertained if it cannot be completely satisfied. Three processes P1, P2, P3 request the sources as follows if executed independently.
Process P1: 
t=0: requests 2 units of R2 
t=1: requests 1 unit of R3 
t=3: requests 2 units of R1 
t=5: releases 1 unit of R2    
        and 1 unit of R1. 
t=7: releases 1 unit of R3 
t=8: requests 2 units of R4 
t=10: Finishes

Process P2: 
t=0: requests 2 units of R3 
t=2: requests 1 unit of R4 
t=4: requests 1 unit of R1 
t=6: releases 1 unit of R3 
t=8: Finishes

Process P3: 
t=0: requests 1 unit of R4 
t=2: requests 2 units of R1 
t=5: releases 2 units of R1 
t=7: requests 1 unit of R2 
t=8: requests 1 unit of R3 
t=9: Finishes
Which one of the following statements is TRUE if all three processes run concurrently starting at time t=0? 
A
All processes will finish without any deadlock
B
Only P1 and P2 will be in deadlock.
C
Only P1 and P3 will be in a deadlock.
D
All three processes will be in deadlock
GATE-CS-2009    Deadlock    
Discuss it


Question 5 Explanation: 

We can apply the following Deadlock Detection algorithm and see that there is no process waiting indefinitely for a resource. See this for deadlock detection algorithm.

Question 6
Which of the following is NOT true of deadlock prevention and deadlock avoidance schemes?
A
In deadlock prevention, the request for resources is always granted if the resulting state is safe
B
In deadlock avoidance, the request for resources is always granted if the result state is safe
C
Deadlock avoidance is less restrictive than deadlock prevention
D
Deadlock avoidance requires knowledge of resource requirements a priori
GATE CS 2008    Deadlock    
Discuss it


Question 6 Explanation: 
Deadlock Prevention: Deadlocks can be prevented by preventing at least one of the four required conditions: 1. Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources. 2. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Require process to request and be allocated all its sources before it begins execution, or allow process to request resources only when the process has none. Low resource utilization; starvation possible. Restrain the ways request can be made. 3. No Pre-emption – If a process that is holding some resources requests another resource that cannot be immediately allocated to it, and then all resources currently being held are released. Pre-empted resources are added to the list of resources for which the process is waiting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. 4. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. Deadlock Avoidance: When a scheduler sees that starting a process or granting resource requests may lead to future deadlocks, then that process is just not started or the request is not granted. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. Choice of the question: (A) In deadlock prevention, the request for resources is always granted if the resulting state is safe. false, Deadlock prevention scheme handles deadlock by making sure that one of the four necessary conditions don't occur. In deadlock prevention, the request for a resource may not be granted even if the resulting state is safe. (B) In deadlock avoidance, the request for resources is always granted if the result state is safe. true, As in Deadlock avoidance, if resultant state is safe than request for resource is granted as being in a safe state, it can hold other resources now. (C) Deadlock avoidance is less restrictive than deadlock prevention. true, As in Deadlock prevention, request for a resource may not be granted even if the resulting state is safe. but in deadlock avoidance, request for a resource is granted if the resulting state is safe. (D) Deadlock avoidance requires knowledge of resource requirements a priori true, deadlock avoidance checks any chance of deadlock means even if the system is in safe state, it checks that after allocating requested resource, the system is not in deadlocked state. So deadlock avoidance requires knowledge of resource requirements a priori. See Question 2 of http://www.geeksforgeeks.org/operating-systems-set-11/ This solution is contributed by Nitika Bansal
Question 7
An operating system uses the Banker’s algorithm for deadlock avoidance when managing the allocation of three resource types X, Y, and Z to three processes P0, P1, and P2. The table given below presents the current system state. Here, the Allocation matrix shows the current number of resources of each type allocated to each process and the Max matrix shows the maximum number of resources of each type required by each process during its execution. GATECS2014Q42 There are 3 units of type X, 2 units of type Y and 2 units of type Z still available. The system is currently in a safe state. Consider the following independent requests for additional resources in the current state:
REQ1: P0 requests 0 units of X,  
      0 units of Y and 2 units of Z
REQ2: P1 requests 2 units of X, 
      0 units of Y and 0 units of Z
Which one of the following is TRUE?
A
Only REQ1 can be permitted.
B
Only REQ2 can be permitted.
C
Both REQ1 and REQ2 can be permitted.
D
Neither REQ1 nor REQ2 can be permitted
GATE-CS-2014-(Set-1)    Deadlock    
Discuss it


Question 7 Explanation: 
This is the current safe state.
AVAILABLE X=3, Y=2, Z=2
MAX ALLOCATION
X Y Z X Y Z
P0 8 4 3 0 0 1
P1 6 2 0 3 2 0
P2 3 3 3 2 1 1
  Now, if the request REQ1 is permitted, the state would become :
AVAILABLE X=3, Y=2, Z=0
MAX ALLOCATION NEED
X Y Z X Y Z X Y Z
P0 8 4 3 0 0 3 8 4 0
P1 6 2 0 3 2 0 3 0 0
P2 3 3 3 2 1 1 1 2 2
  Now, with the current availability, we can service the need of P1. The state would become :
AVAILABLE X=6, Y=4, Z=0
MAX ALLOCATION NEED
X Y Z X Y Z X Y Z
P0 8 4 3 0 0 3 8 4 0
P1 6 2 0 3 2 0 0 0 0
P2 3 3 3 2 1 1 1 2 2
  With the resulting availability, it would not be possible to service the need of either P0 or P2, owing to lack of Z resource. Therefore, the system would be in a deadlock. ⇒ We cannot permit REQ1.   Now, at the given safe state, if we accept REQ2 :
AVAILABLE X=1, Y=2, Z=2
MAX ALLOCATION NEED
X Y Z X Y Z X Y Z
P0 8 4 3 0 0 1 8 4 2
P1 6 2 0 5 2 0 1 0 0
P2 3 3 3 2 1 1 1 2 2
  With this availability, we service P1 (P2 can also be serviced). So, the state is :
AVAILABLE X=7, Y=4, Z=2
MAX ALLOCATION NEED
X Y Z X Y Z X Y Z
P0 8 4 3 0 0 1 8 4 2
P1 6 2 0 5 2 0 0 0 0
P2 3 3 3 2 1 1 1 2 2
  With the current availability, we service P2. The state becomes :
AVAILABLE X=10, Y=7, Z=5
MAX ALLOCATION NEED
X Y Z X Y Z X Y Z
P0 8 4 3 0 0 1 8 4 2
P1 6 2 0 5 2 0 0 0 0
P2 3 3 3 2 1 1 0 0 0
  Finally, we service P0. The state now becomes :
AVAILABLE X=18, Y=11, Z=8
MAX ALLOCATION NEED
X Y Z X Y Z X Y Z
P0 8 4 3 0 0 1 0 0 0
P1 6 2 0 5 2 0 0 0 0
P2 3 3 3 2 1 1 0 0 0
The state so obtained is a safe state. ⇒ REQ2 can be permitted. So, only REQ2 can be permitted. Hence, B is the correct choice.   Please comment below if you find anything wrong in the above post.
Question 8
A system contains three programs and each requires three tape units for its operation. The minimum number of tape units which the system must have such that deadlocks never arise is _________.
A
6
B
7
C
8
D
9
GATE-CS-2014-(Set-3)    Deadlock    
Discuss it


Question 8 Explanation: 
If 6 resources are there then it may be possible that all three process have 2 resources and waiting for 1 more resource. Therefore, all of them will have to wait indefinitely. If 7 resources are there, then atleast one must have 3 resources so deadlock can never occur.
Question 9
Consider the following snapshot of a system running n processes. Process i is holding Xi instances of a resource R, 1 <= i <= n. currently, all instances of R are occupied. Further, for all i, process i has placed a request for an additional Yi instances while holding the Xi instances it already has. There are exactly two processes p and q such that Yp = Yq = 0. Which one of the following can serve as a necessary condition to guarantee that the system is not approaching a deadlock?
A
min (Xp, Xq) < max (Yk) where k != p and k != q
B
Xp + Xq >= min (Yk) where k != p and k != q
C
max (Xp, Xq) > 1
D
min (Xp, Xq) > 1
GATE-CS-2006    Deadlock    
Discuss it


Question 9 Explanation: 
Deadlock refers to a specific condition when two or more processes are each waiting for another to release a resource, or more than two processes are waiting for resources. Solution: Necessary condition to guarantee no deadlock which means without satisfying this condition, no deadlock is possible. Both the process p and q have no additional requirement; they both can be finished releasing Xp + Xq resources without asking for any additional resource. Using this, we can finish one more process only if condition B is satisfied. If the resources released by p and q are sufficient for another process waiting for Yk resources, then system is not approaching deadlock. i.e Xp+Xq > min (Yk) where k != p and k != q Note: Option B just ensured that the system can proceed from the current state. It does not guarantee that there won’t be a deadlock before all processes are finished. See question 4 of http://www.geeksforgeeks.org/operating-systems-set-15/ Reference : https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/7_Deadlocks.html This solution is contributed by Nitika Bansal
Question 10
Suppose n processes, P1, …. Pn share m identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process Pi is Si, where Si > 0. Which one of the following is a sufficient condition for ensuring that deadlock does not occur?
A
A
B
B
C
C
D
D
GATE-CS-2005    Deadlock    
Discuss it


Question 10 Explanation: 
There are 20 questions to complete.
12

Company Wise Coding Practice    Topic Wise Coding Practice