1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money.
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Tanenbaum Ch 6 Silberschatz Ch 7
1 CSE 380 Computer Operating Systems Instructor: Insup Lee and Dianna Xu University of Pennsylvania, Fall 2003 Lecture Note: Deadlocks.
Chapter 3 Deadlocks - Αδιέξοδα 3.1. Resource
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
1 Administrivia  Assignments 0 & 1  Class contact for the next two weeks  Next week  midterm exam  project review & discussion (Chris Chambers) 
1 Wednesday, June 28, 2006 Command, n.: Statement presented by a human and accepted by a computer in such a manner as to make the human feel that he is.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
The Banker’s Algorithm for A Single Resource
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
1 A common project 2 error method Lock () var oldIntStat: int oldIntStat = SetInterruptsTo (DISABLED) if mutex == 0 mutex = 1 heldBy = currentThread oldIntStat.
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention.
1 Deadlocks 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter 3. 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
1 Deadlock Definition of deadlock Condition for its occurrence Solutions for avoiding and breaking deadlock –Deadlock Prevention –Deadlock Avoidance –Deadlock.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Deadlocks II.
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
CSC 322 Operating Systems Concepts Lecture - 28: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Deadlocks System Model RAG Deadlock Characterization
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Sistem Operasi IKH311 Deadlock. 2 Resources Examples of computer resources printers tape drives tables Processes need access to resources in reasonable.
Deadlocks Lots of resources can only be used by one process at a time. Exclusive access is needed. Suppose process A needs R1 + R2 and B needs R1 + R2.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Process Management Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Concurrency: Deadlock and Starvation
Deadlock Management.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
Chapter 7 Deadlocks.
Process Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
CS 333 Introduction to Operating Systems Class 7 - Deadlock
DPNM Lab. Dept. of CSE, POSTECH
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Presentation transcript:

1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University

2 Resources and Deadlocks  Processes need access to resources in order to make progress  Examples of computer resources  printers  tape drives  kernel data structures (process & file table entries …)  locks/semaphores to protect critical sections  Suppose a process holds resource A and requests resource B  at the same time another process holds B and requests A  both are blocked and remain so … this is deadlock

3 Resource Usage Model  Sequence of events required to use a resource  request the resource (like acquiring a mutex lock)  use the resource  release the resource (like releasing a mutex lock)  Must wait if request is denied  block  busy wait  fail with error code

4 Preemptable vs Nonpreemptable Resources  Preemptable resources  can be taken away from a process with no ill effects  Nonpreemptable resources  will cause the process to fail if taken away  Deadlocks occur when processes are granted exclusive access to non-preemptable resources and wait when the resource is not available

5 Definition of Deadlock 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 …  be awakened  run  release resources

6 Deadlock conditions  A deadlock situation can occur if and only if the following conditions hold simultaneously  Mutual exclusion condition – resource assigned to one process  Hold and wait condition – processes can get more than one resource  No preemption condition  Circular wait condition – chain of two or more processes (must be waiting for resource from next one in chain)

7 Resource acquisition scenarios acquire (resource_1) use resource_1 release (resource_1) Thread A: Example: var r1_mutex: Mutex... r1_mutex.Lock() Use resource_1 r1_mutex.Unlock()

8 Resource acquisition scenarios Thread A: acquire (resource_1) use resource_1 release (resource_1) Another Example: var r1_sem: Semaphore r1_sem.Up()... r1_sem.Down() Use resource_1 r1_sem.Up()

9 Resource acquisition scenarios acquire (resource_2) use resource_2 release (resource_2) Thread A:Thread B: acquire (resource_1) use resource_1 release (resource_1)

10 Resource acquisition scenarios acquire (resource_2) use resource_2 release (resource_2) Thread A:Thread B: No deadlock can occur here! acquire (resource_1) use resource_1 release (resource_1)

11 Resource acquisition scenarios: 2 resources acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) Thread A:Thread B:

12 Resource acquisition scenarios: 2 resources acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) Thread A:Thread B: No deadlock can occur here!

13 Resource acquisition scenarios: 2 resources acquire (resource_1) use resources 1 release (resource_1) acquire (resource_2) use resources 2 release (resource_2) acquire (resource_2) use resources 2 release (resource_2) acquire (resource_1) use resources 1 release (resource_1) Thread A:Thread B:

14 Resource acquisition scenarios: 2 resources acquire (resource_1) use resources 1 release (resource_1) acquire (resource_2) use resources 2 release (resource_2) acquire (resource_2) use resources 2 release (resource_2) acquire (resource_1) use resources 1 release (resource_1) Thread A:Thread B: No deadlock can occur here!

15 Resource acquisition scenarios: 2 resources acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) acquire (resource_2) acquire (resource_1) use resources 1 & 2 release (resource_1) release (resource_2) Thread A:Thread B:

16 Resource acquisition scenarios: 2 resources acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) acquire (resource_2) acquire (resource_1) use resources 1 & 2 release (resource_1) release (resource_2) Thread A:Thread B: Deadlock is possible!

17 Examples of deadlock  Deadlock occurs in a single program  Programmer creates a situation that deadlocks  Kill the program and move on  Not a big deal  Deadlock occurs in the Operating System  Spin locks and locking mechanisms are mismanaged within the OS  Threads become frozen  System hangs or crashes  Must restart the system and kill and applications

18 Other examples of deadlock

19 Resource Allocation Graphs Resource R A Process/Thread

20 Resource Allocation Graphs Resource R A Process/Thread “is held by”

21 Resource Allocation Graphs R A “is requesting” S Resource Process/Thread Resource

22 Resource Allocation Graphs R A S B

23 Resource Allocation Graphs Deadlock R A S B

24 Resource Allocation Graphs Deadlock = a cycle in the graph R A S B

25 Mulitple units of a resource  Some resources have only one “unit”.  Only one thread at a time may hold the resource. Printer Lock on ProcessTable  Some resources have several units.  All units are considered equal; any one will do. Page Frames Dice  A thread requests “k” units of the resource.  Several requests may be satisfied simultaneously.

26 Dealing with deadlock  Four general strategies  Ignore the problem Hmm… advantages, disadvantages?  Detection and recovery  Dynamic avoidance through resource allocation  Prevention, by structurally negating one of the four conditions

27 Deadlock detection (1 resource of each)  Let the problem happen, then recover  How do you know it happened?  Do a depth-first-search on the resource allocation graph

28 Deadlock detection (1 resource of each)  Let the problem happen, then recover  How do you know it happened?  Do a depth-first-search on the resource allocation graph

29 Deadlock detection (1 resource of each)  Let the problem happen, then recover  How do you know it happened?  Do a depth-first-search on the resource allocation graph

30 Deadlock detection (1 resource of each)  Let the problem happen, then recover  How do you know it happened?  Do a depth-first-search on the resource allocation graph

31 Deadlock detection (1 resource of each)  Let the problem happen, then recover  How do you know it happened?  Do a depth-first-search on the resource allocation graph

32 Deadlock modeling with multiple resources  Theorem: If a graph does not contain a cycle then no processes are deadlocked  A cycle in a RAG is a necessary condition for deadlock  Is it a sufficient condition?

33 Deadlock modeling with multiple resources  Theorem: If a graph does not contain a cycle then no processes are deadlocked  A cycle in a RAG is a necessary condition for deadlock  Is it a sufficient condition?

34 Deadlock detection (multiple resources)

35 Deadlock detection (multiple resources) Available resource vector Total resource vector

36 Deadlock detection (multiple resources) Available resource vector Total resource vector What I am requesting now What I have (now!)

37 Example  Is there a sequence of running process such that all the resources will be returned?

38 Detection algorithm 1. Look for an unmarked process Pi, for which the ith row of R is less than or equal to A 2. If such a process is found, add the i-th row of C to A, mark the process and go back to step 1 3. If no such process exists the algorithm terminates If all marked, no deadlock

39 Detection algorithm

40 Detection algorithm

41 Detection algorithm

42 Detection algorithm

43 Detection algorithm

44 Detection algorithm

45 Detection algorithm No deadlock!

46 Deadlock detection issues  How often should the algorithm run?  After every resource request?  Periodically?  When CPU utilization is low?  When we suspect deadlock because some thread has been asleep for a long period of time?

47 Recovery from deadlock  What should be done to recover?  Abort deadlocked processes and reclaim resources  Temporarily reclaim resource, if possible  Abort one process at a time until deadlock cycle is eliminated  Where to start?  Low priority process  How long process has been executing  How many resources a process holds  Batch or interactive  Number of processes that must be terminated

48 Other deadlock recovery techniques  Recovery through rollback  Save state periodically take a checkpoint start computation again from checkpoint  Done for large computation systems

49 Deadlock avoidance  Detection vs. avoidance…  Detection – “optimistic” approach Allocate resources “Break” system to fix it  Avoidance – “pessimistic” approach Don’t allocate resource if it may lead to deadlock If a process requests a resource make it wait until you are sure it’s OK  Which one to use depends upon the application

50 Process-resource trajectories time Process A t1t1 t2t2 t3t3 t4t4

51 Process-resource trajectories time Process A t1t1 t2t2 t3t3 t4t4 Requests Printer Requests CD-RW Releases Printer Releases CD-RW

52 Process-resource trajectories time Process B tWtW tXtX tYtY tZtZ

53 Process-resource trajectories time Process B tWtW tXtX tYtY tZtZ Requests CD-RW Requests Printer Releases CD-RW Releases Printer

54 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time

55 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time Both processes hold CD-RW

56 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time Both processes hold Printer

57 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time Forbidden Zone

58 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time Trajectory showing system progress

59 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time B makes progress, A is not running

60 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time B requests the CD-RW

61 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time Request is granted

62 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time A runs & makes a request for printer

63 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time Request is granted; A proceeds

64 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time B runs & requests the printer... MUST WAIT!

65 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time A runs & requests the CD-RW

66 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time A... holds printer requests CD-RW B... holds CD-RW requests printer

67 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time A... holds printer requests CD-RW B... holds CD-RW requests printer DEADLOCK!

68 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time A danger occurred here. Should the OS give A the printer, or make it wait???

69 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time This area is “unsafe”

70 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time Within the “unsafe” area, deadlock is inevitable. We don’t want to enter this area. The OS should make A wait at this point!

71 Process-resource trajectories Process B tWtW tXtX tYtY tZtZ Process A t1t1 t2t2 t3t3 t4t4 time B requests the printer, B releases CD-RW, B releases printer, then A runs to completion!

72 Safe states  The current state: “which processes hold which resources”  A “safe” state:  No deadlock, and  There is some scheduling order in which every process can run to completion even if all of them request their maximum number of units immediately  The Banker’s Algorithm:  Goal: Avoid unsafe states!!!  When a process requests more units, should the system grant the request or make it wait?

73 The Banker’s Algorithm  Assumptions:  Only one type of resource, with multiple units.  Processes declare their maximum potential resource needs ahead of time.  When a process requests more units should the system make it wait to ensure safety? Example: One resource type with 10 units 3

74 Safe states  Safe state – “when system is not deadlocked and there is some scheduling order in which every process can run to completion even if all of them suddenly request their maximum number of resource immediately” total 3

75 Unsafe states total Unsafe!

76 Avoidance with multiple resource types Note: These are the max. possible requests, which we assume are known ahead of time

77 Banker’s algorithm for multiple resources  Look for a row, R, whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system will eventually deadlock since no process can run to completion  Assume the process of the row chosen requests all the resources that it needs (which is guaranteed to be possible) and finishes. Mark that process as terminated and add all its resources to A vector  Repeat steps 1 and 2, until either all process are marked terminated, in which case the initial state was safe, or until deadlock occurs, in which case it was not

78 Avoidance modeling Available resource vector Total resource vector Maximum Request Vector Row 2 is what process 2 might need RUN ALGORITHM ON EVERY RESOURCE REQUEST

79 Avoidance algorithm Max request matrix

80 Avoidance algorithm Max request matrix

81 Avoidance algorithm Max request matrix

82 Avoidance algorithm Max request matrix

83 Avoidance algorithm Max request matrix

84 Avoidance algorithm Max request matrix

85 Deadlock avoidance  Deadlock avoidance is usually impossible  because you don’t know in advance what resources a process will need!  Alternative approach “deadlock prevention”  Prevent the situation in which deadlock might occur for all time!  Attack one of the four conditions that are necessary for deadlock to be possible

86 Deadlock avoidance  Conditions necessary for deadlock: Mutual exclusion condition Hold and wait condition No preemption condition Circular wait condition

87 Attacking the conditions  Attacking mutual exclusion?  a bad idea for some resource types  may work for others  Attacking no preemption?  a bad idea for some resource types  may work for others

88 Attacking the conditions  Attacking hold and wait?  Require processes to request all resources before they begin!  Process must know ahead of time  Process must tell system its “max potential needs”

89 Attacking the conditions  If a process decides it wants more than its initial declared needs, it must...  Release all resources  Give the system a new “max potential needs”  Resume execution  Issues:  Under-allocation of resources  Resource needs not known in advance

90 Attacking the conditions  Attacking circular wait?  Number each of the resources  Require each process to acquire lower numbered resources before higher numbered resources  More precisely: A process is not allowed to request a resource whose number is lower than the highest numbered resource it currently holds.

91 Recall this example of deadlock Assume that resources are ordered: 1. Resource_1 2. Resource_ etc... acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) acquire (resource_2) acquire (resource_1) use resources 1 & 2 release (resource_1) release (resource_2) Thread A:Thread B:

92 Recall this example of deadlock  Assume that resources are ordered:  1. Resource_1  2. Resource_2  3....etc...  Thread B violates the ordering! acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1) acquire (resource_2) acquire (resource_1) use resources 1 & 2 release (resource_1) release (resource_2) Thread A:Thread B:

93 Why Does Resource Ordering Work?  Assume deadlock has occurred.  Process A  holds X  requests Y  Process B  holds Y  requests Z  Process C  holds Z  requests X

94 Why Does Resource Ordering Work?  Assume deadlock has occurred.  Process A  holds X  requests Y  Process B  holds Y  requests Z  Process C  holds Z  requests X X < Y

95 Why Does Resource Ordering Work?  Assume deadlock has occurred.  Process A  holds X  requests Y  Process B  holds Y  requests Z  Process C  holds Z  requests X X < Y Y< Z

96 Why Does Resource Ordering Work?  Assume deadlock has occurred.  Process A  holds X  requests Y  Process B  holds Y  requests Z  Process C  holds Z  requests X X < Y Y< Z Z < X

97 Why Does Resource Ordering Work?  Assume deadlock has occurred.  Process A  holds X  requests Y  Process B  holds Y  requests Z  Process C  holds Z  requests X X < Y Y< Z Z < X This is impossible!

98 Why Does Resource Ordering Work?  Assume deadlock has occurred.  Process A  holds X  requests Y  Process B  holds Y  requests Z  Process C  holds Z  requests X X < Y Y< Z Z < X This is impossible! Therefore the assumption must be false!

99 Resource Ordering  The chief problem:  It is hard to come up with an ordering of the resources that everyone finds acceptable!  Still, I believe this is particularly useful within an OS 1. ProcessControlBlock 2. FileControlBlock 3. Page Frames  Also, the problem of resources with multiple units is not addressed.

100 A word on starvation  Starvation and deadlock are two different things  With deadlock – no work is being accomplished for the processes that are deadlocked, because processes are waiting for each other. Once present, it will not go away.  With starvation – work (progress) is getting done, however, a particular set of processes may not be getting any work done because they cannot obtain the resource they need

101 Quiz  What is deadlock?  What conditions must hold for deadlock to be possible?  What are the main approaches for dealing with deadlock?  Why does resource ordering help?