How to Manually Detect Deadlocks in Algorithms

algorithms

I understand the concepts of deadlock well enough, but when I'm given a problem like the one below I'm not sure how to go about solving it. I can draw a resource allocation graph, but I'm not sure how to solve it from there.

Is there a better more formal way of solving this?

Consider  a  system  with  five  processes,  P1  through P5,  and  five 
resources, R1 through R5. Resource ownership is as follows. 

•  P1 holds R1 and wants R3 
•  P2 holds R2 and wants R1 
•  P3 holds R3 and wants R5 
•  P4 holds R5 and wants R2 
•  P5 holds R4 and wants R2 

Is this system deadlocked? Justify your answer. If  the system is deadlocked, list 
the involved processes. 

Best Answer

Yes it's a deadlock, diagram the wait chain of processes (1 -> 2 indicates P1 waiting on P2 to release a resource):

1 -> 3 -> 4 -> 2 -> 1

Ran back into 1 in the wait chain and the cycle is complete; that is 1 is waiting on resources that are waiting on resources that are waiting on 1.

If following 1 like this didn't run back into itself then it would be accurate to say 1 is not in a deadlock (for instance if it was 1->3->4->2). However if one were not in a deadlock that does not prove or indicate none of the others are in dead locks. To verify none of the resources are in a deadlock you would need to graph the same chain with any nodes that weren't in the critical path for 1 (if any in the critical path were in a deadlock then 1 would be, so you know all members of it's dependency chain are not in deadlocks). Since 5 isn't in the critical path you would have to next follow 5's path if 1 wasn't in a deadlock (incidentally 5 is also in a deadlock because it links into the same cycle 1 is in, therefore all listed resources are deadlocked in that cycle)

Another point regarding this particular problem is that all resources available (the set of R1-R5) are already acquired. In such a scenario it is impossible for any process to acquire another resource if no processes are willing to first let go of a resource. A cycle is inevitable in such a scenario. This fact that you should release resources before requesting more is I think supposed to be the lesson of the 73.4 philosophers problem (don't quote me on that)

Related Topic