The Banker’s Algorithm for A Single Resource Granting the request leads to an unsafe state? True: deny the request False: carry the request out Has Max A 6 B 5 C 4 D 7 Has Max A 1 6 B 5 C 2 4 D 7 Has Max A 1 6 B 2 5 C 4 D 7 Free: 10 Free: 2 Free: 1 Safe Safe Unsafe!
Dealing With Multiple Resources Tape drives Scanners CD ROMs Process Plotters Tape drives Scanners CD ROMs Process Plotters E=(6 3 4 2) A=(1 0 2 0) P=(5 3 2 2) E=A+P A 3 1 B C D E A 1 B 2 C 3 D E C: Resources assigned R: Resources still needed
Algorithm Find a row (process) in R whose unmet resource needs are all smaller than or equal to A 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)
Problem of Deadlock Avoidance Wonderful algorithm in theory Essentially useless in practice 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! Kind of familiar?
Outline Resources and deadlock The ostrich algorithm Deadlock detection and recovery Deadlock avoidance Deadlock prevention Related issues
Deadlock Avoidance Vs. Prevention Avoidance (a police directing traffic) 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) 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. Four necessary conditions for a deadlock: mutual exclusion, hold and wait, no preemption and circular wait
Eliminate Mutual Exclusion Spooling technique 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 Daemon only prints after the complete output file is available. General idea to reduce deadlocks Assign a resource only when it is absolutely necessary As few processes as possible may actually claim the resource
Break Hold And Wait Condition Request all resources before execution Resource requirement is unclear before execution Inefficient usage of resources Used in some mainframe batch systems 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
Destroy Circular Wait One process at most one resource at any moment 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 Hard to find an order good for everyone Different systems may have different orders a program may have to be modified before it runs! RAG i A j B 1. Scanner 2. Plotter 3. Tape driver 4. CD-ROM driver 1-2-3 OK! 1-4-3 illegal! i<j, B not allowed to require i i>j, A not allowed to require j
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
Outline Resources and deadlock The ostrich algorithm Deadlock detection and recovery Deadlock avoidance Deadlock prevention Related issues
Two-phase Locking in Databases 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 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 No process pending/start over in real-time system Many operations cannot be safely repeated.
Starvation Deadlock free is not enough for OS No starvation: make sure each process getting service. Example: allocation of CPU Shortest job first: maximize throughput Long job may starve to death in a busy system! First-come-first-serve: fair, no starvation Lower throughput
Summary Deadlock: all processes are blocked and no progress can be made Mutual exclusion Hold and wait No preemption Circular wait Deadlock detection & recovery, avoidance, and prevention