1 / 59

IOS103 OPERATING SYSTEM DEADLOCKS

IOS103 OPERATING SYSTEM DEADLOCKS. Objectives. At the end of the course, the student should be able to: Define deadlock; Discuss how the deadlock occurs; Discuss how to prevent deadlock; Discuss how to avoid deadlock; Discuss how to detect deadlock; Discuss how to recover from deadlock.

yves
Download Presentation

IOS103 OPERATING SYSTEM 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. IOS103 OPERATING SYSTEM DEADLOCKS

  2. Objectives • At the end of the course, the student should be able to: • Define deadlock; • Discuss how the deadlock occurs; • Discuss how to prevent deadlock; • Discuss how to avoid deadlock; • Discuss how to detect deadlock; • Discuss how to recover from deadlock.

  3. Deadlocks Introduction • In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources such as memory space, CPU cycles, files and I/O devices, if the resources are not available at that time, the process enters a waiting state. • Sometimes, a waiting process is never again able to change state because the resources it has requested are held by other waiting processes, this is called deadlock.

  4. Deadlocks Introduction • A system consists of a finite number of resources to be distributed among a number of competing processes. The resources are partitioned into several types, each consisting of some number of identical instances. • If a process requests an instance of a resource type, the allocation of any instance of the type will satisfy the request. If it will not, then the instances are not identical and the resource type classes have not been defined properly.

  5. Deadlocks Introduction • A process must request a resource before using it and must release the resource after using it. A process may request as many resources as it requires to carry out its designated task. Obviously, the number of resources requested may not exceed the total number of resources available in the system. In other words, a process cannot request three printers if the system has only two.

  6. Deadlocks • Under the normal mode of operation, a process may utilize a resource in only the following sequence: • Request: The process requests the resource. If the request cannot be granted immediately, then the requesting process must wait until it can acquire the resource. • Use: The process can operate on the resource (for example, if the resource is a printer, the process can print on the printer). • Release: The process releases the resource.

  7. Deadlocks • To illustrate deadlock state: • Consider a system with three CD RW drives. Suppose each of three processes holds one of these CD RW drives. If each process now requests another drive, the processes will be in deadlock state. (The same resource type) • Consider a system with one printer and one DVD drive. Suppose that process PI is holding the DVD and process PJ is holding the printer. If PI requests the printer and PJ requests the DVD drive, a deadlock occurs. (Different resource type)

  8. Deadlocks • Deadlock can arise if four conditions hold simultaneously: • Mutual exclusion: 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.

  9. Deadlocks • Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that 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 Pn is waiting for a resource that is held by P0

  10. Resource- Allocation Graph • Deadlocks can be described more precisely in terms of a directed graph called a system resource-allocation graph. This graph consists of a set of vertices V and a set of edges E. The set of vertices V is partitioned into two different types of nodes: • P = {P1, P2, …, Pn}, the set consisting of all the processes in the system • R = {R1, R2, …, Rm}, the set consisting of all resource types in the system

  11. Resource- Allocation Graph • A directed edge from process Pi to resource type Rj is denoted by Pi→Rj; it signifies that process Pi has requested an instance of resource type Rj and is currently waiting for that resource. A directed edge from resource type Rj to process Pi is denoted by Rj→Pi; it signifies that an instance of resource type Rj has beed allocated to process Pi. • Request edge – directed edge Pi→Rj • Assignment edge – directed edge Rj→Pi

  12. Resource- Allocation Graph • Pictorially, we represent: • Process: a circle • Resource Type with 4 instances: rectangle with dots • Pirequests instance of Rj • Pi is holding an instance of Rj Pi Rj Pi Rj

  13. Resource- Allocation Graph • Example: • The sets P, R and E: • P = { P1, P2, P3 } • R = { R1, R2, R3, R4} • E = {P1 → R1 , P2 → R3 , R1 → P2 , • R2 → P2 , R2 → P1 , R3 → P3 } • Resource instances: • One instance of resource type R1 • Two instances of resource type R2 • One instance of resource type R3 • Three instances of resource type R4

  14. Resource- Allocation Graph • Process states: • Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1. • Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3. • Process P3 is holding an instance of R3.

  15. Resource- Allocation Graph • If the graph contains no cycles, then no process in the system is deadlocked. If the graph does contain a cycle, then a deadlock may exist. • If each resource type has several instances, then a cycle does not necessarily imply that a deadlock has occurred. In this case, a cycle in the graph is a necessary but not a sufficient condition for the existence of deadlock.

  16. Resource- Allocation Graph • Let’s use the previous graph: • Suppose that process P3 requests an instance of resource type R2. Since no resource instance is currently available, a request edge P3→R2 is added to the graph. At this point, two minimal cycles exist in the system. • P1→R1→P2 →R3 →P3 →R2 →P1 • P2→R3→P3 →R2 →P2

  17. Resource- Allocation Graph • Therefore: • Processes P1, P2 and P3 are deadlocked. Process P2 is waiting for the resource R3, which is held by process P3. Process P3 is waiting for either process P1 or process P2 to release resource R2. In addition, process P1 is waiting for process P2 to release resource R1.

  18. Resource- Allocation Graph • Consider: • P1→R1→P3 →R2 →P1 • However, there is no deadlock. Observe that process P4 may release its instance of resource type R2. That resource can then be allocated to P3, breaking the cycle.

  19. Resource- Allocation Graph • In summary, if a resource-allocation graph does not have a cycle, then the system is not in a deadlocked state. If there is a cycle, then the system may or may not be in deadlocked state. This observation is important when we deal with the deadlock problem.

  20. Resource- Allocation Graph • Generally: • If graph contains no cycles no deadlock • If graph contains a cycle: • If only one instance per resource type, then deadlock • If several instances per resource type, possibility of deadlock

  21. Methods for Handling Deadlocks • We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlocked state. • We can allow the system to enter a deadlocked state, detect it and recover. • We can ignore the problem altogether and pretend that deadlocks never occur in the system.

  22. Deadlock Prevention Mutual Exclusion • This condition must hold for non-sharable resources. (Example: a printer cannot be simultaneously shared by several processes.) • Sharable resources, in contrast, do not require mutually exclusive access and thus cannot be involved in a deadlock. (Example: read-only files.) • In general, however, we cannot prevent deadlocks by denying the mutual-exclusion condition, because some resources are intrinsically non-sharable.

  23. Deadlock Prevention Hold and Wait • Must guarantee that whenever a process requests a resource, it does not hold any other resources. • Require process to request and be allocated all its resources before it begins execution or allow process to request resources only when the process has none. • Example1: A process that copies data from DVD drive to a file on disk, sorts the file and then prints the results to a printer. (Request first the DVD drive, disk file and printer.)

  24. Deadlock Prevention Hold and Wait • Example 2: Request the DVD drive and disk file then copy the file from DVD drive. Then request the disk file and printer then prints the file from disk file. • Disadvantages: • Resource utilization may be low. Since the resources were requested and waited to be used. • Starvation. Other processes will wait indefinitely for resources being used.

  25. Deadlock Prevention No Preemption • There be no preemption of resources that have already been allocated. • If a process is holding some resources and requests another resource that cannot be immediately allocated to it (the process must wait), then all resources the process is currently holding are preempted. • The preempted resources are added to the list of resources for which the process is waiting. • The process will be restarted only when it can regain its old resources as well as the new ones that is requesting.

  26. Deadlock Prevention No Preemption • This is often applied to resources whose state can be easily saved and restored later such as CPU registers and memory space. • It cannot generally be applied to such resources as printers and tape drives.

  27. Deadlock Prevention Circular Wait • This is to impose a total ordering of all resource types and to require that each process requests resources in an increasing order of enumeration. • Example: F(tape drive) = 1 • F(disk drive) = 5 • F(printer) = 12 • In the example, requests first the tape drive before the printer. In developing the ordering or hierarchy, it is up to application developers based on the normal order of usage of the resources.

  28. Deadlock Avoidance • An alternative method for avoiding deadlocks is to require additional information about how resources are to be requested. • The simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. Given this a priori information, it is possible to construct an algorithm that ensures that the system will never enter a deadlocked state. Such an algorithm defines the deadlock-avoidance approach.

  29. Deadlock Avoidance • A deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that a circular-wait condition can never exist. • The resource-allocation state is defined by the number of available and allocated resources and the maximum demands of the processes.

  30. Deadlock Avoidance Safe State • A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock. • More formally, a system is in safe state only if there exists a safe sequence. • When a process requests an available resources, system must decide if immediate allocation leaves the system in a safe state.

  31. Deadlock Avoidance Safe State • System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes is the systems such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i

  32. Deadlock Avoidance Safe State • That is: • If Pi resource needs are not immediately available, then Pi can wait until all Pjhave finished • When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate • When Pi terminates, Pi +1 can obtain its needed resources, and so on

  33. Deadlock Avoidance Safe State • If a system is in safe state = no deadlocks • If a system is in unsafe state = possibility of deadlocks • Avoidance = ensure that a system will never enter an unsafe state

  34. Deadlock Avoidance Safe State • Example: A system with 12 magnetic tape drives and 3 processes. • Max. Needs Current Needs • P0 10 5 • P1 4 2 • P2 9 2 • A t0, P0 holding 5, P1 holding 2 and P2 holding 2. So 3 free tape drives. Therefore, Safe State. • A t1, P2 requests 1 tape drive (total of 3), P0 requests 5 tape drives (but only 2 available) and P1 will wait. Therefore, Unsafe State.

  35. Deadlock Avoidance Resource-Allocation-Graph Algorithm • Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line • Claim edge converts to request edge when a process requests a resource • Request edge converted to an assignment edge when the resource is allocated to the process • When a resource is released by a process, assignment edge reconverts to a claim edge • Resources must be claimed a priori in the system • This is applicable for single instance of resource type.

  36. Deadlock Avoidance Resource-Allocation-Graph Algorithm • If no cycle exists, then the allocation of the resource will leave the system in a safe state.

  37. Deadlock Avoidance Resource-Allocation-Graph Algorithm • If a cycle is found, then the allocation will put the system in an unsafe state. In that case, process Pi will have to wait for its requests to be satisfied.

  38. Deadlock Avoidance Banker’s Algorithm • This algorithm applicable for multiple instances of a resource type. • The name was chosen because the algorithm could be used in a banking system to ensure that the bank never allocated its available cash in such a way that it could no longer satisfy the needs of all its customers. • Each process must declare the maximum number of instances of each resource type that may need.

  39. Deadlock Avoidance Banker’s Algorithm • Data Structures for the Banker’s Algorithm: • Let:n =no. of processes & m=number of resources types • Available: Vector of length m. If available [j] = k, there are k instances of resource type Rjavailable • Max: n x m matrix. If Max [i,j] = k, then process Pimay request at most k instances of resource type Rj • Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj • Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rjto complete its task Need [i,j] = Max[i,j] – Allocation [i,j]

  40. Deadlock Avoidance Banker’s Algorithm Data Structures for the Banker’s Algorithm: Example: Let X and Y be vectors of length n. And X ≤ Y if and only if X[i] ≤ Y[i] for all i=1, 2,…n. If X=(1, 7, 3, 2) and Y=(0, 3, 2, 1) then Y ≤ X. In addition, Y < X if Y ≤ X and Y ≠ X

  41. Deadlock Avoidance Banker’s Algorithm • Safety Algorithm • Let Work and Finish be vectors of length m and n, respectively. Initialize: • Work = Available & Finish [i] = false for i = 0, 1, …, n- 1 • Find and i such that both: (a) Finish [i] = false (b) Needi Work If no such i exists, go to step 4 • Work = Work + AllocationiFinish[i] = truego to step 2 • If Finish [i] == true for all i, then the system is in a safe state • This require an order of m x n2 operations to determine whether a state is safe.

  42. Deadlock Avoidance Banker’s Algorithm • Resource-Request Algorithm Request = request vector for process Pi. If Requesti[j] = k then process Pi wants k instances of resource type Rj 1. If Requesti Needigo to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim 2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available 3. Pretend to allocate requested resources to Pi by modifying the state as follows: • Available = Available – Request; • Allocationi= Allocationi + Requesti; • Needi= Needi – Requesti; • If safe  the resources are allocated to Pi • If unsafe  Pi must wait, and the old resource-allocation state is restored

  43. Deadlock Avoidance Banker’s Algorithm • Examples: • 5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances) Snapshot at time T0: AllocationMaxAvailable A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3

  44. Deadlock Avoidance Banker’s Algorithm • Example: Then • The content of the matrix Need is defined to be Max – Allocation Need A B C P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 • The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria

  45. Deadlock Avoidance Banker’s Algorithm • Example: What is P1 requests (1,0,2) • Check that Request  Available (that is, (1,0,2)  (3,3,2) =true AllocationNeedAvailable A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 1 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 • Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety requirement • Can request for (3,3,0) by P4 be granted? No • Can request for (0,2,0) by P0 be granted? No

  46. Deadlock Detection • If a system does not employ either a deadlock-prevention or a deadlock-avoidance algorithm, then a deadlock may occur. • Therefore the system may provide an algorithm that examines the state of the system to determine whether a deadlock has occurred. • And an algorithm to recover from the deadlock.

  47. Deadlock Detection Single Instance of Each Resource Type • Maintain wait-for graph • Nodes are processes • Pi Pjif Piis waiting forPj • Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock • An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph

  48. Deadlock Detection Single Instance of Each Resource Type Resource-Allocation Graph Corresponding wait-for graph

  49. Deadlock Detection Several Instances of a Resource Type • The wait-for graph scheme is not applicable to a resource-allocation system with multiple instances of each resource type. Therefore: similar to banker’s algorithm. • Available: A vector of length m indicates the number of available resources of each type. • Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. • Request: An n x m matrix indicates the current request of each process. If Request [ij] = k, then process Pi is requesting k more instances of resource type. Rj.

  50. Deadlock Detection Several Instances of a Resource Type • Example1: • Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b) For i = 1,2, …, n, if Allocationi 0, then Finish[i] = false;otherwise, Finish[i] = true • Find an index i such that both: (a) Finish[i] == false • Requesti Work If no such i exists, go to step 4

More Related