1 / 45

Deadlock – Part 2

(Lecture 7a). Deadlock – Part 2. CS5002 Operating Systems Dr. Jose M. Garrido. Resource Allocation and De-allocation. When a process needs resources, it will normally follow the sequence:

Download Presentation

Deadlock – Part 2

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. (Lecture 7a) Deadlock – Part 2 CS5002 Operating SystemsDr. Jose M. Garrido

  2. Resource Allocation and De-allocation When a process needs resources, it will normally follow the sequence: • Request a number of instances of one or more resource types. If the resource units requested are not available, then process must wait • Acquire the resource(s), the OS allocates resources • Use the resource(s) for a finite period. • Release the resource(s).

  3. Synchronizing Allocation of Resources Semaphores can be used to synchronize the allocation and de-allocation of resources.

  4. Multiple Resource Allocation Problem • Several processes may compete for multiple resources • If the allocation of multiple resources to processes is not done with proper synchronization, deadlock might occur • This waiting state is much more involved than starvation

  5. Deadlock Problem • A set of blocked (suspended) processes • Each process holding one or more resources • Processes waiting to acquire one or more resources held by other processes

  6. Example of Deadlock • A system has two tape drives, which processes P1 and P2 need • Processes P1 and P2 each acquire one tape drive and each wait for the other tape drive • Processes P1 and P2 are blocked waiting indefinitely to acquire the other tape unit

  7. Deadlock – General Definition • A set of processes is in a deadlock state when every processes in the set is waiting for an event that can only be caused by another process in the set. • The events of interest are: • allocations of resources • release of resources • The resources are: CPU cycles, memory spaces, files, I/O devices in the computer system.

  8. Disadvantage Of Deadlock • Deadlocked processes can never complete execution because they are waiting indefinitely • System resources are tied up because they are held by deadlocked processes. • Deadlocks are undesirable because they normally slow down a system

  9. Another Example Of Deadlock • Consider a system with one printer and one disk unit. Processes P and Q both need the two resources • Suppose that process P is holding the printer, and process Q is holding the disk unit. Now P waits for the disk unit and Q waits for the printer. • Process P is waiting for process Q to release the disk unit, and process Q is waiting for process P to release the printer.

  10. Result Both processes are deadlocked, they cannot proceed.

  11. Conditions for Deadlock Conditions For Deadlock: A deadlock arises if four conditions hold simultaneously in a system: • 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.

  12. Conditions for Deadlock (Cont.) • No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its operations. 4. 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, but P1 is waiting for a resource held by P2, …, Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0.

  13. Deadlock Conditions These conditions are necessary but not sufficient for deadlock to occur.

  14. Resource Allocation Graph A System Deadlock Model. A process-resource graph shows the relationships between processes and resources. It consists of the following components: • A set of processes • A set of resources • A set of directed edges

  15. Resource Allocation Graph • a set of processes P = {P0, P1, …., Pi,…., Pn} • a set of resource types, R = {R0, R1, …,Rj, ..,Rm} • a set of directed edges • A request edge (Pi, Rj) from process Pi to resource Rj. Thus, Pi is waiting for an instance of resource type Rj. • An assignment edge (Rj, Pi) from resource Rj to process Pi. an instance of Rj has been allocated to process Pi.

  16. A Graph with no Cycles R1 R3 P1 P3 P2 R2 R2

  17. Graph with a Cycle R1 R3 P3 P1 P2 R2 R2

  18. Analysis Of This Graph • This graph has two minimal cycles • Processes p1, p2, and p3 are deadlocked.

  19. Another Graph R3 R1 P1 P3 P2 R2 R4

  20. Analysis Of Resource Allocation Graph • If the graph contains no cycles, no deadlock exists. • If the graph contains cycles • if each resource type has only 1 instance, then a deadlock exits. • otherwise, a deadlock may exist.

  21. Methods For Dealing With Deadlocks • Deadlock prevention: disallow the existence of one of the four necessary conditions for deadlocks to occur. • Deadlock avoidance: for each resources request which can be satisfied, determine whether the request should be delayed in order to avoid a possible deadlock in the future. • Deadlock detection and recovery: detect the existence of deadlock; if it has occurred, take actions to remove it.

  22. Deadlock Prevention • Non-existence of the Hold and wait condition • Non-existence of the Circular Wait condition

  23. Deadlock Prevention (2) Non-existence of hold and wait condition • Allocation only if all resources requested are available • Allocation of resources only when there is no previous allocation. • Low resource utilization • Starvation possible

  24. Deadlock Prevention (3) Non-existence of the Circular Wait condition • impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

  25. Disallow Circular Wait • Impose a total ordering of all resource types, R = {R1, R2, …, Rm}. • Define a one to one function, F: R  N • Require that each process requests resources in an increasing order of enumeration. • Process P can request resources of type Ri, then resources of type Rj, only if: F( Rj) > F( Ri)

  26. Example of Linear Ordering of Resources • A system has the following resources types: R = {Disk, Tape, Printer} • The following ordering is defined for the resources with function F: F(Disk) = 1; F(Tape) = 3; F(Printer) = 7 • A process, P, has to request first an instance of Disk, then an instance of Tape, then an instance of Printer.

  27. Problems For Deadlock Prevention • Low device utilization • Reduced system throughput Why?

  28. Deadlock Avoidance Requires that the system has some additional a priori information available • Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. • The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. • The system is always kept in a safe state .

  29. Resource Allocation State Resource-allocation state is defined by the • Number of available resources • Number of allocated resources • Maximum resource demands of the processes.

  30. 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 • A system is in a safe state only if there exits a safe sequence of allocations

  31. Banker’s Algorithm • Allows multiple instances of a resource type • Each process must a priori claim maximum use. • The OS must determine whether the allocation of resources will leave the system in a safe state • If it will, the resources are allocated, otherwise, the process has to wait. • When a process gets all its resources it must return them in a finite amount of time.

  32. Data Structures for the Algorithm • 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]. n = number of processes, m = number of resources types.

  33. Example 1 Bankers Algorithm Processes: P0, P1, P2; resources: 12 units of magnetic tape (R1) State 1: PROCESS MAX_NEED ALLOCATION P0 10 5 P1 4 2 P2 9 2 Resources available in this state: 3 units.

  34. Safe State? State 1 is a safe state because a safe sequence: <P1, P0, P2> exists.

  35. Example (Cont.) Assume that when in state 1, process P2 requests And is allocated 1 more unit of tape, Process Max_need Allocation P0 10 5 P1 4 2 P2 9 3

  36. Safe State? This state, state 2 is unsafe, no safe sequence can be found.

  37. Example 2 Banker’s Algorithm There are 5 processes P0 through P4; 3 resource types A (10 instances), B (5 instances), and C (7 instances). System state at time T0: AllocationMax Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 3 3 2 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1

  38. Safe State? • The system is in a safe state since the sequence: < P1, P3, P4, P2, P0> found satisfies safety criteria • There is a safe sequence of allocating resources to the processes and avoiding deadlock

  39. Example 2 • Suppose that in the initial state P1 requests (1, 0, 2) more • Check that Request  Available 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 • Is the state safe?

  40. Deadlock Detection • Allow system to enter deadlock state • Run detection algorithm • Carry out recovery scheme

  41. Detection-Algorithm Usage • When, and how often, to invoke the detection check depends on: • How often a deadlock is likely to occur? • How many processes will need to be rolled back? (one for each disjoint cycle) • If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

  42. Recovery: Process Termination • Abort all deadlocked processes. • Abort one process at a time until the deadlock cycle is eliminated. • In which order should we choose to abort? • Priority of the process. • How long process has computed, and how much longer to completion. • Resources the process has used. • Resources process needs to complete. • How many processes will need to be terminated. • Is process interactive or batch?

  43. Recovery: Resource Preemption • Selecting a victim – minimize cost. • Rollback – return to some safe state, restart process from that state. • Starvation – same process may always be picked as victim, include number of rollback in cost factor.

  44. Selection Criteria • Least amount of CPU service received • Least amount of output • Most estimated remaining time • Least total resources allocated so far • Lowest priority

  45. Combined Approach to Deadlock • Combine the three basic approaches • prevention • avoidance • detection allowing the use of the optimal approach for each of resources in the system. • Partition resources into hierarchically ordered classes. • Use most appropriate technique for handling deadlocks within each class.

More Related