1 / 46

Deadlocks

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.

naoko
Télécharger la présentation

Deadlocks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11 Deadlocks

  2. 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

  3. 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.

  4. 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

  5. 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’

  6. Conditions for resource deadlock(These conditions must hold simultaneously)

  7. 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

  8. 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

  9. 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

  10. Examples of RRAGs and WFGs

  11. 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? . . .

  12. 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

  13. 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

  14. RRAG for the system

  15. 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?

  16. RRAG after all requests of the example are made

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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?

  23. Deadlock resolution

  24. 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

  25. Approaches to deadlock prevention

  26. 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?

  27. Deadlock prevention using resource ranking

  28. 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?

  29. 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?

  30. Allocation state in Banker algorithm for a single resource class

  31. Operation of Banker's algorithm for multiple resources

  32. Can P1 or P2 be allocated (1,0)?

  33. Is this state safe?Would the state be safe if P1 is allocated (1,1,0)?

  34. 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

  35. Classification of systems according to resource class and resource request models

  36. 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?

  37. 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?

  38. 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’

  39. A knot in RRAG of an MISR system

  40. SIMR system • A cycle is both a necessary and a sufficient condition for a deadlock to exist in an SIMR system Q: Why?

  41. Deadlocks in SIMR systems

  42. 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)

  43. RRAG for an MIMR system

  44. 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

  45. Processes in deadlock

  46. 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

More Related