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

Similar documents
Common Concurrency Problems. Dongkun Shin, SKKU

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

Chapter 3. Deadlocks

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

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

Linux kernel synchronization. Don Porter CSE 506

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

More on Synchronization and Deadlock

Chapter 3. Deadlocks

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

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

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

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

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

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

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

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

Operating Systems. Deadlocks

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

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

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

l12 handout.txt l12 handout.txt Printed by Michael Walfish Feb 24, 11 12:57 Page 1/5 Feb 24, 11 12:57 Page 2/5

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

Linux kernel synchronization

6. Concurrency: Deadlock

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

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

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

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

Computer Science 4500 Operating Systems

Chapter 6. Deadlocks

Deadlock and Starvation

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

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

CS450/550 Operating Systems

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

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

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

Resources. Chapter 3. Deadlocks. Resources. Resources

Last Class: Monitors. Real-world Examples

OPERATING SYSTEMS. Deadlocks

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

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

Concurrency: Deadlock and Starvation. Chapter 6

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

Deadlock and Starvation

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 3: Deadlocks

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.

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Bridge Crossing Example

Chapter 6 Concurrency: Deadlock and Starvation

The Deadlock Problem

CSCE Operating Systems Deadlock. Qiang Zeng, Ph.D. Fall 2018

Back to synchronization

ECE519 Advanced Operating Systems

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

CS Operating Systems

CS Operating Systems

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

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

Yet another synchronization problem

CMPT 300 Introduction to Operating Systems

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

System Model. Types of resources Reusable Resources Consumable Resources

The deadlock problem

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

Operating Systems ECE344. Ding Yuan

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

Chris Rossbach, Owen Hofmann, Don Porter, Hany Ramadan, Aditya Bhandari, Emmett Witchel University of Texas at Austin

Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock

Chapter - 4. Deadlocks Important Questions

Concurrency: Principles of Deadlock. Processes and resources. Concurrency and deadlocks. Operating Systems Fall Processes need resources to run

CSE 380 Computer Operating Systems

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

Concurrency: Deadlock and Starvation. Chapter 6

The Deadlock Problem (1)

CS370 Operating Systems

CS370: Operating Systems [Spring 2017] Dept. Of Computer Science, Colorado State University

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlocks. Operating Systems. Resources. Deadlocks

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

CS370 Operating Systems

Deadlock. Concepts to discuss. A System Model. Deadlock Characterization. Deadlock: Dining-Philosophers Example. Deadlock: Bridge Crossing Example

Contents. Chapter 8 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

Operating Systems CMPSC 473. Deadlocks February 28, Lecture 13 Instructor: Trent Jaeger

Chapter 6. Deadlock. (A Quick Introduction)

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

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

Deadlocks: Detection & Avoidance

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

The Deadlock Lecture

Chapter 7: Deadlocks 1

Chapter 6 Concurrency: Deadlock and Starvation

Transcription:

Deadlock

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

Deadlocks Thread 1: lock(l1); lock(l2);! Thread 2: lock(l2); lock(l1);

Examples?

Examples? GFP_IO flag in Linux Kernel Memory allocator Causes get_free_page() calls shm_swap() to pageout. What if shm_swap() needs memory?

Examples? GFP_IO flag in Linux Kernel Memory allocator Causes get_free_page() calls shm_swap() to pageout. What if shm_swap() needs memory? Vector v1, v2; v1.addall(v2); addall() needs to be thread safe. what happens when v2.addall(v1) is called too?

Conditions

Conditions Mutual Exclusion threads claim exclusive control

Conditions Mutual Exclusion threads claim exclusive control Hold-and-Wait resources acquired progressively

Conditions Mutual Exclusion threads claim exclusive control Hold-and-Wait resources acquired progressively No preemption resources cannot be forcibly taken away

Conditions Mutual Exclusion threads claim exclusive control Hold-and-Wait resources acquired progressively No preemption resources cannot be forcibly taken away Circular Wait circular chain such that threads wait on resources held by another

Dining Philosophers http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png

Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png

Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) Philosopher picks left fork then picks right (hold and wait) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png

Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) Philosopher picks left fork then picks right (hold and wait) Fork cannot be taken away from a philosopher (no preemption) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png

Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) Philosopher picks left fork then picks right (hold and wait) Fork cannot be taken away from a philosopher (no preemption) Once all philosophers pick a fork, each of them wait on the philosopher at his right to release the fork (circular wait) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png

Strategies

Ignore - Ostrich algorithm Strategies

Strategies Ignore - Ostrich algorithm Detection & Recovery

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention Negate any one of the deadlock conditions

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention Negate any one of the deadlock conditions Avoidance

Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention Negate any one of the deadlock conditions Avoidance Carefully allocate resources

Prevention - Mutual Exclusion Never assign resource exclusively to a process Not really realistic. ex. printers can t print two documents simultaneously Use daemon to exclusively access printer? Deadlock free? Use hardware instructions, compare and swap

Prevention - Hold & Wait down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);

Prevention - Hold & Wait Ensure that all resources are acquired at once down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);

Prevention - Hold & Wait Ensure that all resources are acquired at once Is this feasible? down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);

Prevention - Hold & Wait Ensure that all resources are acquired at once Is this feasible? Know all resources in advance? down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);

Prevention - No Preemption

Prevention - No Preemption Take away resources owned by thread

Prevention - No Preemption Take away resources owned by thread Use trylock(), non blocking call in getting the lock.

Prevention - No Preemption Take away resources owned by thread Use trylock(), non blocking call in getting the lock. top: lock(get_left(i); if (trylock(get_right(i) == -1) { unlock(get_left(i)); goto top; }

Prevention - No Preemption Take away resources owned by thread Use trylock(), non blocking call in getting the lock. top: lock(get_left(i); if (trylock(get_right(i) == -1) { unlock(get_left(i)); goto top; } Lifelock?

Prevention - Circular Wait http://lxr.free-electrons.com/source/mm/filemap.c

Prevention - Circular Wait Enforce ordering in Locking http://lxr.free-electrons.com/source/mm/filemap.c

Prevention - Circular Wait Enforce ordering in Locking enumerate all locks in the system? http://lxr.free-electrons.com/source/mm/filemap.c

Prevention - Circular Wait Enforce ordering in Locking enumerate all locks in the system? 62 /* 63 * Lock ordering: 64 * 65 * ->i_mmap_rwsem (truncate_pagecache) 66 * ->private_lock ( free_pte-> set_page_dirty_buffers) 67 * ->swap_lock (exclusive_swap_page, others) 68 * ->mapping->tree_lock 69 * 70 * ->i_mutex 71 * ->i_mmap_rwsem (truncate->unmap_mapping_range) 72 * 73 * ->mmap_sen 74 * ->i_mmap_rwsem 75 * ->page_table_lock or pte_lock (various, mainly in memory.c) 76 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) 77 * 78 * ->mmap_sem 79 * ->lock_page (access_process_vm) 80 * 81 * ->i_mutex (generic_perform_write) 82 * ->mmap_sem (fault_in_pages_readable->do_page_fault) 83 * 84 * bdi->wb.list_lock 85 * sb_lock (fs/fs-writeback.c) 86 * ->mapping->tree_lock ( sync_single_inode) 87 * 88 * ->i_mmap_rwsem 89 * ->anon_vma.lock (vma_adjust) 90 * 91 * ->anon_vma.lock 92 * ->page_table_lock or pte_lock (anon_vma_prepare and various) 93 * 94 * ->page_table_lock or pte_lock 95 * ->swap_lock (try_to_unmap_one) http://lxr.free-electrons.com/source/mm/filemap.c

Prevention - Circular Wait Enforce ordering in Locking enumerate all locks in the system? Is it feasible? 62 /* 63 * Lock ordering: 64 * 65 * ->i_mmap_rwsem (truncate_pagecache) 66 * ->private_lock ( free_pte-> set_page_dirty_buffers) 67 * ->swap_lock (exclusive_swap_page, others) 68 * ->mapping->tree_lock 69 * 70 * ->i_mutex 71 * ->i_mmap_rwsem (truncate->unmap_mapping_range) 72 * 73 * ->mmap_sen 74 * ->i_mmap_rwsem 75 * ->page_table_lock or pte_lock (various, mainly in memory.c) 76 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) 77 * 78 * ->mmap_sem 79 * ->lock_page (access_process_vm) 80 * 81 * ->i_mutex (generic_perform_write) 82 * ->mmap_sem (fault_in_pages_readable->do_page_fault) 83 * 84 * bdi->wb.list_lock 85 * sb_lock (fs/fs-writeback.c) 86 * ->mapping->tree_lock ( sync_single_inode) 87 * 88 * ->i_mmap_rwsem 89 * ->anon_vma.lock (vma_adjust) 90 * 91 * ->anon_vma.lock 92 * ->page_table_lock or pte_lock (anon_vma_prepare and various) 93 * 94 * ->page_table_lock or pte_lock 95 * ->swap_lock (try_to_unmap_one) http://lxr.free-electrons.com/source/mm/filemap.c

Deadlock Avoidance Banker s algorithm - Dijkstra (1965) Safe and unsafe states Checks to see if any request would yield a safe state If not, the request is denied

Banker s algorithm

Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build

Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources

Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources Pick a row whose resource needs are smaller or equal to A. If not found, system would eventually deadlock as resources cannot be found.

Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources Pick a row whose resource needs are smaller or equal to A. If not found, system would eventually deadlock as resources cannot be found. Assuming the process makes a maximum allocation of its resources, consider the process as terminated and add its resources to the A vector

Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources Pick a row whose resource needs are smaller or equal to A. If not found, system would eventually deadlock as resources cannot be found. Assuming the process makes a maximum allocation of its resources, consider the process as terminated and add its resources to the A vector Repeat until all processes are terminated.

Problem #1 Is the State Safe? E = (6, 3, 4, 2) Allocated: A B C D P0 3 0 1 1 P1 0 1 0 0 P2 1 1 1 0 P3 1 1 0 1 P4 0 0 0 0 Needed: A B C D P0 1 1 0 0 P1 0 1 1 2 P2 3 1 0 0 P3 0 0 1 0 P4 2 1 1 0

Problem #2 Is the state safe? E = (10, 5, 7) Allocated: A B C P0 0 1 0 P1 2 0 0 P2 3 0 2 P3 2 1 1 P4 0 0 2 Max: A B C P0 7 5 3 P1 3 2 2 P2 9 0 2 P3 2 2 2 P4 4 3 3

Problem #3 Is the following state safe? E = (6, 5, 7, 6) Allocated: A B C D Max: A B C D P0 1 2 2 1 P0 3 3 2 2 P1 1 0 3 3 P1 1 2 3 4 P2 1 2 1 0 P2 1 3 5 0

Done!!!