1 / 29

Chapter 2(sec 2.5) deadlocks

Chapter 2(sec 2.5) deadlocks. Outline. System Model Deadlock Characterization Methods for handling deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock. System Model.

yoko
Download Presentation

Chapter 2(sec 2.5) 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 2(sec 2.5) deadlocks

  2. Outline • System Model • Deadlock Characterization • Methods for handling deadlocks • Deadlock Prevention • Deadlock Avoidance • Deadlock Detection • Recovery from Deadlock

  3. System Model • A system contains a finite number of resource types (R1, R2, . . ., Rm)to be distributed among competing processes • The resource types are partitioned in to several types (e.g files, I/O devices, CPU cycles, memory), each having a number of identical instances • A process mustrequest a resource before using it and releaseit after making use of it. Each process utilizes a resource as follows: • Request • A process requests for an instance of a resource type. If the resource is free, the request will begranted. Otherwise the process should wait until it acquires the resource • Use • The process uses the resource for its operations • Release • The process releases the resource • Resource • commodity required by a process to execute

  4. Definitions • Resources can be of several types • Serially Reusable Resources • CPU cycles, memory space, I/O devices, files • acquire -> use -> release • Consumable Resources • Produced by a process • needed by a process • create ->acquire ->use • Resource ceases to exist after it has been used • The set of blocked processes each hold a resource and wait to acquire another resource held by other process in the set. • A process is deadlocked if it is waiting for an event that will never occur. • Example 1 • Suppose a system has 2 disk drives • If P1 is holding disk 2 and P2 is holding disk 1 and if P1 requests for disk 1 and P2 requests for disk 2, then deadlock occurs The Deadlock Problem

  5. Deadlock Example(Traffic Case)

  6. Conditions for Deadlock Deadlock can arise if four conditions hold simultaneously in a system: • No Mutual exclusion: if not only one process at a time can use a resource. • Hold and wait:a process holding at least one resource is waiting to acquire additional resources held by other processes. • No preemption:a resource can be released only voluntarily by the process holding it, after that process has completed its task. • Circular wait:A circular chain of processes exists in which each process waits for one or more resources held by the next process in the chain. • P0 is waiting for a resource that is held by P1, • P1 is waiting for a resource that is held by P2, …, • Pn–1 is waiting for a resource that is held by Pn, and • Pnis waiting for a resource that is held by P0

  7. Example of Resource Allocation Graph(RAG)

  8. Graph with no cycles R1 R2 Is Deadlock possible? Ans. No. Explain why? P3 P1 P2 R3 R4

  9. Graph with cycles Is there a Deadlock? Ans. No why explain/ justify it. R1 P2 P1 P3 R2 P4

  10. Graph with cycles and Deadlock Basic facts • If graph contains no cycles • NO DEADLOCK • If graph contains a cycle • if only one instance per resource type, then there is deadlock. • if several instances per resource type, possibility of deadlock. R1 R2 P1 P3 P2 Deadlock? Ans. NO why? R3 R4

  11. Methods for handling deadlocks • Deadlock problems can be handled in one of the following 3 ways: • Ensure that the system will never enter a deadlock state. • Allow the system to potentially enter a deadlock state, detect it and then recover from it. • Ignore the problem and pretend that deadlocks never occur in the system; Deadlock Prevention • Mutual Exclusion– • Hold and Wait – guarantee that when a process requests a resource, it does not hold other resources. • Two protocols are used : • Require a process to request and be allocated all its resources before it begins execution • Allow a process to request resources only when the process has none(starvation )

  12. Deadlock Avoidance • 3. No Preemption • If a process is not allocated the resources it requested it should release at hand. • 4. Circular Wait • Impose a total ordering of all resource types • Require that processes request resources in increasing order of enumeration; • Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need in advance • Having this full information about the sequence of requests and release of resources, we can know whether or not the system is entering unsafe/safe state. • A state is safe if the system can allocate resources to each process in some order avoiding a deadlock. A deadlock state is an unsafe state.

  13. Deadlock avoidance: Safe State Basic Facts • If a system is in a safe state, then there areno deadlocks. • If a system is in unsafe state, then there is a possibility of deadlock • Deadlock avoidance method ensures that a system will never enter an unsafe state

  14. Deadlock Avoidance Algorithms • Based on the concept of safe state, we can define algorithms that ensures the system will never enter a deadlock. • If there is a single instance of a resource type, • Use a resource-allocation graph • If there are multiple instances of a resource type, • Use the Dijkstra’s banker’s algorithm

  15. Deadlock Avoidance Algorithms (contd.)Resource-Allocation Graph Algorithm • Suppose that process Pi requests a resource Rj • The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph • suppose P1 is granted R1 .If both p1 and P2 requests R2.,which process should be allocated R2 to avoid cycle formation? • Ans. P1.

  16. Deadlock Avoidance Algorithms (contd.) Resource-Allocation Graph algorithm • If P1 requests for R2 and P2 requests for R1, then deadlock will occur Unsafe State Resource-Allocation

  17. Deadlock Avoidance Algorithms(contd) Banker’s Algorithm • Used for multiple instances of each resource type. • Each process must a priori claim maximum use of each resource type. • The number may not exceed the total number of resource types in the system • When a process requests a resource it may have to wait. • When a process gets all its resources it must return them in a finite amount of time.

  18. Deadlock Avoidance Algorithms (contd.)Banker’s Algorithm • The Banker’s Algorithm is as follows: • Process pi makes requests for resources. Let Request(i) be the corresponding request vector. So, if pi wants k instances of resource type rj, then Request(i)[j] = k • If Request(i) !≤ Need(i), there is an error. • Otherwise, if Request(i) !≤ Available, then pi must wait • Otherwise, Modify the data structures as follows : • Available = Available - Request(i) • Allocation(i) = Allocation(i) + Request(i) • Need(i) = Need(i) - Request(i) • Check whether the resulting state is safe. • If the state is safe, do the allocation. Otherwise, pi must wait for Request(i)

  19. Deadlock Management • Prevention • Design the system in such a way that deadlocks can never occur • Avoidance • Impose less stringent (strict) conditions than for prevention, allowing the possibility of deadlock but sidestepping it as it occurs. • Detection • Allow possibility of deadlock, determine if deadlock has occurred and which processes and resources are involved. • Recovery • After detection, clear the problem, allow processes to complete and resources to be reused. May involve destroying and restarting processes.

  20. Deadlock Detection • If a system does not implement either deadlock prevention or avoidance, deadlock may occur. Hence the system must provide: 1. A deadlock detection algorithm that examines the state of the system if there is an occurrence of deadlock . 2. An algorithm to recover from the deadlock.

  21. Deadlock Detection:1. Single Instance of Each Resource Type • If there are single instances of each resources in a system, then an algorithm that uses a type of resourceallocationgraph called wait-for graph will be used • The wait-for graph is obtained from the resource allocation graph by removing the resource nodes and collapsing the corresponding edges • To detect deadlocks, the system needs to periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock.

  22. Deadlock Detection:1. Single Instance of Each Resource Type (contd.) Deadlock? Resource-Allocation Graph Corresponding wait-for graph

  23. Deadlock Detection (contd.)2. Several Instances of a Resource Type • When there are multiple instances of a resource type in a resource allocation system, the wait-for graph is not applicable. Hence, a deadlock detection algorithm is used • The algorithm uses several data structures similar to the ones in banker’s algorithm

  24. Recovery from Deadlock • Once a deadlock has been detected, recovery strategy is needed. There are two possible recovery approaches: • Process termination • Resource preemption • Process Termination • Abort all deadlocked processes • Abort one process at a time until the deadlock cycle is eliminated • In which order should we choose a process to abort? Chose the process with • Least amount of processor time consumed so far • Least amount of output produced so far • Most estimated time remaining • Least total resources allocated so far • Lowest priority

  25. Recovery from Deadlock(contd) • Resource Preemption • In this recovery strategy, we successively preempt resources and allocate them to another process until the deadlock is broken • While implementing this strategy, there are three issues to be considered • Selecting a victim – which resources and process should be selected to minimize cost just like in process termination. The cost factors may include parameters like the number of resources a deadlocked process is holding, number of resources it used so far • Rollback – if a resource is preempted from a process, then it can not continue its normal execution • The process must be rolled back to some safe state and started • Starvation– same process may always be picked as victim several times. As a result, starvation may occur. The best solution to this problem is to only allow a process to be picked as a victim for a limited finite number of times. This can be done by including the number of rollback in the cost factor

  26. Exercises For each of the resource allocation graphs below, determine whether there is a deadlock or not. Give explanations for each of your answers. b. a. c. d.

  27. Exercises There are four processes in a system and they are going to share nine tape drives. Their current and maximum number of allocation numbers are as follows : Is the system in a safe state? Why or why not? Is the system deadlocked? Why or why not? Explain if the system in the given resource allocation graph here is deadlocked. If not, give an execution order of the processes which successfully terminates

  28. Exercises Apply the deadlock detection algorithm and either indicate why the system is deadlocked, or specify a safe allocation sequence b. If the process P2 also request 2 instances of resource r1, does the system enter a deadlock? Why? • Given the resource allocation graph below: Consider the following snapshot of a system: • Use the banker’s algorithm to answer the following questions: • What is the content of the matrix Need? • Is the system in a safe state? • If a request from process P1 arrives for (0,4,2,0), can the request be granted immediately?

  29. END OF CHAPTER 2(sec 2.5) Deadlocks Thank you!

More Related