Operating Systems Chapter 3: Deadlocks

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
EEE 435 Principles of Operating Systems Deadlock Avoidance, Prevention, and Wrap-Up (Modern Operating Systems 3.5, 3.6, and 3.7)
Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money.
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
5/25/2015Page 1 Deadlock Management B. Ramamurthy.
Chapter 3 Deadlocks - Αδιέξοδα 3.1. Resource
With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
Deadlocks Tore Larsen With slides from T. Plagemann, C. Griwodz, K. Li, A. Tanenbaum and M. van Steen.
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
The Banker’s Algorithm for A Single Resource
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlock Detection with One Resource of Each Type (1)
DeadlocksDeadlocks CS Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Deadlocks CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
OPERATING SYSTEM LESSON 8 (DEADLOCKS).
Deadlocks.
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Overview Resources Why do deadlocks occur? Dealing with deadlocks Ignoring them: ostrich algorithm Detecting & recovering from deadlock Avoiding deadlock.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention.
1 Deadlocks 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter 3. 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
1 Deadlock Definition of deadlock Condition for its occurrence Solutions for avoiding and breaking deadlock –Deadlock Prevention –Deadlock Avoidance –Deadlock.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems (OS)
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
CSC 322 Operating Systems Concepts Lecture - 28: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Deadlocks System Model RAG Deadlock Characterization
CS333 Intro to Operating Systems Jonathan Walpole.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Deadlocks References –text: Tanenbaum ch.3. Deadly Embrace Deadlock definition –A set of process is dead locked if each process in the set is waiting.
Deadlocks Chapter 6 Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Sistem Operasi IKH311 Deadlock. 2 Resources Examples of computer resources printers tape drives tables Processes need access to resources in reasonable.
Deadlocks Lots of resources can only be used by one process at a time. Exclusive access is needed. Suppose process A needs R1 + R2 and B needs R1 + R2.
Chapter 7: Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Deadlock Management.
Chapter 6 : Deadlocks What is a Deadlock?
Deadlocks References text: Tanenbaum ch.3.
Chapter 7: Deadlocks.
Chapter 7 Deadlocks.
Process Deadlocks.
Deadlocks References text: Tanenbaum ch.3.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
DPNM Lab. Dept. of CSE, POSTECH
Review: Readers-Writers Problem
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 7: Deadlocks.
Deadlocks References text: Tanenbaum ch.3.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks -For MCA, PU
Presentation transcript:

Operating Systems Chapter 3: Deadlocks Instr: Yusuf Altunel IKU Department of Computer Engineering (212) 498 42 10 y.altunel@iku.edu.tr

Content 3.1.Sharing Resources 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention 3.7. Other issues

Sharing Resources Computer systems have resources shared between processes can only be used by one process at a time printers, tape drives, slots in the system’s internal tables Two processes simultaneously writing to the printer: leads to gibberish. two processes using the same file system table slot: lead to a corrupted file system. operating systems have the ability to temporarily grant a process exclusive access to certain resources. Gibberish: Anlamsiz ses ya da konusma

Deadlock Two processes each want to record a scanned document on a CD. Process A requests permission to use the scanner and is granted it. Process requests the CD recorder first and is also granted it. Now A asks for the CD recorder, but the request is denied until B releases it. Instead of releasing the CD recorder B asks for the scanner. Both processes are blocked and will remain so forever. This situation is called a deadlock.

Resources a resource is Types of resources: anything that can be used by only a single process at any instant of time Types of resources: hardware device e.g., a tape drive, printers piece of information e.g., a record in a database A computer has many different resources some resources have several identical instances such as three tape drives. any one of them can be used to satisfy requests for the resource.

Resources Preemptable Resources Nonpreemptable Resources can be taken away from the process owning it with no ill effects. Example: Memory of a printer Nonpreemptable Resources cannot be taken away from its current owner without causing the computation to fail deadlocks involve nonpreemptable resources Example: CD Writer If a process has begun to burn a CD-ROM, suddenly taking the CD recorder away from it and giving it to another process will result in a garbled CD

Using a Resource Sequence of events required to use a resource: request the resource use the resource release the resource Must wait if request is denied requesting process may be blocked or fail with some error code

Introduction to Deadlocks Formal definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause Usually the event is release of a currently held resource None of the processes can run release resources be awakened

Conditions for Deadlock All of four conditions must hold to be a deadlock: Mutual exclusion condition. Each resource is either currently assigned to exactly one process or is available. Hold and wait condition. Processes currently holding resources granted earlier can request new resources. No preemption condition. Resources previously granted cannot be forcibly taken away from a process. They must be explicitly released by the process holding them. Circular wait condition. There must be a circular chain of two or more processes, each is waiting for a resource held by the next member of the chain.

Deadlock Modeling Using directed graphs (a) Holding a resource by a process An arc from a resource to a process The resource is held by that process (b) Requesting a resource An arc from a process to a resource The process is currently blocked waiting the resource to be available (c) Deadlock (C-T-D-U-C) A cycle in the graph means a deadlock

How deadlock occurs

How Deadlock Avoided

Strategies for dealing with Deadlocks just ignore the problem altogether detection and recovery dynamic avoidance careful resource allocation prevention negating one of the four necessary conditions

The Ostrich Algorithm Stick your head in the sand Reasonable if and pretend there is no problem Reasonable if deadlocks occur very rarely cost of prevention is high UNIX and Windows takes this approach It is a trade-off between convenience correctness

Deadlock Detection and Recovery system does not attempt to prevent deadlocks from occurring Instead lets them occur tries to detect when this happens and then takes some action to recover

Detection with One Resource of Each Type Condition: There is only one resource from each type Example: 1. Process A holds R and wants S. 2. Process B holds nothing but wants T. 3. Process C holds nothing but wants S. 4. Process D holds U and wants S and T. 5. Process E holds T and wants V. 6. Process F holds W and wants S. 7. Process C holds V and wants U.

Example Note the resource ownership and requests A cycle can be found within the graph, denoting deadlock

Algorithm 1. For each node, N in the graph, perform the following 5 steps with N as the starting node. 2. Initialize L to the empty list, and designate all the arcs as unmarked. 3. Add the current node to the end of L and check to see if the node now appears in L two times. If it does, the graph contains a cycle (listed in L) and the algorithm terminates. 4. From the given node, see if there are any unmarked outgoing arcs. If so, go to step 5; if not, go to step 6

Algorithm 5. Pick an unmarked outgoing arc at random and mark it Then follow it to the new current node and go to step 3. 6. We have now reached a dead end. Remove it and go back to the previous node, make the previous one as the current node, If this node is the initial node, the graph does not contain any cycles and the algorithm terminates.

Detection with Multiple Resource of Each Type Processes: P1 through Pn Resources: (1..m) E1 resources of class 1, (E1 = 2 means the system has two tape drives ) E2 resources of class 2, … Ei resources of class i (1  i m) available resource vector: Ai Resources assigned to no processes Ai : available number of instances for resource i current allocation matrix: C the request matrix : P

Matrix for Deadlock Detection The i-th row of C : tells how many instances of each resource class Pi currently holds, Cij :the number of instances of resource j that are held by process i. Request matrix: Rij is the number of instances of resource j that Pi wants

R1 R2 R3 R4 R1 R2 R3 R4 P1 P2 P3 R1 R2 R3 R4 R1 R2 R3 R4

Invariants Every resource is either allocated or available If we add up all allocated resources j (Example: Printer) To the available resources The result is the number of instances for the resource class

Deadlock Detection Algorithm Each process is initially said to be unmarked. As the algorithm progresses processes will be marked, indicating that they are able to complete and are thus not deadlocked. When the algorithm terminates, any unmarked processes are known to be deadlocked. The deadlock detection algorithm: Look for an unmarked process, Pi for which the ith row of R is less than or equal to A. If such a process is found, add the ith row of C to A, mark the process, and go back to step If no such process exists, the algorithm terminates. When the algorithm finishes, all the unmarked processes, if any, are deadlocked.

Deadlock Detection Algorithm step 1 is looking for a process that can be run to completion Such a process is characterized as having resource demands can be met by the currently available resources The selected process is then run until it finishes, it returns the resources it is holding to the pool of available resources. It is then marked as completed. If all the processes are ultimately able to run none of them are deadlocked If some of them can never run, they are deadlocked. the algorithm is nondeterministic because it may run the processes in any feasible order, the result is always the same.

DDA Example (1) we have three processes four resource classes, Labeled as tape drives (4), plotters (2), scanner (2), and CD-ROM (1) drive. E=(4, 2, 2,1) Process 1 has one scanner (2-1=1). A=(4, 2 , 1, 1) Process 2 has two tape drives (4-2=2) and a CD-ROM drive (1-1=0). A=(2, 2 , 1, 0) Process 3 has a plotter (2-1=1) and two scanners (1-1=0). A=(2, 1 , 0, 0)

DDA Example (2) Each process needs additional resources P1: 2 Tape drives, 1 CD Rom P2: 1 Tape drive, 1 Scanner P3: 2 Tape drive, 1 Plotter Execution of deadlock detection algorithm. we look for a process whose resource request can be satisfied. The first one cannot be satisfied because there is no CD-ROM drive available. The second cannot be satisfied either, because there is no scanner free. the third one can be satisfied, so process 3 runs: A=(0, 0 , 0, 0) After execution eventually returns all its resources A = (2 2 2 0) At this point process 2 can run A = (1 2 1 0) After execution returns its resources, giving A= (4 2 2 1) Now the remaining process can run. There is no deadlock in the system.

DDA Structures P1 P2 P3

When to Detect the Deadlock? One possibility is to check every time a resource request is made. This is certain to detect them as early as possible, but it is potentially expensive in terms of CPU time. An alternative strategy is to check every k minutes, or perhaps only when the CPU utilization has dropped below some threshold. The reason for considering the CPU utilization is if enough processes are deadlocked, there will be few runnable processes, and the CPU will often be idle.

Recovery from Deadlock Recovery through Preemption temporarily take a resource away from its current owner and give it to another process. manual intervention may be required, in batch processing mainframe operating systems Recovery through Rollback arrange to have processes checkpointed periodically write process state to a file so that it can be restarted later When a deadlock occurs: roll back the process to a point an earlier check-point

Recovery from Deadlock Recovery through Killing Processes The crudest, but simplest way kill one or more processes. Either kill a process in the cycle. the other processes will be able to continue. If this does not help it can be repeated until the cycle is broken or kill a process not in the cycle to release its resources. the process to be killed is carefully chosen because it is holding resources that some process in the cycle needs

Deadlock Avoidance The system must be able Question: The answer is: to decide whether granting a resource is safe or not only make the allocation when it is safe. Question: Is there an algorithm can always avoid deadlock by making the right choice all the time? The answer is: Yes: we can avoid deadlocks, but only if certain information is available in advance

Deadlock Avoidance Methods Safe and unsafe states Safe state: A state which is not deadlocked and there is some scheduling order Guarantee to run to completion Even if the process request its maximum number of resources immediately Unsafe state: the completion is not guaranteed Although it may not result in a deadlock The Banker’s Algorithm Extension of deadlock detection algorithm Based on small-town banker Dealing with a group of customers Check to see if granting credit request is a safe state If so grant the request Else reject it

Safe and Unsafe States-1 Only 10 units are reserved! States of processes: A,B,C (a) A has 3 instances of the resources May need as much as 9 B has 2 but need 4 C has 2 but need 7 (b) Select B and apply (c) B completes and frees the resources (d) Select C and apply (e) free the resources As a result this is a safe state

Safe and Unsafe States-2 States of processes: A,B,C (a) A has 3 instances of the resources May need as much as 9 B has 2 but need 4 C has 2 but need 7 (b) A request and gets another resource (c) Select B and apply (d) B completes and frees the resources Only 4 free resources and both processes wants 5

The Banker’s Algorithm Considers each request as it occurs See if granting resource leads to a safe state. If it does, the request is granted; otherwise, it is postponed until later. To see if a state is safe, the banker checks to see if he has enough resources to satisfy some customer. If so, those loans are assumed to be repaid. and the customer now closest to the limit is checked If all loans can eventually be repaid, the state is safe and the initial request can be granted.

The Banker’s Algorithm Four customers A,B,C,& D Total reserved amount is 10 units (a) the reserved amounts for individual customers (b) after using some credits (safe condition) (c) B granted 1 more units of credits (unsafe condition)

Deadlock Prevention Attack the conditions stated for deadlock Mutual exclusion condition. Each resource is either currently assigned to exactly one process or is available. Hold and wait condition. Processes currently holding resources granted earlier can request new resources. No preemption condition. Resources previously granted cannot be forcibly taken away from a process. They must be explicitly released by the process holding them. Circular wait condition. There must be a circular chain of two or more processes, each is waiting for a resource held by the next member of the chain.

Mutual Exclusion Condition if no resource were ever assigned exclusively to a single process we would never have deadlocks. However, allowing two processes to write on the printer at the same time will lead to chaos. By spooling printer output, several processes can generate output at the same time Not all devices can be spooled the process table does not lend itself well to being spooled. Furthermore, disk space competition for spooling an itself lead to deadlock Principle: avoid assigning resource when not absolutely necessary as few processes as possible actually claim the resource

Hold and Wait Condition Require processes to request resources before starting a process never has to wait for what it needs If everything is available, the process will be allocated whatever it needs and can run to completion. If one or more resources are busy, nothing will be allocated and the process would just wait Problems may not know required resources at start of run also ties up resources other processes could be using Variation: process must give up all resources then request all immediately needed

No Preemption Condition Attacking the third condition (no preemption) is even less promising than attacking the second one. If a process has been assigned the printer and is in the middle of printing its output, forcibly taking away the printer because a needed plotter is not available is tricky at best and impossible at worst.

Circular Wait Condition - 1 The circular wait can be eliminated in several ways. One way is simply to have a rule a process is entitled only to a single resource at any moment. If it needs a second one, it must release the first one. For a process needs to copy a huge file from a tape to a printer, this restriction is unacceptable.

Circular Wait Condition - 2 provide a global numbering of all the resources The rule is : processes can request resources whenever they want to, but all requests must be made in numerical order. A process may request first a printer and then a tape drive, but it may not request first a plotter and then a printer. Although numerically ordering the resources eliminates the problem of deadlocks, it may be impossible to find an ordering satisfies everyone.

Circular Wait Condition Summary Summary of approaches to deadlock prevention

Two-Phase Locking Useful in updating Phase One large number of records in a table Phase One process tries to lock all records it needs, one at a time if needed record was 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 To start program stop and restart later

End of Chapter 3 Deadllocks