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

Similar documents
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

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

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

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

Chapter 3. Deadlocks

Chapter 3. Deadlocks

Common Concurrency Problems. Dongkun Shin, SKKU

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 3: Deadlocks

Chapter 6. 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 3. Deadlocks. Resources. Resources. Resources

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

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

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

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

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

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

CONCURRENCY: DEADLOCK. Shivaram Venkataraman CS 537, Spring 2019

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Announcements. Cond Var: Review

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

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

Chapter 3: Deadlocks. Chapter 3

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

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

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

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

6. Concurrency: Deadlock

[537] Concurrency Bugs. Tyler Harter

More on Synchronization and Deadlock

32. Common Concurrency Problems.

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

Operating Systems. Deadlocks

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

Concurrency Bugs. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

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

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. 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

Yet another synchronization problem

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

11 Resource Management Deadlock and Starvation

CMPT 300 Introduction to Operating Systems

Deadlock 1 Chapter 6

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

Chapter 6 - Deadlocks

Chapter 7: Deadlocks 1

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Operating Systems. Operating Systems Sina Meraji U of T

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

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

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

Deadlock 1 Chapter 6

Chapter 7: Deadlocks

The Deadlock Problem

OPERATING SYSTEMS. Deadlocks

UNIT 4 DEADLOCKS 4.0 INTRODUCTION

Deadlocks. Dr. Yingwu Zhu

System Model. Types of resources Reusable Resources Consumable Resources

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

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.

Chapter 7: Deadlocks

CS Operating Systems

CS Operating Systems

The Deadlock Problem

CSE Opera+ng System Principles

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

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

Chapter 7: Deadlocks. Operating System Concepts with Java 8 th Edition

The Deadlock Problem (1)

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

CS370 Operating Systems

Deadlock and Starvation

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

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

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

Silberschatz, Galvin and Gagne 2013! CPU cycles, memory space, I/O devices! " Silberschatz, Galvin and Gagne 2013!

Chapter 7: Deadlocks

Deadlocks. Prepared By: Kaushik Vaghani

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chap. 3. Input/Output

Chapter 7: Deadlocks. Operating System Concepts 9th Edition DM510-14

COS 318: Operating Systems. Deadlocks

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

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

Chapter 8: Deadlocks

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

Transcription:

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

Bugs in some modern applications From some key apps, a summary of bugs * Application Description Non-deadlock Deadlock MySQL Database server 14 9 Apache Web server 13 4 Mozilla Web browser 41 16 OpenOffice Office suite 6 2 Total 74 (~70%) 31 97% of non-deadlock bugs atomicity or order violations *S. Lu et al., Learning from Mistakes, http://pages.cs.wisc.edu/~shanlu/paper/asplos122-lu.pdf (examples from Arapci-Duseau s book) 2

Non-deadlock bugs Atomicity-violation // From MySQL Thread 1:: If (thd->proc_info) { } fputs(thd->proc_info, ); Thread 2:: thd->proc_info = NULL; Where s the bug? The desired serializability among multiple memory accesses is violated (i.e., a code region is intended to be atomic, but the atomicity is not enforced during execution) 3

Non-deadlock bugs Atomicity-violation pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; Thread 1:: pthread_mutex_lock(&lock); If (thd->proc_info) { fputs(thd->proc_info, ); } pthread_mutex_unlock(&lock); Thread 2:: pthread_mutex_lock(&lock); thd->proc_info = NULL; pthread_mutex_unlock(&lock); 4

Non-deadlock bugs Order-violation Thread 1:: Where s the bug? void init() { mthread = PR_CreateThread(mMain, ); } Thread 2:: void mmain() { mstate = mthread->state; } The desired order between two (groups of) memory accesses is flipped (i.e., A should always be executed before B, but the order is not enforced during execution). 5

Non-deadlock bugs Order-violation pthread_mutex_t mtlock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t mtcond = PTHREAD_COND_INITIALIZER; int mtinit = 0; Thread 1:: void init() { mthread = PR_CreateThread(mMain, ); } // Signal that the thread has been created pthread_mutex_lock(&mtlock); mtinit = 1; pthread_cond_signal(&mtcond); pthread_mutex_lock(&mtlock); 6

Non-deadlock bugs Order-violation Thread 2:: void mmain() { // Wait for the thrad to be initialized pthread_mutex_lock(&mtlock); while (mtinit == 0) pthread_cond_wait(&mtcond, &mtlock); pthread_mutex_unlock(&mtlock); } mstate = mthread->state; 7

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

System model System Set of threads or single-threaded processes A collection of resources to be shared Resources different types with multiple instances per (printers, files, memory, ) Preemptable - can be taken away w/o problem (e.g. memory) Nonpreemptable - process will fail if (e.g. CD recorder) A thread must Request a resource before using it Release it once done (open/close, malloc/free, ) 9

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 Multiple resource instances per class A R 10

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 11

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 12

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 13

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 14

Deadlock detection single instance R Detect a cycle in the directed graph Simplest case How, when & what A B 1.L empty A linked 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 D T E Arcs: A S, A R, B T, C S D S, D T, E V, E T F U V 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], 15

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 deadlocked 16

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 deadlocked 17

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 18

Deadlock avoidance 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 19

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 20

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 21

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 22

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 23

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 24

Attacking circular wait Idea Impose total (or partial) order on resources Processes request resources in order (can t request resources w/ lower id than what it has) If all processes follow order, no circular wait occurs Deadlock if i A j & j B i If i < j then A j A i B j Pros/cons Simple Arbitrary ordering http://elixir.free-electrons.com/linux/latest/source/mm/filemap.c /* * Lock ordering: * * ->i_mmap_rwsem * ->private_lock * ->swap_lock * ->mapping->tree_lock * 25

Related issues 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, try again Activity but no progress Starvation SJF to allocate resources consider allocation of a printer May cause long job to be postponed indefinitely FIFO? even though not blocked pthread_mutex_loc(l1); If (pthread_mutex_trylock(l2)!= 0) { pthread_mutex_unlock(l1); goto top; } 26

Next time I/O devices File abstraction and File systems First Hard Disk IBM Model 350 Disk File, part of IBM 305 RAMAC computer in 1956 ~5MB capacity 27