COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

Size: px
Start display at page:

Download "COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall"

Transcription

1 COMP 150-CCP Concurrent Programming Lecture 12: Deadlock Dr. Richard S. Hall Concurrent programming February 28, 2008

2 Scenario Process 1 gets the lock for object A and wants to lock object B A B Process 2 gets the lock for object C and wants the lock for object A C Process 3 gets the lock for object B and wants the lock for object C What happens next?

3 Deadlock Concept When all processes in a system are waiting to acquire a shared resource (i.e., all of the processes are blocked), then the system is deadlocked. When a system is deadlocked, it is not possible to execute any actions or make any progress Each process is waiting for a resource to be released, but no process can make progress to release a held resource Deadlock is a serious issue in concurrent systems The goal is to design systems that are free from deadlock

4 Possibility of Deadlock Serially reusable resources The processes involved share resources which they use under mutual exclusion Incremental acquisition Processes hold on to resources already allocated to them while waiting to acquire additional resources No preemption Once acquired by a process, resources cannot be preempted (forcibly withdrawn) but are only released voluntarily

5 Existence of Deadlock Serially reusable resources Incremental acquisition No preemption Wait-for cycle A circular chain (or cycle) of processes exists such that each process holds a resource which its successor in the cycle is waiting to acquire Referred to as the four necessary and sufficient conditions for deadlock

6 Dealing With Deadlock Ignore the problem altogether Prevention Avoidance Detection and recovery

7 Deadlock Prevention (1/2) Eliminate one of the four necessary conditions Serially reusable resources Not possible to eliminate this, since access to some resources requires mutual exclusion Incremental acquisition Require that all resources are requested at one time Process may block for a long time waiting for all resources Resources may not be used for a long time Not always possible to know all resources in advance

8 Deadlock Prevention (2/2) No preemption Process must release resource and request again May preempt a process to require it releases its resources Wait-for cycle Define a linear ordering of resource types

9 Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock Requires knowledge of future thread resource requests Do not grant an incremental resource request to a thread if this allocation might lead to deadlock e.g., Banker's algorithm Only helps us when allocating with resources, will not help when deadlock occurs due to trying to acquire synchronized locks

10 Deadlock Detection & Recovery Let deadlock occur Try to detect when a deadlock occurs When a deadlock is detected, take some action to recover from it, e.g., Abort/stop all deadlocked threads Cannot really do this in Java Even if you could, you would likely leave the system in an inconsistent state unless you had some sort of rollback mechanism Try interrupting threads Threads would have to understand that an interrupt meant to let go Would not help if they were blocked trying to acquire a synchronized lock

11 Primitive Deadlock Analysis In an LTS graph deadlock is easily visible as a state with no outgoing arcs. In FSP we can achieve deadlock using the STOP process: MOVE = (north->(south->move north->stop)). Using the LTSA, we can find the deadlock through safety analysis: Trace to DEADLOCK: north north

12 Parallel Process Deadlock Analysis In real systems, deadlock may arise from the parallel composition of interacting processes. p:p q:q printer scanner printer scanner SYS printer: RESOURCE get put scanner: RESOURCE get put Deadlock trace? Avoidance? RESOURCE = (lock->unlock->resource). P = (printer.lock->scanner.lock ->copy->scanner.unlock ->printer.unlock->p). Q = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->q). SYS = (p:p q:q {p,q}::printer:resource {p,q}::scanner:resource ).

13 Deadlock Prevention One potential technique for preventing deadlock: If processes share different classes of resources, such as printers and scanners, a general purpose deadlock preventing strategy is to order the resource classes so every process acquires them in the same order. RESOURCE = (lock->unlock->resource). P = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->p). Q = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->q). SYS = (p:p q:q {p,q}::printer:resource {p,q}::scanner:resource).

14 Deadlock Prevention Another potential technique for preventing deadlock: It is also possible to use time-out values to prevent deadlock. P = (printer.lock->getscanner), GETSCANNER = (scanner.lock->copy->scanner.unlock ->printer.unlock->p timeout->printer.unlock->p ). Q = (scanner.lock-> GETPRINTER), GETPRINTER = (printer.lock->copy->printer.unlock ->scanner.unlock->q timeout->scanner.unlock->q ). Deadlock? Progress?

15 Deadlock in Java Similar example in Java but without using locks: // Global space Semaphore scanner = new Semaphore(1); Semaphore printer = new Semaphore(1); // Thread 1 public void run() { printer.down(); } scanner.down(); // do work here scanner.up(); printer.up(); Get printer Get scanner Try scanner Try printer // Thread 2 public void run() { scanner.down(); } printer.down(); // do work here printer.up(); scanner.up();

16 Deadlock in Java Why does deadlock happen in this example? Because of how semaphores are implemented The down() method of each semaphore will wait() until the semaphore value is non-zero Since the two threads acquire the semaphores in the opposite order, it is possible to interleave their instructions such that one thread gets the scanner and one gets the printer and then they both must wait for each other to finish which will never happen This scenario can be resolved just like the model, use resource acquisition ordering It might also be possible to interrupt the threads here, since wait() is interruptible

17 Dining Philosophers Example Five philosophers sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the center of the table is a large bowl of spaghetti. A philosopher needs two forks to eat a helping of spaghetti One fork is placed between each pair of philosophers and they agree that each will only use the fork to his immediate right and left.

18 Dining Philosophers Structure Diagram Each FORK is a shared resource with get and put actions When hungry, each PHIL must first get his right fork and then his left fork before he can start eating.

19 Dining Philosopher Model FORK = (acquire->release->fork). PHIL = (hungry->right.acquire->left.acquire ->eat->right.release->left.release ->think->phil). Table of philosophers DINERS(N=5) = forall[i:0..n-1] (phil[i]:phil {phil[i].left,phil[((i-1)+n)%n].right}::fork ). Can this system deadlock?

20 Dining Philosopher Analysis Trace to DEADLOCK: phil.0.hungry phil.0.right.acquire phil.1.hungry phil.1.right.acquire phil.2.hungry phil.2.right.acquire phil.3.hungry phil.3.right.acquire phil.4.hungry phil.4.right.acquire This is the situation where all the philosophers become hungry at the same time, sit down at the table and each philosopher picks up the fork to his right. The system can make no further progress since each philosopher is waiting for a fork held by his neighbor, i.e., a wait-for cycle exists!

21 Dining Philosophers Implementation Deadlock is easily detected in our model. How easy is it to detect a potential deadlock in an implementation? \/ \ \/

22 Dining Philosophers Implementation Philosophers Forks Active entities, implement as threads Shared passive entities, implement as monitors

23 Dining Philosophers Implementation public class Fork { private boolean taken = false; private int identity; public Fork(int id) { identity = id; } taken encodes the state of the fork public synchronized void acquire() throws InterruptedException { while (taken) { wait(); } taken = true; } } public synchronized void release() { taken = false; notifyall(); }

24 Dining Philosophers Implementation class Philosopher extends Thread {... public void run() { try { while (true) { // No forks. setstate(thinking); Thread.sleep(50 * identity); // Get right fork. right.acquire(); setstate(gotright); Thread.sleep(50 * identity); // Get left fork. left.acquire(); setstate(gotboth); Thread.sleep(50 * identity); // Release forks. right.release(); left.release(); } } catch (InterruptedException ex) { } } }

25 Dining Philosophers Implementation Code to create the philosopher threads and fork monitors in Main: forks = new Fork[N]; phils = new Philosopher[N]; for (int i = 0; i < N; i++) { forks[i] = new Fork(i); } for (int i = 0; i < N; i++) { phils[i] = new Philosopher( this, i, forks[(i N) % N], forks[i]); new Thread(phils[i]).start(); }

26 Deadlock in Implementation Deadlock is possible in the implementation, but it might not be as easy to spot. The program can run for a long time without deadlocking. \ \ \ \ \

27 Fixing the Model We can fix the implementation by eliminating the wait-for cycle... How? Introduce an asymmetry into our definition of philosophers. Use the identity of each philosopher to make even numbered philosophers get their left forks first, odd their right first. Other strategies? PHIL(I=0) = (when (I%2==0) hungry ->left.acquire->right.acquire ->eat ->left.release->right.release ->think->phil when (I%2==1) hungry ->right.acquire->left.acquire ->eat ->left.release->right.release ->think->phil ).

28 Deadlock in Real Systems Important deadlock prevention heuristics from this lecture Ordered acquisition of different resources types Asymmetric acquisition of same resource types Both of these heuristics apply to real world systems, but following them alone will not always lead to deadlock free systems Sometimes wait-for cycles result from unexpected dependencies and circumstances of the system and environment's implementation Requires testing and diligence

Deadlock. INF2140 Parallel Programming: Lecture 6. March 07, INF2140 Parallel Programming: Lecture 6 Deadlock

Deadlock. INF2140 Parallel Programming: Lecture 6. March 07, INF2140 Parallel Programming: Lecture 6 Deadlock March 07, 2012 Concepts System deadlock: no further progress Four necessary & sufficient conditions Models - no eligible actions Practice Blocked threads Aim: deadlock avoidance - to design systems where

More information

Concurrency: Deadlock 1. Magee/Kramer 2 nd Edition

Concurrency: Deadlock 1. Magee/Kramer 2 nd Edition Concurrency: Deadlock 1 Concurrency: Deadlock 2 Chapter 6 Deadlock Concurrency: Deadlock 3 Deadlock Concepts: system deadlock: no further progress four necessary & sufficient conditions Models: deadlock

More information

Deadlock. Concepts: Models: Practice: Aim: deadlock avoidance - to design systems where deadlock cannot occur. Chapter 6. Deadlock

Deadlock. Concepts: Models: Practice: Aim: deadlock avoidance - to design systems where deadlock cannot occur. Chapter 6. Deadlock Chapter 6 Deadlock Deadlock Concepts: Models: Practice: system deadlock: no further progress four necessary & sufficient conditions deadlock - no eligible actions blocked threads Aim: deadlock avoidance

More information

Chapter 6. Deadlock Concurrency: Deadlock. Magee/Kramer 2 nd Edition

Chapter 6. Deadlock Concurrency: Deadlock. Magee/Kramer 2 nd Edition Chapter 6 Deadlock 1 Deadlock Concepts: Models: system deadlock: no further progress four necessary & sufficient conditions deadlock - no eligible actions Practice: blocked threads Aim: deadlock avoidance

More information

Chapter 6. Deadlock. DM519 Concurrent Programming

Chapter 6. Deadlock. DM519 Concurrent Programming Chapter 6 Deadlock 1 But First: Repetition Monitors and Condition Synchronisation 2 Monitors & Condition Synchronisation Concepts: monitors: Models: encapsulated data + access procedures + mutual exclusion

More information

Concurrency 6 - Deadlock

Concurrency 6 - Deadlock Concurrency 6 - Deadlock Monitors & Condition Synchronization - Repetition!!"! "#$! %!#$ &' (&') #&' #&' # wait(), notify(), and notifyall() - Repetition public final void wait() throws InterruptedException;

More information

SFDV3006 Concurrent Programming

SFDV3006 Concurrent Programming SFDV3006 Concurrent Programming Lecture 6 Deadlocks, livelocks, Starvation Introduction Last week we covered semaphore and how to use them for both synchronization and condition synchronization This week

More information

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

Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock Deadlock in Resource Sharing Environment Operating System Deadlocks CIT 595 Spring 2010 A deadlock occurs when 2 or more processes/threads permanently block each other by each having a lock on a resource

More information

3C03 Concurrency: Starvation and Deadlocks

3C03 Concurrency: Starvation and Deadlocks 3C03 Concurrency: Starvation and Deadlocks Wolfgang Emmerich 1 Goals Reader/Writer problem Starvation Dining Philosophers Problem Deadlocks Liveness Analysis using LTS 2 1 Reader / Writer Problem Monitors

More information

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team http://101companies.org/wiki/ Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team Non-101samples available here: https://github.com/101companies/101repo/tree/master/technologies/java_platform/samples/javathreadssamples

More information

Deadlock and Starvation

Deadlock and Starvation Deadlock and Starvation Deadlock Permanent blocking of a set of processes Processes are waiting on events, but the events can only be triggered by other blocked processes No simple, efficient solution

More information

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy Operating Systems Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : eaymanelshenawy@yahoo.com Reference

More information

The deadlock problem

The deadlock problem Deadlocks Arvind Krishnamurthy Spring 2004 The deadlock problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process. Example locks A and B P 0 P

More information

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 Deadlock Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1 Recap: Synchronization Race condition A situation when two or more threads read and write shared

More information

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems : Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks on either

More information

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages The Dining Philosophers Problem CMSC 0: Organization of Programming Languages Threads Classic Concurrency Problems Philosophers either eat or think They must have two forks to eat Can only use forks on

More information

5. Liveness and Guarded Methods

5. Liveness and Guarded Methods 5. Liveness and Guarded Methods Prof. O. Nierstrasz Selected material Magee and Kramer Roadmap > Liveness Progress Properties > Deadlock The Dining Philosophers problem Detecting and avoiding deadlock

More information

Concurrency: Deadlock and Starvation. Chapter 6

Concurrency: Deadlock and Starvation. Chapter 6 Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs for resources

More information

THE QUEEN S UNIVERSITY OF BELFAST

THE QUEEN S UNIVERSITY OF BELFAST THE QUEEN S UNIVERSITY OF BELFAST FSP Quick Reference Guide is attached to the end of the examination paper. 110CSC321 Level 3 EXAMINATION FOR THE DEGREE(S) OF MEng, BEng, BSc Concurrent Programming January

More information

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks

More information

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other No efficient solution Involve conflicting

More information

9/30/2014. CS341: Operating System High Level Construct: Monitor Deadlock Conditions Prevention, Avoidance Detection and Recovery

9/30/2014. CS341: Operating System High Level Construct: Monitor Deadlock Conditions Prevention, Avoidance Detection and Recovery CS341: Operating System High Level Construct: Monitor Conditions Prevention, Avoidance Detection and Recovery Lect24: 1 st Oct 2014 Dr. A. Sahu Dept of Comp. Sc. & Engg. Indian Institute of Technology

More information

Last Class: Synchronization Problems!

Last Class: Synchronization Problems! Last Class: Synchronization Problems! Reader Writer Multiple readers, single writer In practice, use read-write locks Dining Philosophers Need to hold multiple resources to perform task Lecture 11, page

More information

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

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem! 2/1/13 Today: Deadlock CSCC 69H3 Operating Systems Winter 2013 Professor Bianca Schroeder U of T Remember example from last week? My_work(id_t id) { /* id can be 0 or 1 */ flag[id] = true; /* indicate

More information

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

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings Chapter 6 Concurrency: Deadlock and Starvation Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Deadlock

More information

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles Chapter 6 Concurrency: Deadlock and Starvation Seventh Edition By William Stallings Edited by Rana Forsati CSE410 Outline Principles of deadlock Deadlock

More information

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition Chapter 7: Deadlocks Silberschatz, Galvin and Gagne 2013 Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection

More information

More on Synchronization and Deadlock

More on Synchronization and Deadlock Examples of OS Kernel Synchronization More on Synchronization and Deadlock Two processes making system calls to read/write on the same file, leading to possible race condition on the file system data structures

More information

Lecture 9: Introduction to Monitors

Lecture 9: Introduction to Monitors COMP 150-CCP Concurrent Programming Lecture 9: Introduction to Monitors Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 14, 2008 Abstracting Locking Details Recall our discussion

More information

OPERATING SYSTEMS. Deadlocks

OPERATING SYSTEMS. Deadlocks OPERATING SYSTEMS CS3502 Spring 2018 Deadlocks Chapter 7 Resource Allocation and Deallocation When a process needs resources, it will normally follow the sequence: 1. Request a number of instances of one

More information

ECE519 Advanced Operating Systems

ECE519 Advanced Operating Systems IT 540 Operating Systems ECE519 Advanced Operating Systems Prof. Dr. Hasan Hüseyin BALIK (6 th Week) (Advanced) Operating Systems 6. Concurrency: Deadlock and Starvation 6. Outline Principles of Deadlock

More information

Lecture 10: Introduction to Semaphores

Lecture 10: Introduction to Semaphores COMP 150-CCP Concurrent Programming Lecture 10: Introduction to Semaphores Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 19, 2008 Semaphores Semaphores (Dijkstra 1968) are widely

More information

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process. Deadlock A set of processes is deadlocked if each process in the set is waiting for an event that can be caused by another process in the set. The events that we are mainly concerned with are resource

More information

Concurrency: Deadlock and Starvation. Chapter 6

Concurrency: Deadlock and Starvation. Chapter 6 Concurrency: Deadlock and Starvation Chapter 6 1 What is Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs

More information

i219 Software Design Methodology 12. Case study 1 Dining philosopher problem Kazuhiro Ogata (JAIST) Outline of lecture

i219 Software Design Methodology 12. Case study 1 Dining philosopher problem Kazuhiro Ogata (JAIST) Outline of lecture i219 Software Design Methodology 12. Case study 1 Dining philosopher problem Kazuhiro Ogata (JIST) Outline of lecture 2 Dining philosopher problem (DPP) Dining Room in UML & Java Chopstick in UML & Java

More information

Process Synchronization

Process Synchronization CSC 4103 - Operating Systems Spring 2007 Lecture - VI Process Synchronization Tevfik Koşar Louisiana State University February 6 th, 2007 1 Roadmap Process Synchronization The Critical-Section Problem

More information

The Deadlock Problem (1)

The Deadlock Problem (1) Deadlocks The Deadlock Problem (1) A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example System has 2 disk drives. P 1 and P 2

More information

Concurrency: State Models & Design Patterns

Concurrency: State Models & Design Patterns Concurrency: State Models & Design Patterns Practical Session Week 05 1 / 13 Exercises 04 Discussion Exercise 04 - Task 1 a) Why are immutable classes inherently safe? Because the state of an instance

More information

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Deadlocks. Copyright : University of Illinois CS 241 Staff 1 Deadlocks 1 Deadlock Which way should I go? 2 Deadlock I Oh can no! almost I m get stuck! across GRIDLOCK! 3 Deadlock Definition Deadlocked process Waiting for an event that will never occur Typically,

More information

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

CSCE Operating Systems Deadlock. Qiang Zeng, Ph.D. Fall 2018 CSCE 311 - Operating Systems Deadlock Qiang Zeng, Ph.D. Fall 2018 Previous Class What is Deadlock? How to detect it? Dealing with deadlock: Prevention Avoidance Detection CSCE 311 Operating Systems 2 Outline

More information

Last Class: Monitors. Real-world Examples

Last Class: Monitors. Real-world Examples Last Class: Monitors Monitor wraps operations with a mutex Condition variables release mutex temporarily C++ does not provide a monitor construct, but monitors can be implemented by following the monitor

More information

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem. CSE 421/521 - Operating Systems Fall 2011 Lecture - X Process Synchronization & Deadlocks Roadmap Classic Problems of Synchronization Readers and Writers Problem Dining-Philosophers Problem Sleeping Barber

More information

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

Concurrency: Principles of Deadlock. Processes and resources. Concurrency and deadlocks. Operating Systems Fall Processes need resources to run Concurrency: Principles of Deadlock Operating Systems Fall 2002 Processes and resources Processes need resources to run CPU, memory, disk, etc process waiting for a resource cannot complete its execution

More information

Deadlock Prevention. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Deadlock Prevention. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Deadlock Prevention CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements HW #3 is due Friday Feb. 25 extra office hours Thursday 1 pm - post this PA #2 assigned, due Thursday March 17 Midterm

More information

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

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Preemptable and Nonpreemptable Resources Sequence of events required to use a resource: 1. Request the resource. 2. Use the

More information

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples Last Class: Synchronization Problems Reader Writer Multiple readers, single writer In practice, use read-write locks Dining Philosophers Need to hold multiple resources to perform task Lecture 10, page

More information

Verification of Java programs using networks of finite automata with discrete data.

Verification of Java programs using networks of finite automata with discrete data. Catholic University in Ružomberok Scientific Issues, Mathematica II, Ružomberok 2009 Verification of Java programs using networks of finite automata with discrete data. Bożena Woźna, Andrzej Zbrzezny Institute

More information

System Model. Types of resources Reusable Resources Consumable Resources

System Model. Types of resources Reusable Resources Consumable Resources Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock System Model Types

More information

6. Concurrency: Deadlock

6. Concurrency: Deadlock CSC400 - Operating Systems 6. Concurrency: Deadlock J. Sumey Deadlock one problem that results from multiprogramming def: a process (or thread) is said to be deadlocked if it is waiting for an event that

More information

Liveness properties. Deadlock

Liveness properties. Deadlock Liveness properties From a theoretical viewpoint we must ensure that we eventually make progress i.e. we want to avoid : blocked threads/processes waiting for each other Livelock: processes/threads execute

More information

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

Deadlock. A Bit More on Synchronization. The Deadlock Problem. Deadlock Characterization. Operating Systems 2/7/2005. CSC 256/456 - Spring A Bit More on Synchronization Deadlock CS 256/456 Dept. of Computer Science, University of Rochester Synchronizing interrupt handlers: Interrupt handlers run at highest priority and they must not block.

More information

Back to synchronization

Back to synchronization Back to synchronization The dining philosophers problem Deadlocks o Modeling deadlocks o Dealing with deadlocks Operating Systems, 28, I. Dinur, D. Hendler and R. Iakobashvili The Dining Philosophers Problem

More information

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

Deadlock. Concepts to discuss. A System Model. Deadlock Characterization. Deadlock: Dining-Philosophers Example. Deadlock: Bridge Crossing Example Concepts to discuss Deadlock CSCI 315 Operating Systems Design Department of Computer Science Deadlock Livelock Spinlock vs. Blocking Notice: The slides for this lecture have been largely based on those

More information

Deadlocks. Dr. Yingwu Zhu

Deadlocks. Dr. Yingwu Zhu Deadlocks Dr. Yingwu Zhu Deadlocks Synchronization is a live gun we can easily shoot ourselves in the foot Incorrect use of synchronization can block all processes You have likely been intuitively avoiding

More information

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 Deadlock Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1 Recap: Synchronization Race condition A situation when two or more threads read and write shared

More information

Yet another synchronization problem

Yet another synchronization problem Yet another synchronization problem The dining philosophers problem Deadlocks o Modeling deadlocks o Dealing with deadlocks Operating Systems, 25, Meni Adler, Danny Hendler & Roie Zivan The Dining Philosophers

More information

CMPT 300 Introduction to Operating Systems

CMPT 300 Introduction to Operating Systems CMPT 300 Introduction to Operating Systems Introduction to Deadlocks 1 Preemptible resources Resources that can be taken away from a process without adversely affecting outcome Example: memory (swapping)

More information

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 Deadlock Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1 Recap: Synchronization Race condition A situation when two or more threads read and write shared

More information

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

More information

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 Deadlock Problem. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Deadlock 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 Example semaphores A and B, initialized to 1 P 0 P

More information

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

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition, Chapter 7: Deadlocks, Silberschatz, Galvin and Gagne 2009 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks To present a number

More information

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

Outline. Deadlock. Examples, Conditions, Strategies. Deadlock Prevention. Deadlock Avoidance. Banker s algorithm 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?

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst Clicker Question #1 public static void main(string[] args) { (new Thread(new t1())).start(); (new Thread(new t2())).start();}

More information

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

Operating Systems. Lecture3.2 - Deadlock Management. Golestan University. Hossein Momeni Operating Systems Lecture3.2 - Deadlock Management Golestan University Hossein Momeni momeni@iust.ac.ir Contents Resources Introduction to deadlocks Why do deadlocks occur? Ignoring deadlocks : ostrich

More information

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

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009! Chapter 7: Deadlocks Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009! Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling

More information

Deadlock and Starvation

Deadlock and Starvation Deadlock and Starvation Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs for resources by two or more

More information

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3 Operating Systems Antonio Vivace - 2016 revision 4 Licensed under GPLv3 Process Synchronization Background A cooperating process can share directly a logical address space (code, data) or share data through

More information

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

Chapter 7: Deadlocks. Operating System Concepts with Java 8 th Edition Chapter 7: Deadlocks 7.1 Silberschatz, Galvin and Gagne 2009 Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock

More information

Deadlock Risk Management

Deadlock Risk Management Lecture 6: Deadlocks, Deadlock Risk Management Readers and Writers with Readers Priority Shared data semaphore wrt, readcountmutex; int readcount Initialization wrt = 1; readcountmutex = 1; readcount =

More information

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018 Deadlock and Monitors CS439: Principles of Computer Systems February 7, 2018 Last Time Terminology Safety and liveness Atomic Instructions, Synchronization, Mutual Exclusion, Critical Sections Synchronization

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2016 Lecture 24 Deadlocks Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Resource allocation graph:

More information

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition Chapter 7: Deadlocks Silberschatz, Galvin and Gagne 2013 Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection

More information

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University CSC 4103 - Operating Systems Fall 2009 Lecture - XI Deadlocks - II Tevfik Ko!ar Louisiana State University September 29 th, 2009 1 Roadmap Classic Problems of Synchronization Bounded Buffer Readers-Writers

More information

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko! CSC 4103 - Operating Systems Fall 2009 Lecture - XI Deadlocks - II Roadmap Classic Problems of Synchronization Bounded Buffer Readers-Writers Dining Philosophers Sleeping Barber Deadlock Prevention Tevfik

More information

Process Management And Synchronization

Process Management And Synchronization Process Management And Synchronization In a single processor multiprogramming system the processor switches between the various jobs until to finish the execution of all jobs. These jobs will share the

More information

Chapter 7: Deadlocks

Chapter 7: Deadlocks Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Chapter

More information

Lesson 6: Process Synchronization

Lesson 6: Process Synchronization Lesson 6: Process Synchronization Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

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

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne OPERATING SYSTEMS Prescribed Text Book Operating System Principles, Seventh Edition By Abraham Silberschatz, Peter Baer Galvin and Greg Gagne 1 DEADLOCKS In a multi programming environment, several processes

More information

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon CSE 306/506 Operating Systems Deadlock YoungMin Kwon Deadlock A set of processes are deadlocked if Each process in the set is blocked and Waiting for an event that can be triggered only from another process

More information

JOINT PROGRESS WITH DEADLOCK RESOURCE CATEGORIES AND DEADLOCK

JOINT PROGRESS WITH DEADLOCK RESOURCE CATEGORIES AND DEADLOCK DEADLOCK JOINT PROGRESS WITH DEADLOCK The permanent blocking of a set of processes that either compete for resources or communicate with each other A set of processes is deadlocked when each process in

More information

DEADLOCK CS 409, FALL 2013 DEADLOCK AND STARVATION/1

DEADLOCK CS 409, FALL 2013 DEADLOCK AND STARVATION/1 DEADLOCK The permanent blocking of a set of processes that either compete for resources or communicate with each other A set of processes is deadlocked when each process in the set is blocked awaiting

More information

Operating Systems. Operating Systems Sina Meraji U of T

Operating Systems. Operating Systems Sina Meraji U of T Operating Systems Operating Systems Sina Meraji U of T Remember example from third week? My_work(id_t id) { /* id can be 0 or 1 */... flag[id] = true; /* indicate entering CS */ while (flag[1-id]) ;/*

More information

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems Synchronization CS 475, Spring 2018 Concurrent & Distributed Systems Review: Threads: Memory View code heap data files code heap data files stack stack stack stack m1 m1 a1 b1 m2 m2 a2 b2 m3 m3 a3 m4 m4

More information

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

Deadlocks. Thomas Plagemann. With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen Deadlocks Thomas Plagemann With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen Preempting Scheduler Activations Scheduler activations are completely preemptable User" space" Kernel " space"

More information

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles Chapter 6 Concurrency: Deadlock and Starvation Seventh Edition By William Stallings Operating Systems: Internals and Design Principles When two trains

More information

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures CSE 421/521 - Operating Systems Fall 2012 Lecture - X Deadlocks - I Tevfik Koşar University at Buffalo October 2nd, 2012 1 Roadmap Synchronization structures Problems with Semaphores Monitors Condition

More information

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012 CSE 421/521 - Operating Systems Fall 2012 Lecture - X Deadlocks - I Tevfik Koşar Synchronization structures Problems with Semaphores Monitors Condition Variables Roadmap The Deadlock Problem Characterization

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L14 Deadlocks Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Mutex vs Semaphore 2 2 FAQ Does

More information

CS450/550 Operating Systems

CS450/550 Operating Systems CS450/550 Operating Systems Lecture 3 Deadlocks Dr. Xiaobo Zhou Department of Computer Science CS450/550 Deadlocks.1 Review: Summary of Chapter 2 Sequential process model Multi-threading: user-space vs.

More information

The Deadlock Problem

The Deadlock Problem Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock The Deadlock

More information

CSE 4/521 Introduction to Operating Systems

CSE 4/521 Introduction to Operating Systems CSE 4/521 Introduction to Operating Systems Lecture 7 Process Synchronization II (Classic Problems of Synchronization, Synchronization Examples) Summer 2018 Overview Objective: 1. To examine several classical

More information

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition Chapter 7: Deadlocks Silberschatz, Galvin and Gagne 2013 Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection

More information

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

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA Chapter 8: Deadlock Questions? CSCI 4730 Operating Systems! What is a deadlock?! What causes a deadlock?! How do you deal with (potential) deadlocks? Deadlock Deadlock: What is a deadlock? Example: Two

More information

Operating Systems. Deadlocks

Operating Systems. Deadlocks Operating Systems Deadlocks Deadlocks Example: two processes want to scan a document, and then save it on a CD P0 down(scanner); down(cd_writer); scan_and_record(); up(cd_writer); up(scanner); P1 down(cd_writer);

More information

Chapter 8: Deadlocks. Bridge Crossing Example. The Deadlock Problem

Chapter 8: Deadlocks. Bridge Crossing Example. The Deadlock Problem Chapter 8: Deadlocks Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock 8.1 Bridge Crossing Example Bridge has one

More information

Lecture 7 Deadlocks (chapter 7)

Lecture 7 Deadlocks (chapter 7) Bilkent University Department of Computer Engineering CS342 Operating Systems Lecture 7 Deadlocks (chapter 7) Dr. İbrahim Körpeoğlu http://www.cs.bilkent.edu.tr/~korpe 1 References The slides here are

More information

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition Chapter 7: Deadlocks Silberschatz, Galvin and Gagne 2013 Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection

More information

The Deadlock Lecture

The Deadlock Lecture Concurrent systems Lecture 4: Deadlock, Livelock, and Priority Inversion DrRobert N. M. Watson The Deadlock Lecture 1 Reminder from last time Multi-Reader Single-Writer (MRSW) locks Alternatives to semaphores/locks:

More information

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems More Synchronization; Concurrency in Java CS 475, Spring 2018 Concurrent & Distributed Systems Review: Semaphores Synchronization tool that provides more sophisticated ways (than Mutex locks) for process

More information