Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deadlock Management.

Similar presentations


Presentation on theme: "Deadlock Management."— Presentation transcript:

1 Deadlock Management

2 Topics Introduction to Deadlocks Deadlock Management Approaches
Deadlock Acceptance (Ostrich Algorithm) Deadlock Prevention Deadlock Detection and Recovery Deadlock Avoidance (Banker Algorithm) Other issues

3 Introduction to Deadlocks
Deadlock definition: Contributing factors to deadlocks: Concurrent processing Needed for high performance and utilization Competition over limited resources Examples of applications that have deadlock: Operating systems, Database systems, Distributed Systems, … Deadlock is a situation where a group of processes are permanently blocked waiting for the resources held by each other in the group.

4 System Model Concurrent processes P1, P2, …,Pn
Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices, printers Each resource type Ri has Wi instances Each process utilizes a resource as follows: Request Use Release Represent the relationships between processes and resources as a graph

5 Resource-Allocation Graph
A set of vertices V and a set of edges E. V is partitioned into two types: Processes = {P1, P2, …, Pn} Resource types = {R1, R2, …, Rm} E is partitioned into two types: Request edge – directed edge Pi Rj Assignment edge – directed edge Rj Pi

6 Resource-Allocation Graph (Cont’d)
Pi Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Rj Rj Pi Rj Pi

7 Graph Construction A B C

8 Deadlock Characterization
Four necessarily and sufficient conditions 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 Circular wait: Every process Pi is waiting for a resource that is held by P(i+1 mod n), 1≤ i < n P1 P2 P3 Pn-1

9 Examples Deadlock Cycle Cycle, No Deadlock Cycles do NOT necessarily imply deadlock unless … there is ONLY one instance of each resource type

10 Topics Introduction to Deadlocks Deadlock Management Approaches
Deadlock Acceptance (Ostrich Algorithm) Deadlock Prevention Deadlock Detection and Recovery Deadlock Avoidance (Banker Algorithm) Other issues

11 Deadlock Management Approaches
Ignore the problem altogether (Ostrich Algorithm) Prevent deadlocks from happening Negate one of the necessary conditions Allow deadlocks to happen Detect and recover if happened Allow deadlocks to happen (Banker’s Algorithm) Dynamic avoidance with careful resource assignment

12 1- The Ostrich Algorithm
Pretend there is no problem Reasonable if: Deadlocks occur very rarely Cost of prevention is high UNIX and Windows takes this approach It is a trade off between: Convenience & Correctness Not for real-time or critical systems

13 Take out any of the four necessarily conditions
2- Deadlock Prevention Take out any of the four necessarily conditions 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 Circular wait: Every process Pi is waiting for a resource that is held by P(i+1 mod n), 1≤ i < n

14 2- Deadlock Prevention (Breaking Mutual Exclusion)
Not easy to break because it is a resource characteristic Not required for sharable resources (E.g. File opened for read) Must hold for non-sharable resources (E.g., tape drivers, printers, …) Some devices can be spooled (requests added to buffer) Deadlock is eliminated for spooled resources, e.g. printers Problems: Not all devices can be spooled Buffer

15 2- Deadlock Prevention (Breaking Hold and Wait)
Must guarantee that whenever a process requests a resource, it does not hold any other resources Request and be allocated all its resources before it begins execution Allow process to request resources only when the process has none Problems: May not know the required resources at the beginning Low resource utilization Indefinite postponement (Starvation)

16 2- Deadlock Prevention (Breaking No Preemption)
If a process that is holding some resources requests another resource that cannot be immediately allocated All resources currently being held are released Preempted resources are added to the list of resources for which the process is waiting Process will be restarted only when it can regain its old resources, as well as the new ones Problems: May not be a viable solution (process is killed) Try to Stop/Resume the process (very expensive, if feasible)

17 2- Deadlock Prevention (Breaking Circular Wait)
Impose a total ordering of all resource types Require each process to request resources in order (increasing or descending) Problems: May not know in advance all the required resources Low resource utilization

18 Resource Ordering: Example
B C 1- R 2- S 3- T Illegal request

19 Summary of Deadlock Prevention

20 Topics Introduction to Deadlocks Deadlock Management Approaches
Deadlock Acceptance (Ostrich Algorithm) Deadlock Prevention Deadlock Detection and Recovery Deadlock Avoidance (Banker Algorithm) Other issues

21 3- Deadlock Detection and Recovery
Allow the system to enter a deadlock state Periodically run a deadlock detection algorithm Recover if deadlock is detected

22 3- Deadlock Detection and Recovery Detection with One Instance of Each Resource Type
Check the Resource-Allocation Graph If a cycle exists  A deadlock exists

23 3- Deadlock Detection and Recovery
Recovery through preemption Take a resource from one process Recovery through rollback Checkpoint processes periodically Restart one process from its last check point Recovery through killing processes Simplest way to break a deadlock Free its resources for other processes to complete -- Assign priorities -- How long a process has computed? How much longer remains? -- Resources already allocated to it? Resources still needed? -- Random selection -- Good scheduling (E.g., Ageing) Common Problems 1- Selecting a victim 2- Starvation

24 4- Deadlock Avoidance Requires that the system has some additional a priori information available in advance Requires that each process declare the maximum number of resources of each type that it may need The algorithm dynamically examines the resource-allocation state Prevents the circular-wait condition Resource-allocation state is defined by: The number of available resources The number allocated resources The maximum demands of the processes

25 Safe State When a process requests a resource, system decides if immediate allocation leaves the system in a safe state System is in safe state if there exists a safe sequence of all processes Sequence <P1, P2, …, Pn> is safe if: 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 If Pi resource-needs are not immediately available, then Pi waits until all Pj have finished, with j<i 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…

26 Safe, Unsafe , Deadlock State

27 Resource-Allocation Graph
Claim edge Pi  Rj indicate that process Pi may request resource Rj Claim edge converts to request edge when a process actually requests a resource Resources must be claimed a priori in the system

28 Banker’s Algorithm (Key Features)
Multiple instances of a resource type Each process must a priori claim maximum use (claim edges) 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 Moves the system from one safe state to another safe state

29 Banker’s Algorithm: Data Structures
Let n = number of processes, and m = number of resources types. Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available Max: n x m matrix. If Max [i,j] = k, then process Pi may 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 Rj to complete its task. Need [i,j] = Max[i,j] – Allocation [i,j]

30 Safety Algorithm (Tested with each request)
1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false, for i={1,3, …, n} 2. Find process i such that both: (a) Finish [i] = false (b) Needi  Work If no such i exists, go to step 4. 3. Work = Work + Allocationi Finish[i] = true go to step 2. 4. If Finish [i] == true for all i, then the system is in a Safe state, otherwise, Unsafe state. Loop over all processes and make sure they can finish Vector matching Pretend that process i finishes and returns its resources

31 Resource-Request Algorithm for Process Pi
-Request = request vector for process Pi -If Requesti [j] = k then process Pi wants k instances of Rj 1. If Requesti  Needi Go to step 2. Else Raise error since Pi has exceeded its maximum claim. 2. If Requesti  Available Go to step 3. Pi must wait, since resources are not available. 3. Pretend to allocate requested resources to Pi: Available = Available - Requesti; 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 Change the system state

32 Safety Algorithm: Example
5 processes P0 through P4 3 resource types A, B, C A(10 instances), B (5 instances), and C (7 instances) Snapshot at time T0: Allocation Max Available A B C A B C A B C P P P P P System is in a safe state because < P1, P3, P4, P2, P0> is a safe sequence Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Max - Allocation

33 Request Algorithm: Example P1 Request (1,0,2)
Check that Request  Available (that is, (1,0,2)  (3,3,2)  true) Allocation Need Available A B C A B C A B C P P P P P Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement. Exercise: Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?

34 Topics Introduction to Deadlocks Deadlock Management Approaches
Deadlock Acceptance (Ostrich Algorithm) Deadlock Prevention Deadlock Detection and Recovery Deadlock Avoidance (Banker Algorithm) Other issues

35 Starvation Starvation definition Main cause is bad scheduling
Example: Driving license branch takes the shortest request first Solution is good scheduling First-come, first-serve policy Priorities + Aging A process may wait indefinitely to complete without being part of a deadlock

36 Two-Phase Locking in DBMS
Two-phase locking is a concept of database systems Concurrency control Phase 1(Expanding phase): collect all locks a transaction needs Phase 2 (Shrinking phase): start releasing the locks Notices the similarity with “Requesting all resources at once” **Not the same** T1 T2 x y z T1, T2 -- Database Transactions x, y, z -- Database values deadlock wait

37 Topics Introduction to Deadlocks Deadlock Management Approaches
Deadlock Acceptance (Ostrich Algorithm) Deadlock Prevention Deadlock Detection and Recovery Deadlock Avoidance (Banker Algorithm) Other issues

38 Summary Deadlock definition: Deadlock management:
Ignore the problem altogether (Ostrich Algorithm) Prevent deadlocks from happening Negate one of the necessary conditions Allow deadlocks to happen Detect and recover if happened Dynamic avoidance with careful resource assignment Deadlock is a situation where a group of processes are permanently blocked waiting for the resources held by each other in the group.


Download ppt "Deadlock Management."

Similar presentations


Ads by Google