1 / 24

Deadlock Avoidance: Resource Allocation and Prevention

Learn about deadlock avoidance techniques, including resource allocation state, safe states, the Banker's algorithm, and deadlock prevention methods.

Télécharger la présentation

Deadlock Avoidance: Resource Allocation and Prevention

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

  2. Contents • Deadlock avoidance • Deadlock prevention • Other issues

  3. Deadlock Avoidance • Simplest and most useful model for DA requires that • each process declare the maximum number of resources of each type that it may need • The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition • Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes Requires that the system has some additional a prioriinformation available

  4. Resource Allocation State: Safe, Unsafe , Deadlock

  5. Safe State • When a process requests an available resource, • system must decide if immediate allocation leaves the system in a safe state • System is in safe state? • if there exists a safe sequence of all processes • Sequence <P1, P2, …, Pn> is safeif for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<i • If Pi resource needs are not immediately available, then Pi can wait until all Pjhave finished • When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. • When Pi terminates, Pi+1 can obtain its needed resources, and so on

  6. Basic Facts • If a system is in safe state no deadlocks • If a system is in unsafe state  possibility of deadlock • Avoidance  ensure that a system will never enter an unsafe state

  7. Deadlock AvoidanceResource Trajectories Two process resource trajectories

  8. Safe and Unsafe States (1) Demonstration that the state in (a) is safe (a) (b) (c) (d) (e)

  9. Safe and Unsafe States (2) Demonstration that the sate in b is not safe (a) (b) (c) (d)

  10. Banker’s Algorithm • Deadlock avoidance algorithm • Resource-Allocation Graph is not applicable to a resource allocation system with multiple instance of each resource type • Supports multiple instances • Named because this algorithm could be used in a bank • Assumption: • Each process must make a priori claim for maximum use • When a process requests a resource it may have to wait • When a process gets all its resources it must return them in a finite amount of time • Two sub-algorithms • Safety Algorithm • Resource-Request Algorithm

  11. The Banker's Algorithm for a Single Resource • Three resource allocation states • safe • safe • unsafe (a) (b) (c)

  12. Banker's Algorithm for Multiple Resources Example of banker's algorithm with multiple resources

  13. Deadlock Prevention (1) • By ensuring that at least one of the four conditions for deadlock cannot hold, we can prevent deadlock • Restrain the ways request can be made • Mutual Exclusion • Hold and Wait • No Preemption • Circular Wait

  14. Deadlock PreventionAttacking the Mutual Exclusion Condition • Mutual Exclusion • not required for sharable resources (read-only file) • must hold for nonsharable resources (printer) • Some devices (such as printer) can be spooled • only the printer daemon uses printer resource • thus deadlock for printer eliminated • Not all devices can be spooled • Principle: • avoid assigning resource when not absolutely necessary • as few processes as possible actually claim the resource

  15. Attacking the Hold and Wait Condition • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources • require process to request and be allocated all its resources before it begins execution, or • allow process to request resources only when the process has none • Require processes to request resources before starting • a process never has to wait for what it needs • Problems • may not know required resources at start of run • also ties up resources other processes could be using • Low resource utilization; starvation possible • Variation • process must give up all resources • then request all immediately needed

  16. Attacking the No Preemption Condition • No Preemption • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released • Preempted resources are added to the list of resources for which the process is waiting • Process will be restarted only when it can regainits old resources, as well as the new ones that it is requesting • This is not a viable option • Consider a process given the printer • halfway through its job • now forcibly take away printer • !!??

  17. Attacking the Circular Wait Condition • Circular Wait • impose a total ordering of all resource types, and • require that each process requests resources in an increasing order of enumeration • Normally ordered resources • A resource graph (a) (b)

  18. Summary to Deadlock Prevention (1)

  19. Summary to Deadlock Prevention (2) • Deadlock prevention algorithm • Prevent deadlocks by restraining how requests can be made • The restraints ensures that • at least one of the necessary conditions for deadlock cannot occur and, hence, that deadlocks cannot hold • Possible side effects deadlock prevention • Low device utilization • Reduced system throughput • Alternative method  Deadlock Avoidance

  20. Other Issues: Two-Phase Locking • Phase One • process tries to lock all records it needs, one at a time • if needed record found locked, start over • (no real work done in phase one) • If phase one succeeds, it starts second phase, • performing updates • releasing locks • Note similarity to requesting all resources at once • Algorithm works where programmer can arrange • program can be stopped, restarted

  21. Non-resource Deadlocks • Possible for two processes to deadlock • each is waiting for the other to do some task • Can happen with semaphores • each process required to do a down() on two semaphores (mutex and another) • if done in wrong order, deadlock results

  22. Starvation • Algorithm to allocate a resource • may be to give to shortest job first • Works great for multiple short jobs in a system • May cause long job to be postponed indefinitely • even though not blocked • Solution: • First-come, first-serve policy

  23. Summary • A deadlock requires that four conditions hold simultaneously • Mutual exclusion, hold and wait, no preemption, circular wait • Four principle methods for dealing with deadlocks • Ignore the deadlock problem • Deadlock detection-recovery scheme • Deadlock avoidance • Deadlock prevention • Deadlock avoidance requires a priori information such as the maximum number of each resource classes • Bankers algorithm • Deadlock prevention ensures that at least one of the necessary conditions never holds

  24. Review • Deadlock avoidance • Deadlock prevention • Other issues

More Related