The Bankers Algorithm for A Single Resource n
The Banker’s Algorithm for A Single Resource n Granting the request leads to an unsafe state? n n A B C D True: deny the request False: carry the request out Has Max 0 6 0 5 0 4 0 7 Free: 10 Safe A B C D Has Max 1 6 1 5 2 4 4 7 Free: 2 Safe Has Max A 1 6 B 2 5 C 2 4 D 4 7 Free: 1 Unsafe! 1
CD ROMs Scanners Plotters Tape drives Process Dealing With Multiple Resources E=(6 3 4 2) A=(1 0 2 0) P=(5 3 2 2) E=A+P A 3011 B 0100 C 1110 D 1101 E 0000 A 1100 B 0112 C 3100 D 0010 E 2110 C: Resources assigned R: Resources still needed 2
Algorithm n Find a row (process) in R whose unmet resource needs are all smaller than or equal to A n n n No such row the system will possibly deadlock (unsafe) Mark the process as terminated, add all its resources to the A vector Repeat 1 and 2 until either all process are marked terminated (safe state), or until a deadlock occurs (unsafe state) 3
Problem of Deadlock Avoidance n n Wonderful algorithm in theory Essentially useless in practice n n Can you tell in advance what your maximum resource need for running your browser is? Number of process is dynamic Resources can break Information about future requests is unknown! n Kind of familiar? 4
Outline n n n Resources and deadlock The ostrich algorithm Deadlock detection and recovery Deadlock avoidance Deadlock prevention Related issues 5
Deadlock Avoidance Vs. Prevention n Avoidance (a police directing traffic) n n n The system dynamically consider every request and decides if it is safe to grant it at this point Allow more concurrency Prevention (a red traffic light) n n Constrain how requests for resources can be made and how they are handled Ensure at least one of the necessary conditions for deadlock cannot hold. n Four necessary conditions for a deadlock: mutual exclusion, hold and wait, no preemption and circular wait 6
Eliminate Mutual Exclusion n Spooling technique n n Good for printer, plotter, etc. Only printer daemon can access the physical printer and it requires no other resource. Not all resources can be spooled. Examples? Competition on spooling space n n Daemon only prints after the complete output file is available. General idea to reduce deadlocks n n Assign a resource only when it is absolutely necessary As few processes as possible may actually claim the resource 7
Break Hold And Wait Condition n Request all resources before execution n Resource requirement is unclear before execution Inefficient usage of resources Used in some mainframe batch systems n n n Much more instances of each I/O device Many jobs are CPU-bound Improvement: when requesting a new resource, temporarily release all resource, then request all at once 8
Destroy Circular Wait n One process at most one resource at any moment n n n RAG Must release the first one when requiring the second one. How to copy a huge file from a tape to a printer? Global numbering of all resources Processes request resources in numerical order A B n Hard to find an order good for everyone n Different systems may have different orders a program may have to be modified before it runs! i j 1. Scanner 2. Plotter 3. Tape driver 4. CD-ROM driver i<j, B not allowed to require i i>j, A not allowed to require j 9 1 -2 -3 OK! 1 -4 -3 illegal! n
Approaches to Deadlock Prevention Condition Approach Mutual exclusion Spooling Hold and wait Request all resources before execution No preemption Take resources away Circular wait Order resources numerically 10
Outline n n n Resources and deadlock The ostrich algorithm Deadlock detection and recovery Deadlock avoidance Deadlock prevention Related issues 11
Two-phase Locking in Databases n n n An excellent special purpose algorithm Scenario: lock on several records, update all of them and release. Phase I: try to lock all the records it needs, one at a time n n n If succeed (get all the records), go to Phase II Else, release all locks, start Phase I again Problems? Refer to dining philosophers problem Phase II: do updates, release the locks Not a general strategy n n No process pending/start over in real-time system Many operations cannot be safely repeated. 12
Starvation n Deadlock free is not enough for OS n n No starvation: make sure each process getting service. Example: allocation of CPU n n n Shortest job first: maximize throughput Long job may starve to death in a busy system! First-come-first-serve: fair, no starvation n Lower throughput 13
Summary n Deadlock: all processes are blocked and no progress can be made n n n Mutual exclusion Hold and wait No preemption Circular wait Deadlock detection & recovery, avoidance, and prevention 14
- Slides: 14