Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.1
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS 370: OPERATING SYSTEMS
[DEADLOCKS]
Shrideep PallickaraComputer Science
Colorado State University
October 18, 2018 L18.1
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.2Professor: SHRIDEEP PALLICKARA
Frequently asked questions from the previous class survey
October 18, 2018
¨ Prevention or avoidance: Which is better?¨ Delay durations when ”stalling” requests: how long is too long?
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.2
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.3Professor: SHRIDEEP PALLICKARA
Topics covered in this lecture
¨ Deadlock Avoidance¤ Banker’s Algorithm
¨ Deadlock Detection¤ And … recovery
¨ Other issues relating to deadlocks
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.4Professor: SHRIDEEP PALLICKARA
A deadlock-prone system can be in one of three states: safe, unsafe, and deadlocked
October 18, 2018
¨ Safe state: For any possible sequence of resource requests, there is at least one safe sequence of processing the requests ¤ That eventually succeeds in granting all pending and future requests
¨ Unsafe state: There is at least one sequence of future resource requests that leads to deadlock
¨ In a deadlocked state, the system has at least one deadlock
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.3
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.5Professor: SHRIDEEP PALLICKARA
A system in a safe state controls its own destiny
October 18, 2018
¨ For any workload, it can avoid deadlock by delaying the processing of some requests¤ Once the system enters an unsafe state, it may not be able to avoid
deadlock
¨ In particular, the Banker’s Algorithm (that we will look at next) delays any request that takes it from a safe to an unsafe state.
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
BANKER’S ALGORITHM
October 18, 2018 L18.6
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.4
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.7Professor: SHRIDEEP PALLICKARA
Banker’s Algorithm
¨ Designed by Dijkstra in 1965
¨ Modeled on a small-town banker¤ Customers have been extended lines of credit¤ Not ALL customers will need their maximum credit immediately
¨ Customers make loan requests from time to time
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.8Professor: SHRIDEEP PALLICKARA
Crux of the Banker’s Algorithm
October 18, 2018
¨ Consider each request as it occurs¤ See if granting it is safe
¨ If safe: grant it; If unsafe: postpone
¨ For safety banker checks if he/she has enough to satisfy some customer¤ If so, that customer’s loans are assumed to be repaid¤ Customer closest to limit is checked next¤ If all loans can be repaid; state is safe: loan approved
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.5
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.9Professor: SHRIDEEP PALLICKARA
Banker’s Algorithm: Managing the customers. Banker has only reserved 10 units instead of 22
October 18, 2018
A 0 6
B 0 5
C 0 4
D 0 7
Has Max
A 1 6
B 1 5
C 2 4
D 4 7
Has Max
A 1 6
B 2 5
C 2 4
D 4 7
Has Max
Free: 10 Free: 2 Free: 1
SAFE SAFE UNSAFEDelay all requests except C
A customer may not need the entire credit line. But the banker cannot count on this behaviorThere is ONLY ONE resource -- Credit
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.10Professor: SHRIDEEP PALLICKARA
Banker’s algorithm: Crux
October 18, 2018
¨ Declare maximum number of resource instances needed¤ Cannot exceed resource thresholds
¨ Determine if resource allocations leave system in a safe state
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.6
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.11Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: Data Structures [Overview]
Allocation Max AvailableA B C A B C A B C
P0 0 1 0 7 5 3 3 3 2P1 2 0 0 3 2 2P2 3 0 2 9 0 2P3 2 1 1 2 2 2P4 0 0 2 4 3 3
October 18, 2018
A, B, and C are different types of resources
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.12Professor: SHRIDEEP PALLICKARA
Data Structures: n is the number of processes and m is the number of resource types
¨ Available: Vector of length m¤ Number of resources for each type
n Available[i] = k
¨ Max: n x m matrix ¤ Maximum demand for each process (in each row)¤ Max[i,j]= k
n Process Pi may request at most k instances of Rj
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.7
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.13Professor: SHRIDEEP PALLICKARA
Data Structures: n is the number of processes and m is the number of resource types
October 18, 2018
¨ Allocation: n x m matrix ¤ Resource instances allocated for each process (each row)¤ Allocation[i,j]=k
n Process Pi currently allocated k instances of Rj
¨ Need: n x m matrix ¤ Resource instances needed for each process (each row)¤ Need[i,j]=k
n Process Pi may need k more instances of Rj
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.14Professor: SHRIDEEP PALLICKARA
Vectors identifying a process’ resource requirements: Rows in the matrices
¨ Allocationi¤ Resource instances allocated for process Pi
¨ Needi¤ Additional resource instances that process Pi may still request
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.8
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.15Professor: SHRIDEEP PALLICKARA
Banker’s Algorithm: Notations
¨ X and Y are vectors of length m
¨ X ≤ Y if-and-only-ifX[i] ≤ Y[i] for all i=1,2,...,m
¨ X = {1,7,3,2} and Y = {0,3,2,1}So, Y ≤ XAlso Y < X if Y ≤ X and Y ≠ X
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.16Professor: SHRIDEEP PALLICKARA
Banker’s Algorithm: Resource-request
¨ Requesti: Request vector for process Pi¤ Requesti[j]=k
n Process Pi wants k instances of Rj
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.9
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.17Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: Resource-request
Requesti≤ Needi
Requesti≤ Available
Available = Available – RequestiAllocationi = Allocationi + RequestiNeedi = Needi - Requesti
Yes
Yes
NO
NO
ErrorExceeded claim
Wait for availability
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.18Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: SafetyInitialize Work = Available
Find i such that:Finish[i]==false && Needi≤ Work
Work = Work + AllocationiFinish[i]=true
for all iif (Finish[i] = true)
YES
NO
YES
Safe state
NOUnsafe state
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.10
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.19Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: Example
Allocation Max AvailableA B C A B C A B C
P0 0 1 0 7 5 3 3 3 2P1 2 0 0 3 2 2P2 3 0 2 9 0 2P3 2 1 1 2 2 2P4 0 0 2 4 3 3
<P1, P3, P4, P2, P0> satisfies safety criteria
Suppose process P1 requests 1 A, and 2 Cs: Request1 = (1,0,2)Request1≤ AvailablePretend request was fulfilled
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.20Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: Example
Allocation Max AvailableA B C A B C A B C
P0 0 1 0 7 5 3 2 3 0P1 3 0 2 3 2 2P2 3 0 2 9 0 2P3 2 1 1 2 2 2P4 0 0 2 4 3 3
<P1, P3, P4, P0, P2> satisfies safety criteria
Request0 = (0,2,0) from process P0 cannot be granted: unsafe state
Request4 = (3,3,0) from process P4 cannot be granted: resources unavailable
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.11
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.21Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: Example
Allocation Max AvailableA B C A B C A B C
P0 0 3 0 7 3 3 2 1 0P1 3 0 2 3 2 2P2 3 0 2 9 0 2P3 2 1 1 2 2 2P4 0 0 2 4 3 3
Request0 = (0,2,0) from process P0 cannot be granted: unsafe state
October 18, 2018
None of the processes can now satisfy their max resource needs.
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.22Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: Practical implications [1/2]
October 18, 2018
¨ Understanding the Banker’s Algorithm can help in designing simple solutions for specific problems
¨ Banker’s Algorithm to devise a rule for thread safe acquisition of a pair of locks, A and B, with mutually recursive locking?¤ Suppose a thread needs to acquire locks A and B, in that order, while
another thread needs to acquire lock B first, then A¤ RULE: A thread is always allowed to acquire its second lock
n Acquire first lock provided the other thread does not already hold its first lock
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.12
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.23Professor: SHRIDEEP PALLICKARA
Bankers Algorithm: Practical implications [2/2]
¨ Processes rarely know in advance about their maximum resource needs
¨ Number of processes managed by the kernel is not fixed¤ Varies dynamically
¨ Resources thought to be available can vanish
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
DEADLOCK DETECTION
October 18, 2018 L18.24
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.13
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.25Professor: SHRIDEEP PALLICKARA
Single instance of EACH resource type
¨ Use wait-for graph¤ Variant of the resource allocation graph
¨ Deadlock exists if there is a cycle in the graph
¨ Transformation①① Remove resource nodes②② Collapse appropriate edges
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.26Professor: SHRIDEEP PALLICKARA
What the edges in the wait-for graph imply
¨ Pi à Pj¤ Process Pi is waiting for a resource held by Pj
¨ Pi à Pj only if resource allocation graph has① Pià Rq and
② Rqà Pj for some resource Rq
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.14
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.27Professor: SHRIDEEP PALLICKARA
Transforming a resource allocation graph into a wait-for graph
R4R1
R2
P1 P2 P3
R5
R3
P4
P5
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.28Professor: SHRIDEEP PALLICKARA
Transforming a resource allocation graph into a wait-for graph
R4
R2
R1
P1 P2 P3
R5
R3
P4
P5
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.15
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.29Professor: SHRIDEEP PALLICKARA
Transforming a resource allocation graph into a wait-for graph
P1 P2 P3
P4
P5
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.30Professor: SHRIDEEP PALLICKARA
Deadlock detection for multiple instances of a resource type
October 18, 2018
¨ Wait-for graph is not applicable
¨ Approach uses data structures similar to Banker’s algorithm
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.16
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.31Professor: SHRIDEEP PALLICKARA
Data Structures: n is number of processesm is number of resource types
October 18, 2018
¨ Available: Vector of length m¤ Number of resources for each type
¨ Allocation: n x m matrix ¤ Resource instances allocated for each process¤ Allocation[i,j]=k
n Process Pi currently allocated k instances of Rj
¨ Request: n x m matrix ¤ Current request for each process¤ Request[i,j]=k
n Process Pi requests k more instances of Rj
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.32Professor: SHRIDEEP PALLICKARA
Deadlock detection: InitializationWork and Finish are vectors of length m & n
Work = Availableif (Allocationi ≠ 0) {
Finish[i] = false;} else {
Finish[i] = true;}
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.17
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.33Professor: SHRIDEEP PALLICKARA
Deadlock detection
Find i such that:Finish[i]==false && Requesti≤ Work
Work = Work + AllocationiFinish[i]=true
for all iif (Finish[i] = true)
YESNO
YES
Safe state
NODeadlock
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.34Professor: SHRIDEEP PALLICKARA
Deadlock detection: Usage
¨ How often will the deadlock occur?
¨ How many processes will be affected when it happens?
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.18
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.35Professor: SHRIDEEP PALLICKARA
Frequency of invoking deadlock detection
October 18, 2018
¨ Resources allocated to deadlocked process idle¤ Until the deadlock can be broken
¨ Deadlocks occur only when process makes a request¤ Significant overheads to run detection per request
¨ Middle ground: Run at regular intervals
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
RECOVERY FROM DEADLOCK
October 18, 2018 L18.36
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.19
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.37Professor: SHRIDEEP PALLICKARA
Recovery from deadlock
¨ Automated or manual
¨ OPTIONS¤ Break the circular wait: Abort processes¤ Preempt resources from deadlocked process(es)
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.38Professor: SHRIDEEP PALLICKARA
Breaking circular wait: Process termination
¨ Abort all deadlocked processes
¨ Abort processes one at a time¤ After each termination, check if deadlock persists
¨ Reclaim all resources allocated to terminated process
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.20
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.39Professor: SHRIDEEP PALLICKARA
Terminating a Process
October 18, 2018
¨ Process may be in the midst of something¤ Updating files, printing data, etc.
¨ Abort process whose termination will incur minimum costs¤ Policy decision similar to scheduling decisions
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.40Professor: SHRIDEEP PALLICKARA
Factors determining process termination
¨ Priority
¨ How long has the process been running? ¤ How much longer?
¨ Number and types of resources used¤ How many more needed?
¨ Interactive or batch
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.21
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.41Professor: SHRIDEEP PALLICKARA
Deadlock recovery: Resource preemption
October 18, 2018
Preempt resources from some process
Give resources to some other process
Deadlock broken
DONE
Deadlock persists
For a set of deadlocked processes
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.42Professor: SHRIDEEP PALLICKARA
Resource preemption: Issues
October 18, 2018
¨ Selecting a victim¤ Which resource and process¤ Order of preemption to minimize cost
¨ Starvation¤ Process can be selected for preemption finite number of times
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.22
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.43Professor: SHRIDEEP PALLICKARA
Deadlock recovery through rollbacks
¨ Checkpoint process periodically¤ Contains memory image and resource state
¨ Deadlock detection tells us which resources are needed
¨ Process owning a needed resource¤ Rolled back to before it acquired needed resource
n Work done since rolled back checkpoint discarded
¤ Assign resource to deadlocked process
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
OTHER ISSUESOctober 18, 2018 L18.44
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.23
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.45Professor: SHRIDEEP PALLICKARA
Two-phase locking
¨ Used in database systems
¨ Operation involves requesting locks on several records and updating all the locked records
¨ When multiple processes are running?¤ Possibility of deadlocks
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.46Professor: SHRIDEEP PALLICKARA
Two-Phase Locking
¨ First phase¤ Process tries to acquire all the locks it needs, one at time¤ If successful: start second-phase¤ If some record is already locked?
n Release all locks and start the first phase all over
¨ Second-phase¤ Perform updates and release the locks
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.24
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.47Professor: SHRIDEEP PALLICKARA
Communication Deadlocks
¨ Process A sends a request message to process B¤ Blocks until B sends a reply back
¨ Suppose, that the request was lost¤ A is blocked waiting for a reply¤ B is blocked waiting for a request to do something¤ Communication deadlock
October 18, 2018
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.48Professor: SHRIDEEP PALLICKARA
Communication deadlocks
¨ Cannot be prevented by ordering resources (there are none)¤ Or avoided by careful scheduling (no moments when a request can be
postponed)
¨ Solution to breaking communication deadlocks?¤ Timeouts
n Start a timer when you send a message to which a reply is expected.
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.25
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.49Professor: SHRIDEEP PALLICKARA
Livelocks
October 18, 2018
¨ Polling (busy waits) used to enter critical section or access a resource¤ Typically used for a short time when overhead for suspension is considered
greater
¨ In a livelock two processes need each other’s resource¤ Both run and make no progress, but neither process blocks¤ Use CPU quantum over and over without making progress
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.50Professor: SHRIDEEP PALLICKARA
Livelocks do occur
¨ If fork fails because process table is full¤ Wait for some time and try again
¨ But there could be a collection of processes each trying to do the same thing
October 18, 2018
SLIDES CREATED BY: SHRIDEEP PALLICKARA L18.26
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
CS370: Operating Systems [Fall 2018]Dept. Of Computer Science, Colorado State University
L18.51Professor: SHRIDEEP PALLICKARA
The contents of this slide-set are based on the following references
October 18, 2018
¨ Avi Silberschatz, Peter Galvin, Greg Gagne. Operating Systems Concepts, 9th edition. John Wiley & Sons, Inc. ISBN-13: 978-1118063330. [Chapter 7]
¨ Andrew S Tanenbaum and Herbert Bos. Modern Operating Systems. 4th Edition, 2014. Prentice Hall. ISBN: 013359162X/ 978-0133591620. [Chapter 6]
¨ Thomas Anderson and Michael Dahlin. Operating Systems Principles and Practice. 2nd
Edition. ISBN: 978-0985673529. [Chapter 6]