Day 16 Deadlock Deadlock Processes permanently blocked. Processes are waiting on events, but the events are to be triggered by blocked processes. No simple, efficient solution Can deadlock occur? Process P … Get A … Get B … Release A … Release B …. Process Q … Get B … Get A … Release B … Release A …. Joint process diagram (Fig.6.2) Can deadlock occur? Process P … Get A … Release A … Get B … Release B …. Process Q … Get B … Get A … Release B … Release A …. The occurrence of deadlock depends on relative execution rates of processes details of the application. Types of resources Re-usable – returned to the system after use by a process i.e. not depleted Examples: Consumable resources • • Created (produced) and destroyed (consumed) by processes Examples: Example of deadlock with a consumable resource Process P … Receive (Q) … Send (Q, message1) Process Q … Receive (P) … Send (P, message2) • Deadlock occurs if at some point each is waiting to receive a message from the other, assuming Receive is blocking. • Caused by a design error. Pre-emptible resources Can be taken away from a process without having to terminate the process and can be restored to the process at a later time without altering the behavior of the process. Examples: Cannot cause deadlock Non-pre-emptible resources If the resource is taken away from the process, the process will have to be aborted. The resource must be voluntarily released by the resource. Examples: Resource allocation graphs A directed graph used to represent the current snapshot of a system, i.e. current allocations and pending requests. A circle represents a process A square represents a resource (can have more than one instance of a resource) A graph edge from a resource to a process implies that the resource is allocated to the process A graph edge from a process to a resource implies that the process is requesting an instance of the resource Can be used to determine if a system is deadlocked. Resource allocation graph – example of a deadlocked system (Circular wait) 4 conditions for deadlock Necessary conditions 1. Mutual exclusion must be enforced. 2. No preemption – no resource can be forcibly removed from the process. 3. Hold and Wait – a process may hold a resource while waiting for another. Sufficient condition 4. Circular wait – closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain. Conditions 1-3 create a situation that could result in deadlock Condition 4 indicates that deadlock has occurred. Deadlock handling Prevention Avoidance Detection and Recovery Integrated approach Ostrich approach Prevention Indirect method – prevent any one of the three (1-3) conditions from existing Can’t do without enforcing mutual exclusion. Hold and wait A process must request all its resources initially. If it does not get all of them, then release all and block until all resources simultaneously available. Prevention Indirect method No pre-emption The process may pre-empt the process that is holding a resource that the current process wants. Priority must be used to decide which process should be pre-empted. Also, the resource must be a pre-emptible resource. Prevention Direct method – prevent circular wait Consider a linear ordering of resources. Organize resources into groups that are linearly ordered: G1 , G2 , G3 … Gn. A process can only request a resource from G1 and then from G2 and then from G3 and so on. No deadlock possible here.
© Copyright 2026 Paperzz