220 likes | 612 Views
CS423UG Operating Systems . Deadlocks - I. Indranil Gupta Lecture 11 Sep 19, 2005. Content. “Resources” Deadlocks Deadlock Prevention. Review: Synchronization and IPC. Data races Critical regions and mutual exclusions Solutions: Peterson’s solution
E N D
CS423UG Operating Systems Deadlocks - I Indranil Gupta Lecture 11 Sep 19, 2005
Content • “Resources” • Deadlocks • Deadlock Prevention CS423UG - Operating Systems, Indranil Gupta
Review: Synchronization and IPC • Data races • Critical regions and mutual exclusions • Solutions: • Peterson’s solution • TSL • Semaphores & Mutex • Monitor • Barriers • Message Passing • Classical IPC Problems CS423UG - Operating Systems, Indranil Gupta
Resources (1) • A resource is a commodity needed by a process. • A resource could be either: • serially reusable: e.g., CPU, memory, disk space, I/O devices, files. acquire use release , or • consumable: produced by a process, needed by a process; e.g., messages, buffers of information, interrupts. create acquire use A consumable resource ceases to exist after it has been used, so it is not released. CS423UG - Operating Systems, Indranil Gupta
Resources (2) • Resources can also be either: • preemptible: e.g., CPU, central memory, or • non-preemptible: e.g., a tape drive. • And resources can be either: • shared among several processes, or • dedicated exclusively to a single process. CS423UG - Operating Systems, Indranil Gupta
Using Semaphore to Share a Resource 0 2 6 3 4 5 External Semaphore A(1), B(1); 2 External Semaphore A(0), B(1); 3 4 5 External Semaphore A(0), B(0); External Semaphore A(0), B(1); External Semaphore A(1), B(1); Process Q(); { A.Down(); B.Down(); use both resource B.Up(); A.Up(); } Process P(); { A.Down(); B.Down(); use both resource B.Up(); A.Up(); } CS423UG - Operating Systems, Indranil Gupta
But Deadlock can Happen! 1 1 2 3 1 External Semaphore A(1), B(1); 2 External Semaphore A(0), B(1); 3 External Semaphore A(0), B(0); Process Q(); { B.Down(); A.Down(); use both resources A.Up(); B.Up(); } Process P(); { A.Down(); B.Down(); use both resources B.Up(); A.Up(); } DEADLOCK CS423UG - Operating Systems, Indranil Gupta
Deadlock Traffic Lights: Mechanism for Deadlock Control CS423UG - Operating Systems, Indranil Gupta
Deadlock Definition • What is a deadlock? • A process is deadlocked if it is waiting for an event that will never occur. • Typically, but not necessarily, more than one process will be involved together in a deadlock (the deadly embrace). • Is deadlock the same as starvation (or indefinitely postponed)? • A process is indefinitely postponed if it is delayed repeatedly over a long period of time while the attention of the system is given to other processes, i.e., logically the process may proceed but the system never gives it the CPU. CS423UG - Operating Systems, Indranil Gupta
Conditions for Deadlock • What conditions should exist in order to lead to a deadlock? • Real life analogy such as • All Dining Philosophers simultaneously pick up their left forks. • 4 cars stuck in a “Stop All Way” intersection – each driver waiting for the car on her right. CS423UG - Operating Systems, Indranil Gupta
Necessary Conditions for Deadlock I. Mutual exclusion • Processes claim exclusive control of the resources they require II. Wait-for condition • Processes hold resources already allocated to them while waiting for additional resources III. No preemption condition • Resources cannot be preempted (removed) from the processes holding them until released explicitly IV. Circular wait condition • A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain Deadlock present => I-IV are all true CS423UG - Operating Systems, Indranil Gupta
Resource Allocation Graph CS423UG - Operating Systems, Indranil Gupta
Deadlock Model I-IV are all true may not => Deadlock CS423UG - Operating Systems, Indranil Gupta
Anti-Deadlock Strategies • Prevention • design a system in such a way that deadlocks cannot occur. • Avoidance • impose less stringent conditions than for prevention, allowing the possibility of deadlock, but sidestepping it as it approaches. • Detection • allow the possibility of deadlock, but determine deadlock when it has occurred, and which processes and resources are involved. • Recovery • after a deadlock has been detected, clear the problem, allowing the deadlocked processes to complete and the resources to be reused. Usually involves destroying the affected processes and starting them over. CS423UG - Operating Systems, Indranil Gupta
The Ostrich Algorithm • Don’t do anything, simply restart the system (stick your head into the sand, pretend there is no problem at all). • Rationale: makes the common case (no deadlocks) faster and more reliable • Deadlock prevention, avoidance or detection/recovery algorithms are expensive, and add extra overhead • if deadlock occurs only rarely, the Ostrich algorithm is ok CS423UG - Operating Systems, Indranil Gupta
Deadlock Prevention: Havender's Algorithms • Restrict the system so that at least one of the deadlock conditions is always false. • Mutual exclusion condition • Solution: exclusive use of resources is an important feature, but for some resources (virtual memory, virtual disks, CPU), it is possible to have two processes share a resource. • Hold-and-Wait condition • Solution: Force each process to request all required resources at once, e.g., at start. Process cannot proceed until all resources have been acquired. If not able to acquire all, release all and start again. CS423UG - Operating Systems, Indranil Gupta
Deadlock Prevention: Havender's Algorithms (2) • Restrict the system so that at least one of the deadlock conditions is always false. • No preemption condition • Solution: If a process holding some reusable resources makes a further request which is denied, and it wishes to wait for the new resources to become available, it must release all resources currently held and, if necessary, request them again along with the new resources. Thus, resources are removed from a process holding them. • Circular wait condition • Solution: All resource types have unique integer id. Processes can request resources only in increasing order of the resource id’s. CS423UG - Operating Systems, Indranil Gupta
Two-Phase Locking • Phase One • process tries to lock all records it needs, one at a time, but one after another • if needed record is found to be already locked, release all locks and start over • (no real work done in phase one) • If phase one succeeds, it starts second phase • performs updates/writes shared variables • releases locks when no longer required • Note similarity to requesting all resources at once • Two-phase locking algorithm used in database systems CS423UG - Operating Systems, Indranil Gupta
Summary: Deadlock Prevention CS423UG - Operating Systems, Indranil Gupta
Reminders • Reading for this lecture: Sections 3.1-3.3, 3.6, 3.7 • Reading for next lecture: Sections 3.4-3.5, and 3.9 CS423UG - Operating Systems, Indranil Gupta