Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity.

Similar presentations


Presentation on theme: "11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity."— Presentation transcript:

1 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2 22 Content Resource Deadlocks Deadlock modeling Deadlock detection and recovery Deadlock avoidance and prevention

3 33 Resources Something needed by a thread A thread waits for resources Examples of computer resources –Printers, tape drives, tables, –locks, disk space, memory, CPU

4 44 Resources Processes need access to resources –in reasonable order Preemptable resources –can be taken away from a process with no ill effects Nonpreemptable resources –will cause the process to fail if taken away

5 55 Resources Suppose a process holds resource A –and requests resource B At same time another process holds B –and requests A both are blocked and remain so –Deadlock!

6 66 Resources Sequence of events required to use a resource 1.request the resource 2.use the resource 3.release the resource

7 77 Resources Must wait if request is denied –requesting process may be blocked –may fail with error code

8 88 Deadlocks A circular waiting for resources –leading to threads involved not being able to make progress Deadlocks occur when … –processes are granted exclusive access to resources

9 99 Deadlocks Formal definition: A set of processes is deadlocked if: –each process in the set is waiting for an event that only another process in the set can cause Usually the event is release of a held resource

10 10 Deadlocks In a deadlock, none of the processes can … –run –release resources –be awakened

11 11 Deadlocks General structure of thread code –phase 1. while (not done) { – acquire some resources – work –}–} –phase 2. release all resources Assume phase 1 has finite amount of work

12 12 Deadlocks Example thread A thread B lock(x) lock(y) lock(y) lock(x)... unlock(y) unlock(x) unlock(x) unlock(y)

13 13 Deadlocks Can deadlock occur with code? Will deadlock always occur with this code?

14 14 Dining Philosophers Problem

15 15 Dining Philosophers 5 philosophers sitting around a round table 1 chopstick in between each pair of philosophers –5 chopsticks total Each philosopher needs two chopsticks to eat How to prevent deadlock

16 16 Dining Philosophers Algorithm for each philosopher –wait for chopstick on right to be free, then pick it up –wait for chopstick on left to be free, then pick it up –eat –put both chopsticks down Can this deadlock?

17 17 Conditions for Deadlock 4 conditions must all be true for deadlock to occur Limited resource: –not enough resources to serve all threads simultaneously Hold and wait: –threads hold resources while waiting to acquire others

18 18 Conditions for Deadlock No preemption –can t force thread to give up resource Circular chain of requests Resource BResource A Thread A Thread B

19 19 Four Conditions for Deadlock The first condition is sometimes called Mutual exclusion condition –each resource assigned to 1 process or is available Tanebaum, etc.

20 20 Deadlock Modeling Modeled with directed graphs

21 21 Deadlock Modeling Resource R assigned to process A Process B is requesting/waiting for resource S Process C and D are in deadlock –over resources T and U

22 22 Deadlock Modeling

23 23 (o) (p) (q) How deadlock can be avoided

24 24 Strategies for dealing with Deadlocks Ignore: just ignore the problem altogether Detection and recovery Dynamic avoidance: careful resource allocation Prevention: –negating one of the four necessary conditions

25 25 Ignore Strategy Pretend there is no problem Reasonable if –deadlocks occur very rarely –cost of prevention is high UNIX and Windows takes this approach a trade off between convenience & correctness

26 26 Detect and Fix Note the resource ownership and requests Detect by looking for cycles in the wait-for graph How to fix once detected?

27 27 Detection with Wait-for Graph

28 28 Detection with Wait-for Graph

29 29 An example for the deadlock detection algorithm Detection with Wait-for Graph

30 30 Recovery from Deadlock Recovery through preemption –take a resource from some other process –depends on nature of the resource Recovery through rollback –checkpoint a process periodically –use this saved state –restart the process if it is found deadlocked

31 31 Recovery from Deadlock Recovery through killing processes –crudest but simplest way to break a deadlock –kill one of the processes in the deadlock cycle –the other processes get its resources –choose process that can be rerun from the beginning

32 32 Deadlock Avoidance Don t get in a position where –Deadlock becomes possible Use: –Resource Trajectories

33 33 Safe and Unsafe States Demonstration that the state in (a) is safe A39 B24 C27 Has Max (a) Free: 3 A39 B44 C27 Has Max (b) Free: 1 A39 B0 C27 Has Max (c) Free: 5 A39 B0 C77 Has Max (d) Free: 0

34 34 Safe and Unsafe States Demonstration that the state in b is not safe A39 B24 C27 Has Max (a) Free: 3 A49 B24 C27 Has Max (b) Free: 2 A49 B44 C27 Has Max (c) Free: 0 A49 B C27 Has Max (d) Free: 4

35 35 Deadlock Prevention Idea is to eliminate one of the four necessary conditions Increase resources to decrease waiting –this minimizes chance of deadlock

36 36 Deadlock Prevention Attacking the Mutual Exclusion Condition Attacking the Hold and Wait Condition Attacking the No Preemption Condition Attacking the Circular Wait Condition

37 37 Attacking Mutual Exclusion Some devices (such as printer) can be spooled –only the printer daemon uses printer resource –thus deadlock for printer eliminated Not all devices can be spooled

38 38 Attacking Mutual Exclusion Principle: Avoid assigning resource when not absolutely necessary As few processes as possible actually claim the resource

39 39 Attacking Hold & Wait Condition Processes to request resources before starting –a process never has to wait for what it needs Eliminate hold and wait

40 40 Attacking Hold & Wait Condition phase 1a. acquire all resources phase 1b. while (not done) { acquire some resource work } phase 2. release all resources

41 41 Attacking Hold & Wait Condition A. wait until all resources you ll need are free, then grab them all at once (or) B. if you find resource busy, release all acquired resources and go back to beginning

42 42 Attacking Hold & Wait Condition Problems? May not know required resources at start of run Ties up resources others could be using

43 43 Attacking Hold & Wait Condition Variation: Process must give up all resources Then request all immediately needed

44 44 Attacking No Preemption Condition Allow Preemption! Can preempt CPU by saving its state to thread control block and resuming later Can preempt memory by swapping memory out to disk and loading it back later Can we preempt the holding of a lock?

45 45 Attacking No Preemption Condition Some resource cannot be preempted Consider a process given the printer –halfway through its job –now forcibly take away printer –!!??

46 46 Attacking Circular Wait Condition Normally ordered resources –Imagesetter scanner plotter printer A resource graph

47 47 Banker s Algorithm Derive from methods by bankers to grant loans Similar to reserving all resources at beginning –but more efficient

48 48 Banker s Algorithm State maximum resource needs in advance –but don t actually acquire the resources When thread later tries to acquire a resource, Banker s algorithm determines: –when it s safe to satisfy the request –and blocks the thread when it s not safe

49 49 Banker s Algorithm General structure of thread code –phase 1a. state maximum resource needed –phase 1b. while (not done) { – acquire some resources – work – } –phase 2. release all resources

50 50 Banker s Algorithm Preventing deadlock by requesting all resources at beginning would block thread in phase 1a above –but phase 1b can proceed without waiting

51 51 Banker s Algorithm Phase 1a provides information needed to determine –when it s safe to satisfy each resource request in phase 1b Safe means guaranteeing the ability for all threads to finish –no possibility of deadlock

52 52 Banker s Algorithm Example: use banker s algorithm to model a bank loaning money to its customers

53 53 Banker s Algorithm Bank has $6000 Customers sign up with bank and establish a credit limit (maximum resource needed) They borrow money in stages (up to credit limit) When they re done, they return all the money

54 54 Solution #1 Bank gives money when requested –as long as the money is available –Bank must reserve all resources when customer starts Ann asks for credit limit of $2000 Bob asks for credit limit of $4000 Charlie asks for credit limit of $6000

55 55 Banker s Algorithm Can bank approve all these credit lines –if it promises to give money upon request if available?

56 56 Solution #2: Banker s Algorithm bank approves all credit limits but customer may have to wait –when actually asking for the money

57 57 Banker s Algorithm Ann asks for credit limit of $2000 (bank oks) Bob asks for credit limit of $4000 (bank oks) Charlie asks for credit limit of $6000 (bank oks) Ann takes out $1000 (bank has $5000 left) Bob takes out $2000 (bank has $3000 left) Charlie wants to take out $2000. Is this allowed?

58 58 Banker s Algorithm Allow iff, after giving the money, there exists some sequential order of fulfilling all maximum resources –worst-case analysis

59 59 Banker s Algorithm If give $2000 to Charlie, bank will have $1000 left Ann can finish even if she takes out her max –i.e. another $1000 When Ann finishes, she returns her money –bank will have $2000

60 60 Banker s Algorithm After Ann finishes, Bob can take out his max –another $2000, then finish Then Charlie can finish –even if he takes out his max (another $4000)

61 61 Banker s Algorithm What about this scenario? –Ann asks for credit limit of $2000 (bank oks) –Bob asks for credit limit of $4000 (bank oks) –Charlie asks for credit limit of $6000 (bank oks) –Ann takes out $1000 (bank has $5000 left) –Bob takes out $2000 (bank has $3000 left) –Charlie wants to take out $2500. Is this allowed?

62 62 Banker s Algorithm Banker allows system to over commit resources without introducing the possibility of deadlock Sum of max resource needs of all current threads can be greater than total resources as long as there s some way for the all the threads to finish without getting into deadlock.

63 63 Banker s Algorithm How to apply banker s algorithm to dining philosophers? Unfortunately, it s difficult to anticipate maximum resources needed

64 64 Three resource allocation states A06 B05 C04 D07 Has Max (a) Free: 10 A16 B15 C24 D47 Has Max (b) Free: 2 A16 B25 C24 D47 Has Max (c) Free: 1 Banker's Algorithm for a Single Resource

65 65 Banker's Algorithm for Multiple Resources

66 66 Summary of Deadlock Prevention Mutual Exclusion: spool everything Hold and wait: request all resources initially No preemption: takes resource away Circular wait: order resource numerically

67 67 Other Issues Two-Phase Locking Nonresource Deadlocks Starvation

68 68 Two-Phase Locking Phase One –process tries to lock all records it needs, one at a time –if needed record found locked, start over –(no real work done in phase one)

69 69 Two-Phase Locking If phase one succeeds, it starts second phase, –performing updates –releasing locks

70 70 Two-Phase Locking Note similarity to requesting all resources at once Algorithm works where programmer can arrange – program can be stopped, restarted

71 71 Nonresource Deadlocks Possible for two processes to deadlock –each is waiting for the other to do some task Can happen with semaphores –each process required to do a down() on two semaphores (mutex and another) –if done in wrong order, deadlock results

72 72 Starvation Algorithm to allocate a resource –may be to give to shortest job first Works great for multiple short jobs in a system May cause long job to be postponed indefinitely –even though not blocked Solution: –First-come, first-serve policy

73 73 Lets return to dining philosophers problem Approach: –Attacking the Hold and Wait Condition –Attacking the Circular Wait Condition Solution to Dinning Philosopher Problem

74 74 A Solution with Semaphore #define N 5/*# of philosophers*/ #define LEFT(i+N-1)%N/*# of Is left neighbor*/ #define RIGHT(i+1)%N/*# of Is right neighbor*/ #define THINKING0 #define HUNGRY1 #define EATING2 typedef int semaphore; int state[N];/*array to keep track of everyones state*/ semaphore mutex=1; semaphore s[N];/*one semaphore per philosopher*/

75 75 A Solution with Semaphore void philosopher(int i) { while(TRUE) { think(); take_forks(i);/*acquire two forks or block*/ eat(); put_forks(i);/*put both forks on the table*/ } }

76 76 A Solution with Semaphore void take_forks(int i) { down(&mutex); state[i]=HUNGRY; test(i);/*try to acquire 2 forks*/ up(&mutex); down(&s[i]);/*block if forks were not acquired*/ }

77 77 A Solution with Semaphore void put_forks(i) { down(&mutex); state[i]=THINKING; test(LEFT);/*see if left neighbor can eat*/ test(RIGHT); /*see if right neighbor can eat*/ up(&mutex); }

78 78 A Solution with Semaphore void test(i) { if(state[i]==HUNGRY && state[LEFT]!=EATING && state[RIGHT]!=EATING) { state[i]=EATING; up(&s[i]); } }

79 Computer Changes Life


Download ppt "11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity."

Similar presentations


Ads by Google