Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Deadlocks pp System Model 1.Request 2.Use 3.Release Necessary Conditions 1.Mutual exclusion (non-sharable) 2.Hold and wait (holding at least one.
Deadlock and Starvation
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 7 – Deadlock and Indefinite Postponement Outline 7.1 Introduction 7.2Examples of Deadlock.
1 CSC 539: Operating Systems Structure and Design Spring 2005 Process deadlock  deadlock prevention  deadlock avoidance  deadlock detection  recovery.
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Concurrency: Deadlock & Starvation
CS 450 OPERATING SYSTEMS DEADLOCKS Manju Muralidharan Priya.
CPS110: Deadlock Landon Cox February 5, Concurrency so far  Mostly tried to constrain orderings  Locks, monitors, semaphores  It is possible.
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
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 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter 3. 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems (OS)
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
This Time - Deadlock Definition Conditions for deadlocks
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CS 346 – Chapter 7 Deadlock –Properties –Analysis: directed graph Handle –Prevent –Avoid Safe states and the Banker’s algorithm –Detect –Recover.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
Deadlocks Introduction to Operating Systems: Module 7.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Informationsteknologi Monday, October 1, 2007Computer Systems/Operating Systems - Class 111 Today’s class 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.
Process Management Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 7 – Deadlock and Indefinite Postponement
Introduction to Operating Systems
CSCI 511 Operating Systems Chapter 7 Deadlock
Chapter 7: Deadlocks.
CSc 552 Advanced Unix Process deadlock deadlock prevention
CS 333 Introduction to Operating Systems Class 7 - Deadlock
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Andy Wang Operating Systems COP 4610 / CGS 5765
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
SE350: Operating Systems Lecture 5: Deadlock.
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CENG334 Introduction to Operating Systems
Chapter 7: Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Presentation transcript:

Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765

Deadlocks  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot be taken away from its current thread without failing the computation (e.g., storage allocated to a file)

Deadlocks  Deadlocks that involve preemptable resources (e.g., CPU) can usually be resolved by reallocation of resources  Starvation: a thread waits indefinitely  A deadlock implies starvation

An Example of Deadlocks Thread AThread B P(x);P(y); P(y);P(x);  A deadlock won’t always happen with this code, but it might

Deadlocks, Deadlocks, Everywhere…  Can happen with any kind of resource Among multiple resources  Cannot be resolved for each resource independently A thread can grab all the memory The other grabs all the disk space Each thread may need to wait for the other to release

Deadlocks, Deadlocks, Everywhere…  Round-Robin CPU scheduling cannot prevent deadlocks (or starvation) from happening  Can occur whenever there is waiting…

A Classic Example of Deadlocks  Dinning lawyers (philosophers)  Each needs two chopsticks to eat

Dinning Lawyers  If each first grabs the chopstick on their right before the one on their left, and all grab at the same time, we have a deadlock (Personally, I prefer to starve than share chopsticks…)

A Dinning Lawyer Implementation semaphore chopsticks[5] = {1, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[j]); P(chopstick[(j + 1) % 5]; // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); }

Conditions for Deadlocks  Four necessary (but not sufficient) conditions Limited access (lock-protected resources) No preemption (if someone has the resource, it cannot be taken away) Wait while holding (holding a resource while requesting and waiting for the next resource) Circular chain of requests

Deadlock Prevention  Involves removing one of the four conditions 1.Infinite resources (buy a very large disk) 2.No sharing (independent threads) 3.No waiting (phone company) 4.Preempt resources (copying memory content to disk)

More Deadlock Prevention Methods 5.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) 6.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) 7.A combination of techniques

Banker’s Algorithm  The idea of Banker’s algorithm: Allows the sum of requested resources > total resources As long as, there is some way for all threads to finish without getting into any deadlocks

Banker’s Algorithm  Banker’s algorithm: A thread states its maximum resource needs in advance The OS allocates resource dynamically as needed. A thread waits if granting its request would lead to deadlocks A request can be granted if some sequential ordering of threads is deadlock free

An Example of Banker’s Algorithm  Physical memory 256 MB  Unallocated memory: 56 MB  The system is in a safe state, since we can run P 1, and then P 2 AllocatedMax required Still needed P1P1 80 MB110 MB30 MB P2P2 120 MB200 MB80 MB

Another Example of Banker’s Algorithm  Physical memory 256 MB  Unallocated memory: 56 MB  Unsafe state, since 56 MB is not enough for either processes AllocatedMax required Still needed P1P1 80 MB140 MB60 MB P2P2 120 MB200 MB80 MB

Deadlock Detection and Recovery  Scan the resource allocation graph  Detect circular chains of requests  Recover from the deadlock

Resource Allocation Graph Resource X Resource Y waiting for Thread A owned by Thread B owned by

Once A Cycle is Detected…  Some possible actions Kill a thread and force it to give up resources  Remaining system may be in an inconsistent state Rollback actions of a deadlocked thread  Not always possible (a file maybe half- way through modifications)  Need checkpointing, or taking snapshots of system states from time to time