Quoting https://en.wikipedia.org/wiki/Deadlock#Necessary_conditions :
A deadlock situation can arise if all of the following conditions hold simultaneously in a system:
- Mutual exclusion: at least one resource must be held in a non-shareable mode.Only one process can use the resource at any given instant of time.
- Hold and wait or resource holding: a process is currently holding at least one resource and requesting additional resources which are being held by other processes.
- No preemption: a resource can be released only voluntarily by the process holding it.
- Circular wait: a process must be waiting for a resource which is being held by another process, which in turn is waiting for the first process to release the resource. In general, there is a set of waiting processes, P = {P1, P2, …, PN}, such that P1 is waiting for a resource held by P2, P2 is waiting for a resource held by P3 and so on until PN is waiting for a resource held by P1.
Wikipedia claims these conditions are necessary for a deadlock to occur. In addition, I’ve heard claims they are also sufficient (although I may’ve misinterpreted these claims).
The problem is that I have examples that seem to me to contradict both necessity or sufficiency of those conditions. And I can’t see my error.
Necessity first:
Let R1 and R2 be two resources shareable by two processes at most. Let P1, P2, P3, P4 be processes such as P1 and P2 hold one piece of R1 each and each wait for a piece of R2, and P3 and P4 hold one piece of R2 each and each wait for a piece of R1, as below:
Assume that #3 holds. #2 and #4 are obviously fulfilled, but #1 is not as both R1 and R2 can be shared. Yet a deadlock seems to occur.
Now sufficiency:
Let R1 and R2 be two resources, such as R1 is shareable by two processes at most and R2 is non-shareable. Let P1, P2 and P3 be processes such as P1 holds R2 and wants a piece of R1, R2 holds a piece of R1 and wants R2 and R3 holds a piece of R1 and wants nothing more, as below:
Again, assume #3 holds. Then #1 is fulfilled as R2 is non-shareable, P1 and P2 both fulfil #2 so this condition also holds, and #4 is fulfilled by the cycle formed by P1 and P2. Yet no deadlock occurs, since as soon as P3 finishes its piece of R1 can be granted to P1, whose requirements will be fulfilled by then; so, as soon as P1 releases R2, P2 will be able to proceed.
Where is my error? Where is my misunderstanding?