Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.

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.
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
6. Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
CS 450 OPERATING SYSTEMS DEADLOCKS Manju Muralidharan Priya.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
Overview Resources Why do deadlocks occur? Dealing with deadlocks Ignoring them: ostrich algorithm Detecting & recovering from deadlock Avoiding deadlock.
1 Deadlocks 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
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.
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
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.
Operating Systems Lecture 4 Deadlock Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
Deadlocks System Model RAG Deadlock Characterization
CS333 Intro to Operating Systems Jonathan Walpole.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlocks Introduction to Operating Systems: Module 7.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
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.
Deadlocks References –text: Tanenbaum ch.3. Deadly Embrace Deadlock definition –A set of process is dead locked if each process in the set is waiting.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
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.
Synchronization Deadlocks and prevention
MODERN OPERATING SYSTEMS Third Edition ANDREW S
ITEC 202 Operating Systems
Chapter 7 – Deadlock and Indefinite Postponement
Operating System: DEADLOCKS
Introduction to Operating Systems
CSCI 511 Operating Systems Chapter 7 Deadlock
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

 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 Starvation can also caused by livelocks.

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 chopstick[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]); }

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

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

Necessary Conditions for deadlock  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

Dealing with deadlock  Deadlock prevention Make sure that at least one of the conditions for deadlock does not hold. Prevent deadlock from happening  Deadlock avoidance Make sure the system is always in a safe state.  Deadlock detection and recovery Let it be. Deal with it only when bad things (deadlock) happens.  The chance for deadlock to happen is not big.

Deadlock Prevention Techniques  Involves removing one of the four conditions

Deadlock Prevention Techniques 1. Limited resource  Virtualize the physical limited resource to make infinite instances  Example: printer pool

Deadlock Prevention Techniques 2. No preemption  Make the source preempt-able  CPU sharing, copy memory to disk  Not always possible for all resources

Deadlock Prevention Techniques  Wait while holding Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) easier said than done…

Deadlock Prevention Techniques  Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) semaphore chopstick[5] = {1, 1, 1, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); }

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 1, 1, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); }

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 1  0, 1  0, 1, 1}, s = 1  0  1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 1

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 0, 0, 1  0, 1  0}, s = 1  0  1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 13

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 0, 0, 0, 0}, s = 1  0; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 13 2

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 0, 0, 0, 0}, s = 0; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 13 2

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) int counter[5] = {1, 1, 1, 1, 1}; semaphore chopstick[5] = {1, 1, 1, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); }

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // counter[5] = {1, 0, 0, 1, 1}; chopstick[5] = {1, 0, 0, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); } (1, 2) 1

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // counter[5] = {1, 0, 0, 1, 1}; chopstick[5] = {1, 0, 0, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); } (1, 2) 1 () 2

Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // counter[5] = {1, 0, 0, 0, 0}; chopstick[5] = {1, 0, 0, 0, 0}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); } (1, 2) 1 (3, 4) 3

Deadlock Prevention Techniques 4. Circular chain of requests Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)) // chopstick[5] = {1, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {1  0, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 1  0, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 1

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 1  0, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 12

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 1  0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 123

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1  0}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 0}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 0  1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0  1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 1  0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0  1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0  1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 1  0, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0  1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0  1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 1  0, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0  1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0  1, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {1  0, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 1, 1, 1, 1  0}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

Deadlock avoidance: Banker’s Algorithm  The idea of Banker’s algorithm: Allows the sum of requested resources > total resources  Before granting any resources, make sure that 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 (in safe state after granting) A request will not be granted if the state is unsafe after granting.

Example 1 (safe state)  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46

Example 1  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 20 MB0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36

Example 1  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P

Example 1  Total RAM 256 MB AllocatedStill needed P1P1 110 MB0 MB P2P2 P3P3 120 MB80 MB time free RAM (MB) 46 P2P P1P1 26

Example 1  Total RAM 256 MB AllocatedStill needed P1P1 0 MB P2P2 P3P3 120 MB80 MB time free RAM (MB) 46 P2P P1P

Example 1  Total RAM 256 MB AllocatedStill needed P1P1 0 MB P2P2 P3P3 200 MB0 MB time free RAM (MB) 46 P2P P1P P3P3 56

Example 1  Total RAM 256 MB AllocatedStill needed P1P1 0 MB P2P2 P3P3 time free RAM (MB) 46 P2P P1P P3P

Example 1  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P P1P P3P The system is initially in a safe state, since we can run P 2, P 1, and then P 3

Example 2 (unsafe state)  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46

Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 20 MB0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36

Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P

Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P

Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P The system is initially in an unsafe state, since we cannot find an execution sequence for P 1, P 2, and P 3

Banker’s algorithm  Every time a request is granted, the banker makes sure that the system is in a safe state.

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

Resource allocation graph  Nodes: A node for each thread A node for each resource  Edges: Between a thread and a resource Resource -> thread if the thread owns the resource Thread -> resource if the thread waits for the resource

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

Detecting deadlock with resource allocation graph  A deadlock involves the resources and threads that form a circle in the resource allocation graph.

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

Questions  Which technique allows most concurrrency, deadlock prevention, deadlock avoidance, or deadlock detection and recovery?  Which technique is most effective in dealing with deadlocks?  Which technique is in windows?