Download presentation
Presentation is loading. Please wait.
Published byLindsey Wilkinson Modified over 6 years ago
1
CSCI 511 Operating Systems Chapter 7 Deadlock
Dr. Frank Li
2
Resources Resources – passive entities needed by processes/threads to do their work CPU time, disk space, memory Types of resources: Preemptable – can take it away e.g. CPU Non-preemptable – must leave it with the thread e.g. Printer, Disk space Exclusive access or sharable e.g. Read-only files are typically sharable Printers are not sharable during time of printing One of the major tasks of an operating system is to manage resources OS keep a system table records whether each resource is free or allocated. Processes request, use, release the resource
3
Revisit: Deadlock in Dining Philosophers Problem
Five chopsticks and five philosophers Recall: using semaphore to guarantee that no two philosophers grab a same chopstick. What if all philosophers grab one chopstick at same time? Deadlock!
4
Conditions for Deadlock
Deadlock doesn’t have to be deterministic! Consider two mutexes x and y: Thread A Thread B x.wait(); y.wait(); y.wait(); x.wait(); Deadlock won’t always happen with this code Have to have exactly the right timing So you release a piece of software, and you tested it, and there it is. Can’t solve deadlock for each resource independently! Why? Since deadlocks occur with multiple resources
5
Four Conditions for Deadlock
Mutual exclusion Only one thread at a time can use a resource. Hold and wait Thread holding at least one resource is waiting to acquire additional resources held by other threads No preemption Resources are released only voluntarily by the thread holding the resource, after thread is finished with it Circular wait There exists a set {T1, …, Tn} of waiting threads T1 is waiting for a resource that is held by T2 T2 is waiting for a resource that is held by T3 … Tn is waiting for a resource that is held by T1 (All 4 conditions must be hold for a deadlock.)
6
Starvation vs. Deadlock
Starvation: thread waits indefinitely e.g., low-priority thread waiting for resources constantly in use by high-priority threads Deadlock: circular waiting for resources e.g., Thread A owns Res 1 and is waiting for Res 2 Thread B owns Res 2 and is waiting for Res 1 Deadlock Starvation, but not vice versa Starvation maybe ends (but doesn’t have to) Deadlock can’t end without external intervention Res 2 Res 1 Thread B A Wait For Owned By
7
Resource-Allocation Graph (1)
Symbols System Model A set of Threads T1, T2, . . . Resource types R1, R2, . . . Each resource type Ri has Wi instances. Each thread utilizes a resource as follows: Request(); Use(); Release(); Resource-Allocation Graph: T = {T1, T2, …, Tn}, the set threads in the system. R = {R1, R2, …, Rm}, the set of resource types in system Request edge: directed edge T1 Rj Assignment edge: directed edge Rj Ti T1 T2 R1 R2
8
Resource-Allocation Graph (2)
Process Resource Type with 4 instances Pi requests instance of Rj (Request edge) Pi is holding an instance of Rj (Assignment edge) Request edge Assignment edge Pi Rj Pi Rj
9
Resource Allocation Graph Examples
Q. Cyclic dependency == deadlock ? T1 T2 T3 R1 R2 R3 R4 T1 T2 T3 R1 R2 R3 R4 T1 T2 T3 R2 R1 T4 Cyclic dependency ?= deadlock No! If graph contains no cycle no deadlock If graph contains cycle(s): a) if each resource type has exactly one instance deadlock b) if each resource type has several instances may or may not
10
Three Methods for Handling Deadlocks
Detect and recover: allow entering deadlock state, detect it and then recover Requires deadlock detection algorithm (details later) Techniques for selectively preempting resources and/or terminating tasks Prevent or avoid deadlock: ensure that system will never enter a deadlock Prevention: ensure at least one of four conditions of deadlock cannot hold; Avoidance: Given information on which resources a process will request and use during its lifetime Consider the resources currently available decide whether a request can be satisfied or must be delayed. Ignore deadlock and pretend that deadlocks never occur in the system used by most operating systems, including UNIX, Win The simplest way What would happen?
11
Method 1: Deadlock Prevention (1)
Deadlock Prevention: Prevent deadlocks by restraining how requests can be made ensure at least one of the necessary conditions for deadlock cannot occur Mutual Exclusion No mutual exclusion If resources are sharable (read-only file) no mutual exclusion Drawbacks: some resources are non-sharable. we cannot prevent deadlock by denying the mutual-exclusion condition. Hold and Wait: Don’t allow waiting 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 (must release all the resources that it’s currently allocated before requesting resources.) Drawbacks: Low resource utilization; possible starvation. (why?)
12
Deadlock Prevention (2)
No Preemption Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it All resources currently being held are released. Alternative: preempt desired resources held by other waiting processes, otherwise waiting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Drawback: applicable for resources whose state can be easily saved and restored (e.g. CPU) not applicable for printer …
13
Deadlock Prevention (3)
Circular Wait prevents any cyclic use of resources Impose a total ordering of all resource types, R = {R1, R2, …, Rn}, and require that each process requests resources in an increasing order of enumeration (Read proof on p255) Recall: two mutexes x and y Thread A Thread B x.wait(); y.wait(); y.wait(); x.wait(); If we require an order: X and then Y e.g. A lock-order verifier Witness in FreeBSD dynamically maintaining the relationship of lock orders in a system. Witness generates a warning message if the locks out of order.
14
Deadlock Avoidance (1) Requires that the system has some additional information available. With the knowledge of the complete sequence of requests and releases for each process, the system can decide for each request whether to grant the resource or to let the process wait A deadlock avoidance algorithm dynamically examines the resource allocation state to ensure that there can never be a circular-wait condition. 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. i.e., when a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state.
15
Safe, Unsafe & Deadlock State
If a system is in safe state no deadlocks. If a system is in unsafe state possibility of deadlock. Deadlock avoidance ensure that a system will never enter an unsafe state.
16
Deadlock Avoidance (3) Safe sequence: 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 requests can be satisfied by currently available resources + resources held by all Pj (j < i) Basic idea: If Pi resource needs are not immediately available, then Pi can wait until all Pj have 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.
17
Banker’s Algorithm Basic idea of Banker’s algorithm Details:
Each process states maximum resource needs in advance maximum credit line Allow particular process to proceed if: (#available resources - #requested) max remaining that might be needed by any process Details: Evaluate each request and grant if some ordering of processes is still deadlock free afterward Keeps system in a safe state, i.e. there exists a safe sequence {P1, P2, … Pn} with P1 requesting all remaining resources, finishing, then P2 requesting all remaining resources, etc.
18
Resource-Request Algorithm
Requesti is request vector for process Pi. For instance, if Requesti [j] = k then process Pi wants k instances of resource type Rj. 1. If Requesti Needi go to step 2. Otherwise, raise error condition. (exceeded its maximum claim.) If Requesti Available, go to step 3. Otherwise Pi must wait. (resources are not available.) 3. Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available - Requesti; Allocationi = Allocationi + Requesti; Needi = Needi – Requesti; If the new state is safe, then the resources are allocated to Pi. If the new state is unsafe , then Pi must wait, and the old resource-allocation state is restored (How does the system decide safe or unsafe?)
19
Method 1: Deadlock Avoidance (4)
Example: 3 process, total 12 tape drives At time To, drives available = 12 – 5 – 2 – 2 = 3 Max needs Current holding Still Need p p p Q1. Is the system in a safe state? Q2. At time T1, if system assigns 1 drive to p2, is the system still in a safe state? Max needs current holding Needs p p p Is it in safe state? Y. safe sequence < p1, p0, p2> If assign 1 tape to p2, is it in safe state? N. 2 free P1 4 free ?!
20
Safety Algorithm Data structures in the algorithm
(n: number of processes; m: number of resource types) 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.
21
Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1, …, n-1 2. Find an i such that both: Finish [i] = false && Needi Work If no such i exists, go to step 4. Work = Work + Allocationi Finish[i] = true go to step 2. If Finish [i] == true for all i, then the system is in a safe state.
22
Example of Banker’s Algorithm
5 processes P0 through P4; 3 resource: types A (10 instances), B (5instances), and C (7 instances). At time T0: Allocation Max A B C A B C P P P P P Q. Is this system currently in a safe state? Available A B C 3 3 2 Need P P P P P safe sequence < P1, P3, P4, P2, P0> satisfies safety criteria. There are maybe multiple safe sequences.
23
Example of Banker’s Algorithm
When P1 Request (1,0,2), invoke Resource-Request Algorithm Check that Request Available, i.e., (1,0,2) (3,3,2) Allocation Need A B C A B C P P P P P Could this request be granted? Is this safe sequence unique? Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted? Available A B C Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement.
24
Another Banker’s Algorithm Example
Using Banker’s algorithm solve dining-philosophers problem Safe state (won’t cause deadlock) if when try to grab a chopstick either: This chopstick is Not the last chopstick OR this chopstick is the last chopstick but someone will have two afterwards
25
Method 2: Deadlock Detection
If a system does NOT employ deadlock prevention or avoidance algorithm, then a deadlock situation may occur. Step 1. Deadlock Detection Wait-for graph: only works for single instance of each resource type Deadlock detection algorithm: a general solution Step 2. Deadlock recovery
26
Wait-for graph If all resources have only a single instance, we can use Wait-for graph to detect deadlock Nodes are processes. Pi Pj if Pi is waiting for Pj. Periodically invoke an algorithm that searches for a cycle in the graph. Q. What is the complexity of an algorithm to detect a cycle in a graph?
27
Method 2: Deadlock Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available For i = 0,1,, …, n-1, if Allocationi 0, then Finish[i] = false;otherwise, Finish[i] = true. 2. Find an index i such that both: Finish[i] == false && Requesti Work If no such i exists, go to step 4. Work = Work + Allocationi Finish[i] = true go to step 2. 4. If Finish[i] == false, for some i, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.
28
Method 2: Deadlock Recovery Schemes
Terminate thread, force it to give up resources In dining philosophers example: force a philosopher put down a chopstick This isn’t always possible: for instance, with a mutex, can’t shoot a thread and leave world inconsistent Preempt resources without killing off thread Take away resources from thread temporarily But this doesn’t always fit with semantics of computation Roll back actions of deadlocked threads Hit the rewind button, pretend last few minutes never happened. A common technique in DBMS (transactions) Of course, if you restart in exactly the same way, may reenter deadlock once again
29
Starvation vs. Deadlock
In Conclusion … Starvation vs. Deadlock Starvation: thread waits indefinitely Deadlock: circular waiting for resources Four conditions for deadlocks Mutual exclusion Hold and wait No preemption Circular wait Techniques for addressing deadlock Detection and recovery Prevention and avoidance Ignore the problem
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.