Presentation is loading. Please wait.

Presentation is loading. Please wait.

Avishai Wool lecture 5 - 1 Introduction to Systems Programming Lecture 5 Deadlocks.

Similar presentations


Presentation on theme: "Avishai Wool lecture 5 - 1 Introduction to Systems Programming Lecture 5 Deadlocks."— Presentation transcript:

1

2 Avishai Wool lecture 5 - 1 Introduction to Systems Programming Lecture 5 Deadlocks

3 Avishai Wool lecture 5 - 2 Example of Deadlock Dining Philosophers problem “pick left, pick right, eat” algorithm With some bad luck: –All “pick left”, –All stuck on “pick right” This is called Deadlock

4 Avishai Wool lecture 5 - 3 When Do Deadlocks Occur Deadlocks occur when … –processes are granted exclusive access to devices –we refer to these devices generally as resources

5 Avishai Wool lecture 5 - 4 Resources Examples of computer resources –Printers –Tape drives –Entries in OS tables (like PCB entries) –Memory –Database records Processes need access to resources in reasonable order Access restricted via: critical sections / mutex / semaphore

6 Avishai Wool lecture 5 - 5 Resources Usage Pattern Sequence of events required to use a resource 1.request the resource 2.use the resource 3.release the resource Must wait if request is denied –requesting process may be blocked –may fail with error code

7 Avishai Wool lecture 5 - 6 Introduction to 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 the release of a currently held resource. None of the processes can … –run –release resources –be awakened

8 Avishai Wool lecture 5 - 7 Four Conditions for Deadlock 1.Mutual exclusion condition each resource assigned to 1 process or is available 2.Hold-and-wait condition process holding resources can request additional resources 3.No preemption condition previously granted resources cannot forcibly taken away 4.Circular wait condition must be a circular chain of 2 or more processes each is waiting for resource held by next member of the chain

9 Avishai Wool lecture 5 - 8 Strategies for Dealing with Deadlock 1.Just ignore the problem altogether. 2.Detection and recovery. 3.Avoidance careful resource allocation. 4.Prevention negating one of the four necessary conditions.

10 Avishai Wool lecture 5 - 9 Deadlock Detection and Recovery

11 Avishai Wool lecture 5 - 10 Modeling Deadlock with Graphs Process == circle, Resource == square –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

12 Avishai Wool lecture 5 - 11 A B C Using Resource Allocation Graph

13 Avishai Wool lecture 5 - 12 How deadlock can be avoided (o) (p) (q) Scheduling B is delayed

14 Avishai Wool lecture 5 - 13 Detection with One Resource of Each Type Note the resource ownership and requests A cycle can be found within the graph, denoting deadlock

15 Avishai Wool lecture 5 - 14 Detection with Multiple Resource of Each Type Data structures needed by deadlock detection algorithm

16 Avishai Wool lecture 5 - 15 Deadlock Detection: overview Algorithm knows resources, requests Needs to determine whether there exists a scheduling order such that all requests can be honored. Algorithm is not (necessarily) responsible for scheduling.

17 Avishai Wool lecture 5 - 16 Deadlock Detection Algorithm For vectors X,Y we say X ≤ Y if X i ≤ Y i for all i. 1.Set all processes as “unmarked” Loop: 2.Find unmarked process i with R i* ≤ A // i can run 3.If found: A  A + C i* ; “mark” process i; // assume i has run, make its resources available 4.Else: If all processes are marked: Success 5. Else: System is deadlocked

18 Avishai Wool lecture 5 - 17 Example of Deadlock Detection Algorithm After process 3 runs: A = ( 2, 2, 2, 0) After process 2 runs: A = ( 4, 2, 2, 1) X X

19 Avishai Wool lecture 5 - 18 Recovery from Deadlock (1) 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

20 Avishai Wool lecture 5 - 19 Recovery from Deadlock (2) 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 None of the deadlock recovery options is very attractive.

21 Avishai Wool lecture 5 - 20 Deadlock Avoidance

22 Avishai Wool lecture 5 - 21 Deadlock Avoidance Can we make decisions one at a time and always avoid deadlock? Need some advance knowledge: –Each process has to “declare”, in advance, what resources, and what is the maximum number of resource units, it will need

23 Avishai Wool lecture 5 - 22 Safe and Unsafe States The “State” of the system includes –How many resources are allocated to each process –What is the maximum number of resources a process could request –How many are available A state is Safe if –It is not deadlocked –There exists a scheduling order in which all processes run to completion.

24 Avishai Wool lecture 5 - 23 The state in (a) is safe (a) (b) (c) (d) (e) Only one resource, 10 copies exist We could schedule B After B terminates we could Schedule C

25 Avishai Wool lecture 5 - 24 The state in (b) is not safe (a) (b) (c) (d) Schedule B

26 Avishai Wool lecture 5 - 25 Unsafe != Deadlocked If state is safe  deadlock can be avoided. If state is unsafe  not clear –We are making “worst case” assumptions, processes could use less than their maximum

27 Avishai Wool lecture 5 - 26 Banker’s Algorithm (Dijkstra, 1965) When process requests resource X, 1.Assume the process gets X Loop: 2.Find unmarked process i with R i* ≤ A // i can run 3.If found: A  A + C i* ; mark process i as terminated. // assume i has run, make its resources available 4.Else: System is deadlocked, giving the resource was unsafe

28 Avishai Wool lecture 5 - 27 The Banker's Algorithm for a Single Resource Three resource allocation states –safe –unsafe (a) (b) (c)

29 Avishai Wool lecture 5 - 28 Is Deadlock Avoidance Practical? Banker’s algorithm needs information about future needs (max values for each resource). In general OS does not have this information. Number of processes varies. Resources can “disappear”.

30 Avishai Wool lecture 5 - 29 Deadlock Prevention

31 Avishai Wool lecture 5 - 30 Idea: invalidate one of the four conditions for deadlock 1.Mutual exclusion condition 2.Hold-and-wait condition 3.No preemption condition 4.Circular wait condition

32 Avishai Wool lecture 5 - 31 Attacking the Mutual Exclusion Condition 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 Principle: –avoid assigning resource when not absolutely necessary –as few processes as possible actually claim the resource

33 Avishai Wool lecture 5 - 32 Attacking the Hold and Wait Condition Require processes to request resources before starting –a process never has to wait for what it needs Problems –may not know required resources at start of run –also ties up resources other processes could be using Variation: before requesting a new resource, –process must give up all resources –then request all immediately needed

34 Avishai Wool lecture 5 - 33 Attacking the No Preemption Condition In general this is not a viable option Consider a process given the printer –halfway through its job –now forcibly take away printer –!!??

35 Avishai Wool lecture 5 - 34 Attacking the Circular Wait Condition Every resource has a unique number A process must request resources in increasing number order

36 Avishai Wool lecture 5 - 35 Requesting in Order Prevents Deadlock 2 resources (i > j), 2 processes A, B Deadlock can only occur if : –A holds i and requests j, –B holds j and requests i. But if they requested resources in order, after A holds i it is not allowed to request j! It should have asked for j earlier (and become blocked).

37 Avishai Wool lecture 5 - 36 Summary of deadlock prevention All these options are very restrictive, and not very practical

38 Avishai Wool lecture 5 - 37 The Ostrich Algorithm 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 General-purpose OSs do not do much towards deadlock detection, recovery, avoidance, or prevention.

39 Avishai Wool lecture 5 - 38 A Database example A database maintains many tables. A transaction might update records in many tables. A transaction has to be atomic: –either all records are updated, or none are.

40 Avishai Wool lecture 5 - 39 Example transfer $1000 from account A to account B Read A A  A – 1000 Write A Read B B  B + 1000 Write B

41 Avishai Wool lecture 5 - 40 Need to Avoid Inconsistencies Lock records before accessing to them –otherwise race conditions possible. Database software (Oracle, Sybase, IBM DB2…) provides record locking mechanism. Deadlock is certainly possible.

42 Avishai Wool lecture 5 - 41 Two-Phase Locking Phase One –A transaction locks all records it needs –(no real work done in phase one) Phase Two –performing updates –Issuing “commit” –releasing locks

43 Avishai Wool lecture 5 - 42 Properties of Two-phase locking Deadlock detection by a per-transaction timeout. –Has occasional “false positives”, when disk response time is bad. Deadlock recovery by aborting transactions –An aborted transaction releases all its locks

44 Avishai Wool lecture 5 - 43 Why can this work in DB? Unlike general OS, a transaction knows which records it will need in advance. A transaction can be aborted (rolled back) safely. Rollback capability needed for other reasons –Power outage, program crash, may leave DB inconsistent.

45 Avishai Wool lecture 5 - 44 Concepts for review Deadlock Conditions for deadlock Graph models of deadlock Deadlock detection algorithms Deadlock avoidance: Safe/Unsafe states Banker’s algorithm Deadlock prevention Spooling Database transaction 2-phase locking


Download ppt "Avishai Wool lecture 5 - 1 Introduction to Systems Programming Lecture 5 Deadlocks."

Similar presentations


Ads by Google