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

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

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

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

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

Chapter 3. Deadlocks

Chapter 3. Deadlocks

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

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

Chapter 6. Deadlocks

Chapter 3: Deadlocks

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

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

CS450/550 Operating Systems

Resources. Chapter 3. Deadlocks. Resources. Resources

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

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

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

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

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

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

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

Chapter 3: Deadlocks. Chapter 3

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

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

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

Computer Science 4500 Operating Systems

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

6. Concurrency: Deadlock

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

More on Synchronization and Deadlock

Operating Systems. Deadlocks

Deadlocks. Operating Systems. Resources. Deadlocks

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

Back to synchronization

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

Yet another synchronization problem

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

UNIT 4 DEADLOCKS 4.0 INTRODUCTION

Frequently asked questions from the previous class survey

CSE Opera+ng System Principles

Chapter 6 - Deadlocks

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

Chapter 7: Deadlocks 1

Operating Systems. Operating Systems Sina Meraji U of T

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.

Operating Systems ECE344. Ding Yuan

11 Resource Management Deadlock and Starvation

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

CMPT 300 Introduction to Operating Systems

Module 6: Deadlocks. Reading: Chapter 7

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

System Model. Types of resources Reusable Resources Consumable Resources

Deadlock and Starvation

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

Deadlock 1 Chapter 6

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Administrivia. Assignments 0 & 1 Class contact for the next two weeks Next week. Following week

CS Operating Systems

CS Operating Systems

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.

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

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

Chapter 7: Deadlocks CS370 Operating Systems

OPERATING SYSTEMS. Deadlocks

Comp 204: Computer Systems and Their Implementation. Lecture 9: Deadlock

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 6. Deadlock. (A Quick Introduction)

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

Deadlocks. Dr. Yingwu Zhu

We need a formal algorithm, as visual inspection is not sufficient Many algorithms for detecting cycles in a directed graph are known o Below is a

Chapter 7: Deadlocks

Chapter 6 Concurrency: Deadlock and Starvation

Deadlock and Starvation

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

CS420: Operating Systems. Deadlocks & Deadlock Prevention

The Deadlock Problem

Chapter 7: Deadlocks

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Operating Systems. Deadlocks. Stephan Sigg. November 30, Distributed and Ubiquitous Systems Technische Universität Braunschweig

Chapter 7: Deadlocks

The Deadlock Problem (1)

Chapter 8: Deadlocks

Chapter 8: Deadlocks. The Deadlock Problem. System Model. Bridge Crossing Example. Resource-Allocation Graph. Deadlock Characterization

Principles of Operating Systems

The Deadlock Problem

Deadlock 1 Chapter 6

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Bridge Crossing Example

ECS 150 (Operating Systems) Goal To examine what causes deadlock, and what to do about it. Spring Quarter

Module 7: Deadlocks. The Deadlock Problem. Bridge Crossing Example. System Model

Deadlocks. Prepared By: Kaushik Vaghani

Deadlock. Chapter Objectives

Chap. 3. Input/Output

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

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Transcription:

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

That s some catch, that Catch-22 Thread A: lock(l1); lock(l2);... A Thread B: lock(l2); lock(l1);... 1 2 B A set of threads is deadlocked if each thread in the set is waiting for an event that only another thread in the set can cause None of the threads can run release resources be awakened 2

Introduction to deadlocks Assumptions Threads or single-threaded processes There are no interrupts possible to wake up a blocked thread Another cute example When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up until the other has gone. An actual law passed by the Kansas legislature 3

Conditions for deadlock 1. Mutual exclusion - Each resource assigned to 1 thread or available 2. Hold and wait - A thread holding resources can request others resources 3. No preemption - Previously granted resources cannot forcibly be taken away 4. Circular wait A circular chain of 2+ threads, each waiting for resource held by the next thread All conditions must hold for a deadlock to occur. Each of the 1-3 conditions is associated with a policy the system can or not have; break one condition no deadlock 4

System model System a collection of resources to be shared Resources, in types with multiple instances each (printers, files, memory, ) Resources can be Preemptable - can be taken away w/o ill effects (e.g. memory) Nonpreemptable - process will fail if resource were taken away (e.g. CD recorder) A thread must request a resource before using it & release it once done (open/close, malloc/free, ) Sequence of events to use a resource: request/use/release 5

Deadlock modeling Modeled with directed graphs Process B is requesting/waiting for resource S Resource R assigned to process A Process C & D in deadlock over resources T & U S R D Request Assignment T U B A C Generalizing to multiple resource instances per class A R 6

Basic facts If graph contains no cycles no deadlock. If graph contains a cycle if only one instance per resource type, then deadlock. if several instances per resource type, maybe a deadlock. Deadlock here R1 R2 No deadlock here R1 B A B C C A R2 R4 R2 D 7

Deadlock modeling Clearly, the ordering of operations plays a role and one particular ordering Requests and releases of each process 1.A requests R 2.B requests S 3.C requests T 4.A requests S 5.B requests T 6.C requests R. deadlock A Request R Request S Release R Release S A B Request S Request T Release S Release T B C Request T Request R Release T Release R C But with an alternative 1.A requests R 2.C requests T 3.A requests S 4.C requests R 5.A releases R 6.A releases S... no deadlock R S T 8

Dealing with deadlocks Possible strategies Ignore the problem altogether ostrich algorithm Detection and recovery do not stop it; let it happen, detect it and recover from it Dynamic avoidance careful resource allocation Prevention negating one of the four necessary conditions 9

The ostrich algorithm Pretend there is no problem Reasonable if deadlocks occur very rarely cost of prevention is high UNIX s & Windows approach A clear trade off between Convenience Correctness Not quite an option for your code 10

Deadlock detection single instance R Detect a cycle in the directed graph Simplest case How, when & what A B A linked 1.L empty list of nodes all arcs set as unmarked 2.For each node N /* depth-first search */ 2.1.Add N to L & check if N in L twice there s a deadlock; exit 2.2.Pick one arc at random, mark it & follow it to next current node 3.At end, if no arc no deadlock C S F D U T E V Arcs: A S, A R, B T, C S D S, D T, E V, E T F S, F W, G V, G V W G L:[R], L:[R,A], L:[R,A,S] L:[B], L:[B,T], L:[B,T,E], 11

Detection - multiple instances n processes, m classes of resources E vector of existing resources A vector of available resources C matrix of currently allocated resources R request matrix C ij P i holds C ij instances of resource class j R ij P i wants C ij instances of resource class j Invariant Σ i C ij + A j = E j (Currently allocated + available = existing) i.e. all resources are either allocated or available Algorithm: Idea: See if there s any process that can be run to completion with available resources, mark it and free its resources All processes unmarked 1.Look for unmarked process P i for which R i A 2.If found, add C i. to A, mark the process and go to 1 3.If not, exit All unmarked processes, if any, are deadlock 12

Detection (existing) (available) E = ( 4 2 3 1) A = ( 2 1 0 0 ) What process 1 needs 0 0 1 0 C = R = 2 0 0 1 2 0 0 1 1 0 1 0 0 1 2 0 2 1 0 0 Algorithm: What process 1 has Three processes and 4 resource types After running process 3 A = (2 2 2 0) Now you can run process 2 A = (4 2 2 1) All processes unmarked 1.Look for unmarked process P i for which R i A 2.If found, add C i. to A, mark the process and go to 1 3.If not, exit All unmarked processes, if any, are deadlock 13

When to check & what to do When to try Every time a resource is requested Every fixed period of times or when CPU utilization drops What to do then - recovery Through preemption depends on nature of the resource Through rollback Need to checkpoint processes periodically By killing a process Crudest but simplest way to break a deadlock Kill one in or not in the deadlock cycle 14

Deadlock avoidance Dynamically make sure not to get into a deadlock Two process resource trajectories Every point in the graph, a joint state of the processes Your only option here is to run A up to I 4 B * u (Both processes done) printer I 8 I 7 impossible impossible plotter I 6 I 5 t deadlock r sunsafe p q I 1 I 2 I 3 I 4 A printer plotter 15

Safe and unsafe states Safe if There is no deadlock There is a scheduling order by which all processes can finish Un-safe is not deadlock just no guarantee Example with one resource (10 instances of it) Free: 3 Has Needs Has Needs Has Needs Has Needs Has Needs A 3 9 A 3 9 A 3 9 A 3 9 A 3 9 Safe B 2 4 C 2 7 B 4 4 C 2 7 B 0 - C 2 7 B 0 - C 7 7 B 0 - C 0 - Free: 1 Free: 5 Free: 0 Free: 7 Free: 3 Has Needs A 3 9 Has Needs A 4 9 Has Needs A 4 9 Has Needs A 4 9 Unsafe B 2 4 B 2 4 B 4 4 B 0 - C 2 7 C 2 7 C 2 7 C 2 7 A requests and is granted another instance Free: 2 Free: 0 Free: 4 In retrospect, A s request should not have been granted 16

Banker's algorithm (Dijkstra, again) Considers Each request as it occurs Sees if granting it leads to a safe state i.e. there are enough resources to satisfy one customer With multiple resources 1.Look for a row R i A, if none the system will eventually deadlock 2.If found, mark P i and add C i to A 3.Repeat until processes are terminated or a deadlock occurs Very cute, but mostly useless Most processes don t know in advance what they need The lists of processes and resources are not static Processes may depend on each other 17

Deadlock prevention Avoidance is pretty hard or impossible Can we break one of the condition? Mutual exclusion Hold & wait No preemption Not a viable option How can you preempt a printer? Circular wait 18

Attacking mutual exclusion Some devices can be spooled (printer) Only the printer daemon uses printer resource Thus deadlock for printer eliminated But not all devices can be spooled process table? Principle: Assigning resource only when absolutely necessary Reduce number of processes that may claim the resource 19

Attacking hold & wait Processes request all resources at start (hold & wait) Process never has to wait for what it needs But May not know required resources at start It ties up resources others could be using Variation (hold & wait) Process must release all resources to request a new one 20

Attacking circular wait Impose total order on resources Processes request resources in order If all processes follow order, no circular wait occurs Deadlock if i A j & j B i If i < j then A j A B Process cannot request resource lower than what it s holding Advantage - Simple Disadvantage - Arbitrary ordering i j 21

Related issues Two-phase locking gather all locks, work & free all If you cannot get all, drop all you have and start again Non-resource deadlocks Each is waiting for the other to do some task E.g. communication deadlocks: A sends a request and blocks until B replies, message gets lost! Timeout! Livelocks try, sleep and try again There s some action, just not progress Starvation SJF to allocate resources consider allocation of a printer May cause long job to be postponed indefinitely FIFO? even though not blocked 22

Next time I/O devices, file abstraction and file systems 23