Consider a computer with five individual resources name R1 …. R5. Let five processes P1, …. P5 make requests in order, as follows:
i. P1 requests R2
ii. P4 requests R3
iii. P3 requests R1
iv. P2 requests R4
v. P5 requests R5
vi. P4 requests R2
vii. P5 requests R3
viii. P3 requests R5
Assume Process P_i gets R_i if R_i is currently available.
Is there deadlock and if so at what point did it occur and which processes did it involve?
Can anyone please help me out? For the first one I was thinking there’s no deadlock, but I’m not sure how to prove.
Thanks!
Assuming that’s the actual sequence of events, there is no deadlock there. Initially, all resources are free, Running that code in sequence:
So the current state is:
and the chain of waits is (waiter -> blocker):
or:
Because there is no cycle in there, deadlock has not happened.
Further proof can be obtained by simply having the non-waiters releasing their resources and following the chain of events:
Final state after those steps is that:
Now there are no blockages and each thread can simply release whatever resources they still have allocated.
Now, were you to expand your question to ask if deadlock were possible if those operations could happen in any order (while maintaining order within each thread), the answer is still no.
It’s a basic tenet of mult-threading that you can avoid deadlock by ensuring all your resources are allocated in the same order in every thread. From the operations you gave, the individual threads allocate their resources as follows (order must be maintained within a thread):
So, how can we ensure they’re all allocating in the same sequence? We just need to find a sequence that matches. First, we start with the above list but add spaces so that like resources are lined up and no resource is on both sides of another resource:
And there’s your sequence. Every thread is allocating resources in the order 4, 1, 5, 3, 2. Not every thread allocates every resource but that’s irrelevant here.
That’s also not the only solution (R4 is stand-alone so it could go anywhere in the list – every other resource is involved in a single dependency chain (1,5,3,2) so their relative positions are fixed).
However, it’s sufficient to prove that every thread is allocating the resources in a specific order, hence deadlock is impossible.