<<

UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department Deadlock: Why does it happen? CS 537 Andrea C. Arpaci-Dusseau Introduction to Operating Systems Remzi H. Arpaci-Dusseau Informal: Every entity is waiting for resource held by another entity; none release until it gets what it is Deadlock waiting for

Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided? How can deadlock be detected and recovered from?

Deadlock Example Deadlock Example

Two threads access two shared variables, A and B int A, B; Variable A is protected by x, variable B by lock y lock_t x, y; How to add lock and unlock statements? 1 Thread 2 int A, B; lock(x); lock(y); A += 10; B += 10; lock(y); lock(x); Thread 1 Thread 2 B += 20; A += 20; A += 10; B += 10; A += B; A += B; B += 20; A += 20; unlock(y); unlock(x); A += B; A += B; A += 30; B += 30; A += 30; B += 30; unlock(x); unlock(y);

What can go wrong??

1 Representing Deadlock Conditions for Deadlock

Two common ways of representing deadlock • Vertices: • Resource can not be shared – Threads (or processes) in system – Resources (anything of value, including locks and semaphores) • Requests are delayed until resource is released • Edges: Indicate thread is waiting for the other Hold-and-wait Wait-For Graph Resource-Allocation Graph • Thread holds one resource while waits for another No “waiting for” wants y held by • Resources are released voluntarily after completion T1 T2 T1 T2 Circular wait • Circular dependencies exist in “waits-for” or “resource- “waiting for” x held by wants allocation” graphs ALL four conditions MUST hold

Handing Deadlock Deadlock Prevention #1

Deadlock prevention Approach • Ensure deadlock does not happen • Ensure 1 of 4 conditions cannot occur • Ensure at least one of 4 conditions does not occur Deadlock avoidance • Negate each of the 4 conditions • Ensure deadlock does not happen No single approach is appropriate (or possible) for • Use information about resource requests to dynamically avoid unsafe situations all circumstances Deadlock detection and recovery • Allow deadlocks, but detect when occur No mutual exclusion --> Make resource sharable • Recover and continue Ignore • Example: Read-only files • Easiest and most common approach

2 Deadlock Prevention #2 Deadlock Prevention #2

No Hold-and-wait --> Two possibilities No Hold-and-wait 1) Only request resources when have none • Release resource before requesting next one 2) Atomically acquire all resources at once Thread 1 Thread 2 • Example #1: Single lock to protect all lock(x); lock(y); Thread 1 Thread 2 A += 10; B += 10; lock(z); lock(z); unlock(x); unlock(y); A += 10; B += 10; lock(y); lock(x); B += 20; A += 20; B += 20; A += 20; A += B; A += B; unlock(y); unlock(x); A += 30; B += 30; lock(x); lock(y); unlock(z); unlock(z); A += 30; B += 30; unlock(x); unlock(y);

Deadlock Prevention #2 Deadlock Prevention #2

No Hold-and-wait Problems w/ acquiring many resources atomically • Low resource utilization 2) Atomically acquire all resources at once – Must make pessimistic assumptions about resource usage • Example #2: New primitive to acquire two locks if (cond1) { lock(x); Thread 1 Thread 2 } lock(x,y); lock(x,y); if (cond2) { lock(y); A += 10; B += 10; } B += 20; A += 20; • Starvation A += B; A += B; – If need many resources, others might keep getting one of unlock(y); unlock(x); them A += 30; B += 30; unlock(x); unlock(y);

3 Deadlock Prevention #3 Deadlock Prevention #4

No “no preemption” --> Preempt resources No circular wait --> Impose ordering on resources Example: A waiting for something held by B, then take • Give all resources a ranking; must acquire highest resource away from B and give to A ranked first • Only works for some resources (e.g., CPU and • How to change Example? memory) • Not possible if resource cannot be saved and restored – Can’t take away a lock without causing problems

Problems?

Deadlock Avoidance Banker’s Example Scenario: • Three processes, P0, P1, and P2 Dijkstra’s Banker’s Algorithm • Five available resources, N=5 Avoid unsafe states of processes holding • Each may need maximum of 4 resources simultaneously Not safe example: P0 has 2, P1 has 1, P2 has 1 resources • Why could this lead to deadlock? • Unsafe states might lead to deadlock if processes • Implication: Avoid this state, allow only states with enough resources left to satisfy claim of at least 1 process make certain future requests • Claim: Maximum need - currently loaned to this process • When process requests resource, only give if doesn’t Example: cause unsafe state • P0 requests: Allow? • P1 requests: Allow? • Problem: Requires processes to specify all possible • P2 requests: Allow? future resource demands • P0 requests: Allow? • P1 requests: Allow? • P0 requests: Allow? • P0 releases 2 • Allow any others now?

4 Deadlock Detection and Recovery

Detection • Maintain wait-for graph of requests • Run algorithm looking for cycles – When should algorithm be run? Recovery: Terminate deadlock • Reboot system (Abort all processes) • Abort all deadlocked processes • Abort one process in cycle Challenges • How to take resource away from process? Undo effects of process (e.g., removing money from account) – Must roll-back state to safe state (checkpoint memory of job) • Could starve process if repeatedly abort it

5