Computer Science 4500 Operating Systems. In This Module. Deadlock Definition. Module 7 Deadlocks

Similar documents
Computer Science 4500 Operating Systems

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

CS Operating Systems

CS Operating Systems

Resources. Lecture 4. Deadlocks. Resources (2) Resources (1) Four Conditions for Deadlock. Introduction to Deadlocks

Chapter 3. Deadlocks

Chapter 3. Deadlocks

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

Operating Systems. Deadlock. User OS. Kernel & Device Drivers. Interface Programs. Brian Mitchell - Operating Systems

CISC 7310X. C10: Deadlocks. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 4/12/2018 CUNY Brooklyn College

Chapter 6. Deadlocks

Deadlocks. Today. Next Time. ! Resources & deadlocks! Dealing with deadlocks! Other issues. ! Memory management

Deadlocks. Today. Next Time. Resources & deadlocks Dealing with deadlocks Other issues. I/O and file systems

Operating Systems. Lecture3.2 - Deadlock Management. Golestan University. Hossein Momeni

Deadlocks Prof. James L. Frankel Harvard University. Version of 7:05 PM 7-Mar-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

Process P holds resource R. Process P is waiting for resource R. P Process P holds T and requests R. R Process Q holds R and requests T

Learning Outcomes. Chapter 3. Deadlocks. Resources. Resources. Resources

Learning Outcomes. Chapter 6. Deadlocks. Resources. Resources & Deadlocks. Resource Access. Two example resource usage patterns

Deadlocks. Thomas Plagemann. With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

6. Concurrency: Deadlock

Resources. Chapter 3. Deadlocks. Resources. Resources

!! What is a deadlock? !! What causes a deadlock? !! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

OPERATING SYSTEMS. After A.S.Tanenbaum, Modern Operating Systems, 3rd edition. Uses content with permission from Assoc. Prof. Florin Fortis, PhD

Deadlocks. Operating Systems. Resources. Deadlocks

Shared and Preemptable Resources. A few resources may be sharable. Anyone can write the system log.

Operating Systems ECE344. Ding Yuan

Deadlocks. Dr. Yingwu Zhu

Deadlocks. Tore Larsen. With slides from T. Plagemann, C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

UNIT 4 DEADLOCKS 4.0 INTRODUCTION

Chapter 3: Deadlocks

Deadlock and Starvation

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 7: Deadlocks. Operating System Concepts 8th Edition, modified by Stewart Weiss

DEADLOCKS M O D E R N O P E R A T I N G S Y S T E M S C H A P T E R 6 S P R I N G

CS450/550 Operating Systems

Introduction to OS. Deadlock. MOS Ch. 6. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

Concurrency problems. To do. q Non-deadlock bugs q Resources & deadlocks q Dealing with deadlocks q Other issues q Next Time: I/O and file systems

Deadlock. Rab Nawaz Jadoon DCS. Assistant Professor COMSATS Lahore Pakistan. Department of Computer Science

Chapter 7: Deadlocks 1

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem!

Deadlock and Starvation

Process Management. Deadlock. Process Synchronization. Management Management. Starvation

Concurrency problems. q Non-deadlock bugs q Resources & deadlocks q Dealing with deadlocks q Other issues q Next Time: I/O and file systems

COS 318: Operating Systems. Deadlocks. Jaswinder Pal Singh Computer Science Department Princeton University

Deadlocks. Mehdi Kargahi School of ECE University of Tehran Spring 2008

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

Deadlock 1 Chapter 6

The Deadlock Problem. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

The Slide does not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams.

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Deadlock

2. Which of the following resources is not one which can result in deadlocking processes? a. a disk file b. a semaphore c. the central processor (CPU)

ECE519 Advanced Operating Systems

Operating Systems. Deadlocks

11 Resource Management Deadlock and Starvation

Deadlock 1 Chapter 6

More on Synchronization and Deadlock

Operating System: Chap7 Deadlocks. National Tsing-Hua University 2016, Fall Semester

Deadlock. A Bit More on Synchronization. The Deadlock Problem. Deadlock Characterization. Operating Systems 2/7/2005. CSC 256/456 - Spring

Last Class: Monitors. Real-world Examples

Bridge Crossing Example

Potential Deadlock Example

OPERATING SYSTEMS. Deadlocks

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

Operating Systems. Deadlock. Lecturer: William Fornaciari Politecnico di Milano Homel.deib.polimi.

Deadlock Risk Management

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

CSE 380 Computer Operating Systems

Operating Systems. Operating Systems Sina Meraji U of T

Outline. Deadlock. Examples, Conditions, Strategies. Deadlock Prevention. Deadlock Avoidance. Banker s algorithm

Concurrency: Deadlock and Starvation. Chapter 6

Chapter 3: Deadlocks. Chapter 3

Back to synchronization

VI. Deadlocks. What is a Deadlock? Intended Schedule. Deadlock Problem (1) ???

VI. Deadlocks Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term

Deadlocks. System Model

Yet another synchronization problem

Chapter 6 - Deadlocks

COMP 346 WINTER Tutorial 9 DEADLOCK

Deadlock CS 241. March 19, University of Illinois

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

What is Deadlock? Two or more entities need a resource to make progress, but will never get that resource. Examples from everyday life:

DUH! Deadlocks. Concurrency Issues. The TENEX Case. If a process requests all systems buffers, operator console tries to print an error message

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Module 6: Deadlocks. Reading: Chapter 7

The Deadlock Problem

Chapter - 4. Deadlocks Important Questions

Introduction to Deadlocks

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Chapter 7: Deadlocks

Final Exam Review. CPSC 457, Spring 2016 June 29-30, M. Reza Zakerinasab Department of Computer Science, University of Calgary

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009!

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks

Lecture 7 Deadlocks (chapter 7)

System Model. Deadlocks. Deadlocks. For example: Semaphores. Four Conditions for Deadlock. Resource Allocation Graph

Transcription:

Computer Science 4500 Operating Systems Module 7 Deadlocks 2008 Stanley A. Wileman, Jr. Operating Systems Slide 1 In This Module Deadlock: Definition and Examples Deadlock: Models Deadlock: Prevention Algorithms 2008 Stanley A. Wileman, Jr. Operating Systems Slide 2 Deadlock 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. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 3 1

Deadlock Characteristics Deadlocks occur when multiple processes are attempting to make exclusive use of multiple resources. The resources that are the source of deadlock conflicts may be hardware resources, software resources, or both. Example: Process A gets exclusive access to resource R, B gets access to S. Then A requests S and B requests R DEADLOCK! 2008 Stanley A. Wileman, Jr. Operating Systems Slide 4 Consumable and Non-Consumable Resources We categorize resources involved in deadlocks in several ways. First, resources may be consumable or non-consumable. For example Primary memory is non-consumable, since after its use it is available for other uses. Messages from another process are consumable resources. When they arrive and are processed they disappear. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 5 Resource Preemption Resources may also be preemptable or nonpreemptable. By preemption, we mean as resource can be taken away from a process for a while (to satisfy the needs of another process) and later returned. For example Primary memory is a preemptable resource in virtual memory systems, since it can have its contents saved on secondary storage and later restored. A tape drive isn t normally considered to be a preemptable resource, as it would be very costly to share it. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 6 2

Typical Use of Non-Preemptable Resources 1. A process first requests exclusive use of the resource. If it is available, the process continues. Otherwise, the process blocks until it is available. If multiple units of the resource are available, any one of these is usually acceptable. 2. The process then uses the resource for an arbitrary time. 3. Finally, the process releases the resource, allowing a process waiting on the resource to continue. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 7 Outcomes for Resource Allocation Requests A resource allocation request may have several possible outcomes: The request may be successful, and the process can continue running. The resource may be unavailable, and the process will block. The resource may be unavailable, and the process receives an error indication. This last outcome is usually possible only if a process has indicated it does not wish to block if the resource is unavailable. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 8 Example Outcomes Let s consider the Win32 system call WaitForSingleObject. This call has two parameters: A handle to the object (a semaphore, for example). A timeout parameter. If the resource is unavailable, then if timeout is infinite, the calling thread will block. timeout = 0, the calling thread will return immediately with an appropriate error indication. timeout > 0 the thread will wait at most timeout for the resource to become available. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 9 3

How Are Resources Requested? Some system calls, like open, explicitly request the use of resources. Other resources may be requested with an explicit request system call (e.g. the UNIX system call fcntl is used to lock regions of a file). 2008 Stanley A. Wileman, Jr. Operating Systems Slide 10 Coffman: Four Conditions Necessary for Deadlock Mutual exclusion: each resource is either assigned to a single process or is unavailable. Hold and wait: processes holding resources may request and wait for additional resources. No preemption: resources previously allocated cannot be forcibly removed from a process. Circular wait: there must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 11 Deadlock Modeling Resource Allocation Graphs are directed graphs that can be used to illustrate deadlock conditions: Resources are shown as squares labeled with the resource name. Processes are shown as circles labeled with the process identification. An edge from a resource to a process shows the process owns the resource. An edge from a process to a resource shows the process is requesting use of the resource. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 12 4

Process A Holds Resource R A R 2008 Stanley A. Wileman, Jr. Operating Systems Slide 13 Process B Requests Resource S S B 2008 Stanley A. Wileman, Jr. Operating Systems Slide 14 Deadlock! A R S B 2008 Stanley A. Wileman, Jr. Operating Systems Slide 15 5

Deadlock Strategies Ignore the problem entirely Detect and recover Prevention Avoidance 2008 Stanley A. Wileman, Jr. Operating Systems Slide 16 Ignoring Deadlock Tanenbaum calls this the ostrich algorithm. This approach is appropriate if the cost of a deadlock is small compared to the cost of preventing or avoiding them. Many UNIX systems ignore deadlocks. Most mission-critical systems are designed with consideration for deadlock. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 17 Detect and Recover Detection requires that the system maintain a complete database of all resources and owner processes and all pending requests. This database must be in a form usable for an algorithm that can detect cycles in graphs. For systems with one unit of each resource type, a simple cycle-detection algorithm can be used. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 18 6

Deadlock Recovery Techniques for Recovery: Preempt resources - not always possible Kill a process (releasing its resources) and restart it from the last checkpoint Kill a process which can be run again with no adverse side effects (changes) Kill a process which breaks the deadlock 2008 Stanley A. Wileman, Jr. Operating Systems Slide 19 Deadlock Prevention Impose restrictions on processes so deadlocks are structurally impossible. Solution approaches are keyed to the four conditions necessary for deadlock. Ensuring that at least one of the conditions will be satisfied guarantees that deadlock will be impossible. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 20 Prevention: No Mutual Exclusion On the surface this doesn t appear to be viable. However some resources are never shared, but owned by a single process that then accepts requests for the use of that resource. Example: printer spooling. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 21 7

Prevention: Hold and Wait All processes would need to request all resources before they begin execution. Problem 1: this may not be possible, as some resource needs may be determined only at execution time. Problem 2: this approach doesn t lead to efficient use of resources. Alternate method: processes must give up resources if a new request would block. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 22 Prevention: No Preemption Preempting the use of an assigned resource is either costly or impossible for some resources. Examples: preempting a tape drive or printer Some resources may also be left with inconsistent states if they are preempted at arbitrary times. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 23 Prevention: Circular Wait Assign each resource a unique numeric code. Processes must request resources in increasing order of their numeric codes. This technique has been used with success in some systems. Due to Havender, Standard Allocation Patterns 2008 Stanley A. Wileman, Jr. Operating Systems Slide 24 8

Deadlock Avoidance Q. How do you avoid having people step in your rose garden? A1. Nicely ask each person who might accidentally step in it to please not do so. A2. Put up a fence some distance from the roses. It is the second approach that is taken by deadlock avoidance: avoid getting into situations that might result in deadlock. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 25 Resource Trajectories - I Draw a plot with one axis for each process. Each axis has increasing process progress (i.e. time) extending from the origin. In the appropriate regions on each axis draw orthogonal regions in which various resources are used. Intersecting regions with the same unit of the same resource are forbidden. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 26 Resource Trajectories - II Since progress in each process must be in one direction only, reaching a region in which any process progress is blocked by forbidden regions spells t-r-o-u-b-l-e! Such regions are called unsafe. Regions where at least one process can make progress are called safe. Unsafe states are not deadlocked states. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 27 9

Resource Trajectories - III Process B S & R DEADLOCK DEADLOCK S UNSAFE DEADLOCK R R & S Process A 2008 Stanley A. Wileman, Jr. Operating Systems Slide 28 Dijkstra s Banker s Algorithm Originally expressed in terms of florins. Idea: Customers express their maximum need for money in advance. The banker has enough cash to satisfy each request, but not necessarily all requests at the same time. Thus some customers may have to wait for others to repay their loans before getting their cash. Problem: how can the banker make safe loans? 2008 Stanley A. Wileman, Jr. Operating Systems Slide 29 The Banker s Algorithm Build a matrix that shows allocation to customers, and their maximum claim. We also keep track of the available cash. When a customer asks for more cash, the algorithm simulates making the loan, and then determines if all customers could eventually complete. If they can, then the real loan is given out to the customer. If not, the customer has to wait for the cash. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 30 10

A Safe State (10 available units) Who Allocated Max. Claim Andy 0 6 Barbara 0 5 Bill 0 4 Paula 0 7 With 10 available units, anyone can ask for and receive their maximum claim, after which they will be required to return it. All other users can then obtain their maximum claims and return them. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 31 Another Safe State (2 available units) Who Allocated Max. Claim Andy 1 6 Barbara 1 5 Bill 2 4 Paula 4 7 Bill can get 2 units and then repay his total of 4 units. Then either Barbara or Paula could get their maximum claim (and repay). Finally, Andy could get his maximum claim. Since all users can obtain their maximum claims, this is a safe state. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 32 An Unsafe State (1 available unit) Who Allocated Max. Claim Andy 1 6 Barbara 2 5 Bill 2 4 Paula 4 7 With this allocation of resources, there is no process that is guaranteed to be able to obtain its maximum claim. Of course, we don t know for sure that any process will make such a claim. Nevertheless, this is an unsafe state. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 33 11

Banker s Algorithm for Multiple Resources Generalization of the single resource algorithm Keep two matrices, each having rows labeled with process names and columns labeled with resource names One matrix records assigned resources The other matrix records resources still needed We also need to know total units of all resources in the system. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 34 Arrays for Multiple Resources E: a vector listing units of existing resources A: a vector listing available units of resources C: a matrix (processes by resources) listing current allocations of resources to processes R: a matrix (processes by resources) listing remaining resources that may be requested. C A E i j j j 2008 Stanley A. Wileman, Jr. Operating Systems Slide 35 The Algorithm s Steps 1. Look for a row with unmet needs less than or equal to the available resources. If no such row exists, system will deadlock. 2. Assume the process for the row selected in 1 gets the needed resources and eventually finishes. Mark it complete and release its resources to the available vector 3. Repeat steps 1 and 2 until all processes are marked complete or deadlock occurs. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 36 12

Resources assigned (C Matrix) A 3 0 1 1 B 0 1 0 0 C 1 1 1 0 D 1 1 0 1 E 0 0 0 0 2008 Stanley A. Wileman, Jr. Operating Systems Slide 37 Resources still needed (R Matrix) A 1 1 0 0 B 0 1 1 2 C 3 1 0 0 D 0 0 1 0 E 2 1 1 0 2008 Stanley A. Wileman, Jr. Operating Systems Slide 38 Summary vectors Existing resources (E): 6 3 4 2 Possessed resources: 5 3 2 2 Available resources (A): 1 0 2 0 2008 Stanley A. Wileman, Jr. Operating Systems Slide 39 13

Examples of the Banker s Algorithm B requests 1 unit of the third resource; this request leads to a safe state. Then E requests 1 unit of the third resource; this request would result in deadlock, so E must wait. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 40 Two-phase locking Frequently used in database systems with multiple concurrent users Process tries to lock all required records, one at a time. If successful, the record updates are done and all records are unlocked. If unsuccessful, existing locks are released and the sequence of locks is tried again. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 41 Next In the next module we ll begin our study of input/output. We will examine input/output hardware characteristics in general, and look at a few devices in some detail. We ll then study the way input/output devices are managed by the operating system and made available to application programs. 2008 Stanley A. Wileman, Jr. Operating Systems Slide 42 14