EECE.4810/EECE.5730 Operating Systems

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks Adapted by Donghui Zhang from the original version by Silberschatz et al.
Advertisements

Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6 Concurrency: Deadlock and Starvation
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 12: Deadlocks (Chapter 7)
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.
CSC 322 Operating Systems Concepts Lecture - 29: by
Deadlock CSCI 444/544 Operating Systems Fall 2008.
02/18/2008CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Deadlock Prevention CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
1 Concurrency: Deadlock and Starvation Chapter 6.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 7: Deadlocks.
CPS110: Deadlock Landon Cox February 5, Concurrency so far  Mostly tried to constrain orderings  Locks, monitors, semaphores  It is possible.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided?
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Deadlock cs550 Operating Systems David Monismith.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
CS333 Intro to Operating Systems Jonathan Walpole.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Informationsteknologi Monday, October 1, 2007Computer Systems/Operating Systems - Class 111 Today’s class Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Process Management Deadlocks.
Synchronization Deadlocks and prevention
Synchronization Deadlocks and prevention
Operating systems Deadlocks.
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
CS703 - Advanced Operating Systems
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Chapter 7 – Deadlock and Indefinite Postponement
Chapter 6 : Deadlocks What is a Deadlock?
Deadlocks (part II) Deadlock avoidance – (not reasonable)
Operating System: DEADLOCKS
Introduction to Operating Systems
Operating systems Deadlocks.
CS 333 Introduction to Operating Systems Class 7 - Deadlock
Andy Wang Operating Systems COP 4610 / CGS 5765
EECE.4810/EECE.5730 Operating Systems
SE350: Operating Systems Lecture 5: Deadlock.
Lecture 26 Syed Mansoor Sarwar
CSCI 315 Operating Systems Design
CENG334 Introduction to Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
CSE 542: Operating Systems
CSE 542: Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

EECE.4810/EECE.5730 Operating Systems Instructor: Dr. Michael Geiger Spring 2019 Lecture 16: Deadlock

Operating Systems: Lecture 16 Lecture outline Announcements/reminders Program 2 due 3/20 3/20 = Wednesday after Spring Break Start the program early! Exams to be returned Friday Today’s lecture Review: synchronization primitives Deadlock 7/19/2019 Operating Systems: Lecture 16

Review: Comparing monitors/semaphores Semaphores provide 1 mechanism for both mutual exclusion and ordering, while monitors use separate mechanisms for each Elegant mechanism Can be difficult to use Monitor lock = binary semaphore (init to 1) lock() = down() unlock() = up() Monitor CV = binary semaphore (init to 0) down() on semaphore initiates wait up() on semaphore signals 1+ threads to wake up 7/19/2019 Operating Systems: Lecture 16

Operating Systems: Lecture 16 Deadlock Resources: something a thread needs and will therefore wait for Locks, disk space, memory, CPU Deadlock Cyclical wait for resources, which prevents involved threads from making progress Threads don’t release resources until they’re done Example: Thread A Thread B x.lock y.lock y.lock x.lock … … y.unlock x.unlock x.unlock y.unlock 7/19/2019 Operating Systems: Lecture 16

Dining-Philosophers Problem Philosophers spend their lives alternating thinking and eating Don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl Need both to eat, then release both when done In the case of 5 philosophers Shared data Bowl of rice (data set) Semaphore chopstick [5] initialized to 1 (could be set of locks, too) 7/19/2019 Operating Systems: Lecture 16

Dining-Philosophers Problem Algorithm The structure of Philosopher i: do { down(chopstick[i] ); down(chopStick[ (i + 1) % 5] ); // eat up(chopstick[i] ); up(chopstick[ (i + 1) % 5] ); // think } while (TRUE); What is the problem with this algorithm? 7/19/2019 Operating Systems: Lecture 16

Generic multithreaded program 7/19/2019 Operating Systems: Lecture 16

Necessary deadlock conditions Limited resource: not enough to serve all threads simultaneously Hold and wait: threads hold resources while waiting to acquire other resources No preemption: thread system can’t force one thread to give up resources Cyclical chain of requests How does Dining Philosophers problem meet all of these conditions? 7/19/2019 Operating Systems: Lecture 16

Operating Systems: Lecture 16 Handling deadlock General strategies Ignore (not very effective) Detect and fix Find cycles in resource graph Don’t allow requests that would cause cycle to occur Prevent 7/19/2019 Operating Systems: Lecture 16

Operating Systems: Lecture 16 Deadlock prevention Eliminate one of four necessary conditions Increase resources to decrease waiting Eliminate hold and wait: move resource acquisition to beginning If can’t (atomically) get all resources, release everything Phase 1a: acquire all resources Phase 1b: while (!done) {work} Phase 2: release all resources 7/19/2019 Operating Systems: Lecture 16

Operating Systems: Lecture 16 Banker’s Algorithm Similar to reserving all resources at beginning, but with more concurrency State maximum resource needs in advance (without acquiring) May block when thread attempts to acquire resource General structure Phase 1a: state maximum resource need Phase 1b: while (!done) { acquire some resource (block if not safe) work } Phase 2: release all resources 7/19/2019 Operating Systems: Lecture 16

Banker’s Algorithm example Model bank loaning money Bank has $6000 Customers establish credit limit (max $ needed) Customers borrow in stages and return all money when done Solution 1: Bank promises to give money immediately upon request, up to limit Ann asks for $2000 limit Bob asks for $4000 limit Charlie asks for $6000 limit Can bank approve all of them? 7/19/2019 Operating Systems: Lecture 16

Banker’s Algorithm example (2) Solution 2: Bank approves all limits, but may block individual requests Ann requests $2000 limit (bank ok’s) Bob requests $4000 limit (bank ok’s) Charlie requests $6000 limit (bank ok’s) Ann takes out $1000 ($5000 left in bank) Bob takes out $2000 ($3000 left in bank) Charlie wants to take out $2000 Can this request be satisfied? Is there a way for all threads to get maximum resources and therefore finish? 7/19/2019 Operating Systems: Lecture 16

Banker’s Algorithm example (3) Prior example can finish in following order Charlie takes out $2000 ($1000 left in bank) Ann takes out $1000, then finishes ($2000 left in bank) Bob takes out $2000, then finishes ($4000 left in bank) Charlie takes out $4000, then finishes ($6000 left in bank 7/19/2019 Operating Systems: Lecture 16

Banker’s Algorithm example (4) Another scenario Ann requests $2000 limit (bank ok’s) Bob requests $4000 limit (bank ok’s) Charlie requests $6000 limit (bank ok’s) Ann takes out $1000 ($5000 left in bank) Bob takes out $2000 ($3000 left in bank) Charlie wants to take out $2500 Request won’t be satisfied—no way for all to finish Algorithm allows system to overcommit resources without deadlock Sum of max resource needs can be greater than total resources as long as threads can finish Difficult to anticipate maximum resources 7/19/2019 Operating Systems: Lecture 16

Operating Systems: Lecture 16 Deadlock example Given function from MT bank software package used to transfer money Assume global array of locks, 1 lock per account locks[i] = lock for account # i void transfer_money(int src_acct, int dest_acct, int amt) { locks[src_acct].lock(); // Lock account sending money locks[dest_acct].lock(); // Lock account receiving money <transfer money> locks[dest_acct].unlock(); locks[src_acct].unlock(); } Describe how this function can deadlock if called in multiple threads How can we rewrite function to remove deadlock by: Removing hold and wait? Removing circular wait? 7/19/2019 Operating Systems: Lecture 16

Example solution: deadlock condition 2 threads access same accounts Account 1: source in one case, dest in other Account 2: dest in one case, source in other For example T1: transfer_money(1, 2, 1000); T2: transfer_money(2, 1, 500); Following interleaving deadlocks: T1: locks[1].lock(); // T1 gets locks[1] T2: locks[2].lock(); // T2 gets locks[2] T1: locks[2].lock(); // T1 blocks T2: locks[1].lock(); // T2 blocks 7/19/2019 Operating Systems: Lecture 16

Example solution: remove hold and wait Force thread to give up all locks if one lock call unsuccessful Pseudo-code: while (both locks not acquired) { locks[src_acct].lock(); if (locks[dest_acct].lock() fails) locks[src_acct].unlock(); } 7/19/2019 Operating Systems: Lecture 16

Example solution: remove circular wait Impose ordering on locks so all threads acquire in same order Possible solution: always acquire lowest-numbered lock first if (src_acct < dest_acct) { locks[src_acct].lock(); locks[dest_acct].lock(); } else { 7/19/2019 Operating Systems: Lecture 16

Operating Systems: Lecture 16 Final notes Next time Return exams Reminders: Program 2 due 3/20 3/20 = Wednesday after Spring Break Start the program early! 7/19/2019 Operating Systems: Lecture 16

Operating Systems: Lecture 16 Acknowledgements These slides are adapted from the following sources: Silberschatz, Galvin, & Gagne, Operating Systems Concepts, 9th edition Chen & Madhyastha, EECS 482 lecture notes, University of Michigan, Fall 2016 7/19/2019 Operating Systems: Lecture 16