470 likes | 796 Views
Chapter 11. Deadlocks. Deadlocks. A deadlock is a situation in which a set of processes face indefinite waits A Deadlock results in reduction of concurrency and parallelism Both response times to applications and system performance suffer. Deadlock: definition.
E N D
Chapter 11 Deadlocks
Deadlocks • A deadlock is a situation in which a set of processes face indefinite waits • A Deadlock results in reduction of concurrency and parallelism • Both response times to applications and system performance suffer
Deadlock: definition • A deadlock involving a set of processes D is a situation in which • Every process Pi in D is blocked on some event ei. • Event ei can be caused only by actions of some process(es) in D.
Deadlocks • Various kinds of deadlocks can arise in an OS, e.g. • Synchronization deadlocks • Processes wait for one another to perform expected actions • Message communication deadlocks • Processes wait for messages from one another • Resource deadlocks • Processes wait for requested resources to be allocated to them • An OS handles only resource deadlocks, if at all
Events related to resource allocation • Resource request • A process requests a resource through a system call • The process is blocked if the resource cannot be allocated to it • Resource allocation to a process • The process becomes a holder of the resource • If the process was blocked, its state is changed to ready • The resource state is changed to `allocated’ • Resource release • A process releases a resource through a system call • The OS allocates the resource to a waiting process, if any • Otherwise, it changes the resource state to `free’
Conditions for resource deadlock(These conditions must hold simultaneously)
Modeling the resource state • We use the term resource state to indicate • State of each resource in the system • Resource allocations and pending resource requests • A resource class contains resources of the same kind • We use two kinds of models • Graph models expect a process to request only one resource of a resource class • Processes and resources are nodes, and allocations and pending requests are edges in a graph • The matrix model does not impose any constraints • Resources, allocations and pending requests are represented in separate matrices
Wait-for graphs (WFG) • A WFG can be used to depict the resource state of a system in which every resource class contains only one resource unit • A Node is a process • An edge is a wait-for relationship between processes • A wait-for edge (Pi, Pj) indicates that • Process Pj holds the resource of a resource class • Process Pi has requested the resource and its request is pending • In essence Pi waits for Pj to release the resource
Resource request and allocation graph (RRAG) • Two kinds of nodes exist in an RRAG • A circle is a process • A rectangle is a resource class • A bullet in a rectangle is a resource unit • Edges can also be of two kinds • An edge from a resource class to a process is a resource allocation • An edge from a process to a resource class is a pending resource request
Paths in WFG and RRAG • A path in a graph is a sequence of edges such that the destination node of an edge is the source node of the subsequent edge • Consider a path P1 – Ri – P2 – Rj – P3 – Rk – P4 This path indicates that • Process P4 has been allocated a resource unit of Rk • Process P3 has been allocated a resource unit of Rj and awaits a resource unit of Rk, etc. Q: Does a deadlock exist? . . .
Paths in WFG and RRAG • A path in a graph is a sequence of edges such that the destination node of an edge is the source node of the subsequent edge • Consider a path P1 – Ri – P2 – Rj – P3 – Rk – P4 This path indicates that • Process P4 has been allocated a resource unit of Rk • Process P3 has been allocated a resource unit of Rj and awaits a resource unit of Rk, etc. Q: Does a deadlock exist? A: No, because resource Rkis allocated to P4, and P4 is not blocked. Hence it may finish using the resource and release Rk. Then P3 can use Rk and so on
An example of a deadlock • A system contains a printer and a tape and two processes, each of which requires both resources Process Pi Process Pj Request a tape Request a printer … … Request a printer Request a tape
Another example • What if the system contained two tapes and a printer and three processes that use these resources as follows: Process Pi Process Pj Process Pk Request a tape Request a printer Request a tape … … … Request a printer Request a tape Q: Do we have a deadlock?
Example (continued) • So what can we conclude from this example? • A cycle in RRAG is a necessary condition for deadlock • A cycle in RRAG is not a sufficient condition for deadlock
Deadlock handling approaches • Deadlock detection and resolution • Kernel periodically analyzes resource state • If a deadlock exists, it aborts some processes and reallocates their resources to other processes • Deadlock prevention • The kernel’s resource allocation policy ensures that the four conditions for deadlock cannot be satisfied simultaneously • Deadlock avoidance • The kernel grants a resource request only if granting it cannot lead to future deadlocks • Other requests are kept pending until they can be granted
Deadlock detection • We use a matrix model to represent resource state • Given a system with r resource classes and n processes: • Allocated resources: n x r matrix • Requested resources: n x r matrix • Free resources: 1-D array with r elements
Deadlock detection • How can we detect deadlocks? • A deadlock implies that • there is no sequence of process completion – resource release – resource allocation events through which all processes can complete. • So the kernel simulates process completion, resource release and resource allocation actions to check whether all processes can complete • A process can be deemed to complete anytime all its present resource requests are granted
Algorithm for deadlock detection • Input two sets of processes---Blocked, Running • Finish, set of finished processes, is initially empty • Perform following steps until Running is empty • Transfer a process from Running to Finished • If all resources requested by a process in Blocked set can be granted, grant the resources and transfer the process to Running • A deadlock exists if Blocked is empty
Deadlock resolution • Abort some processes and distribute their resources among other processes such that there is no deadlock • The aborted processes are called victim processes Q: How to determine victim processes?
Deadlock prevention • Use resource allocation policies that make deadlocks impossible • How to design a deadlock prevention approach? • Consider the conditions for deadlock • Ensure that they cannot hold simultaneously • A simple policy • Allocate all resources required by a process together. Hence the hold-and-wait condition is never satisfied • Expensive in practice as resources would be allocated much before they are actually needed by a process
Resource ranking • Associate numerical ranks with resources • When process Pi requests a resource Rk with rank rk • Abort process Pi if rk is ≤ the rank of some resource already allocated to Pi • Allocate resource Rk if it is available; otherwise, let process Pi wait for resource Rk Q: Can one prove absence of deadlocks when this policy is used?
Deadlock avoidance • Safe resource allocation state: • If the system is in this state, all processes will be able to complete their operation without deadlocks no matter how and when processes request and release their resources in future • Unsafe resource allocation state: • There is a possibility of a deadlock in future • The kernel ensures that the system goes from one safe state to another Q: How to check for future deadlocks?
Deadlock avoidance Q: How to check for future deadlocks? • Ask users to declare the maximum requirement of each resource class by a process • Check whether a process can get the maximum resources of each resource class simultaneously by some sequence of process completion – resource release – resource allocation events • This is done through simulation (analogous to deadlock detection) • A deadlock would arise in future if this is not the case Q: Why?
Allocation state in Banker algorithm for a single resource class
Is this state safe?Would the state be safe if P1 is allocated (1,1,0)?
Characterization of deadlocks • We determine the necessary and sufficient conditions for deadlocks to exist in a system • For this, we classify systems on the basis of the nature of resource classes and resource requests made by processes • Resource classes • Single instance (SI): Resource class has a single resource unit • Multiple instance (MI): Resource class has several resource units • Resource requests • Single request (SR): request for 1 unit of 1 class only • Multiple request (MR): request for 1 unit each of several resource classes
Classification of systems according to resource class and resource request models
SISR systems • What are the necessary and sufficient conditions for a deadlock in SISR systems? • A cycle in a WFG is both a necessary and a sufficient condition Q: Is a cycle in an RRAG necessary and sufficient?
MISR system • A cycle in an RRAG is a necessary but not a sufficient condition for a deadlock in an MISR system • See the example involving three processes and a printer and a tape (slides 15, 16) Q: So, what is a sufficient condition?
MISR system • The RRAG must contain a knot • A subgraph G’ = (N’, E’) of graph G = (N, E) is a knot if • For every node niin N’: All out-edges of ni in G are included in G’ • For every path ni .. nj in G’, a path nj .. ni also exists in G’
SIMR system • A cycle is both a necessary and a sufficient condition for a deadlock to exist in an SIMR system Q: Why?
MIMR system • The RRAG must contain a resource knot • A subgraph G’ = (N’, E’) of graph G = (N, E) is a resource knot if • For every resource nodein N’: All out-edges of ni in G are included in G’ • For every path ni .. nj in G’, a path Nj .. Ni also exists in G’ • Q: Does a deadlock exist in the RRAG of the next slide? (You could complete the out-edge of R3 as you like)
Processes in a deadlock • How to identify all processes that are in a deadlock? • RRi : Set of resource classes requested by Pi • HSk: Set of processes holding resources of resource class Rk • KS : Set of processes in resource knots • AS : Auxiliary set of process nodes AS = { Pi| RRicontains Rk such that HSk is included in (KS Ụ AS) } D = KSỤAS
Deadlock handling in practice • To limit overhead, the kernel uses different deadlock handling policies for different resources • Memory: Swapping is used, hence no deadlocks. • I/O devices: All deadlock handling policies have weaknesses. Hence the kernel creates virtual I/O devices for allocation • Files and messages: Deadlock handling is not implemented • Control blocks in the kernel: Resource ranking could be used