Download presentation
1
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!
2
Dealing With Multiple Resources
Tape drives Scanners CD ROMs Process Plotters Tape drives Scanners CD ROMs Process Plotters E=( ) A=( ) P=( ) 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
3
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)
4
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?
5
Outline Resources and deadlock The ostrich algorithm
Deadlock detection and recovery Deadlock avoidance Deadlock prevention Related issues
6
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
7
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
8
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
9
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 Plotter 3. Tape driver 4. CD-ROM driver OK! illegal! i<j, B not allowed to require i i>j, A not allowed to require j
10
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
11
Outline Resources and deadlock The ostrich algorithm
Deadlock detection and recovery Deadlock avoidance Deadlock prevention Related issues
12
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.
13
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
14
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.