Introduction to Operating Systems

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
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 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.
Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.
Deadlocks CS 3100 Deadlocks1. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another.
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.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
CS333 Intro to Operating Systems Jonathan Walpole.
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
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.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
Chapter 7: Deadlocks. 7.2CSCI 380 – Operating Systems Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Multi-Object Synchronization. Multi-Object Programs What happens when we try to synchronize across multiple objects in a large program? – Each object.
Deadlocks Introduction to Operating Systems: Module 7.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Deadlocks CPE Operating Systems
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
CSE Operating System Principles Deadlocks. CSE – Operating System Principles2 Overview System Model Deadlock Characterization Methods for.
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);
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
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.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Process Management Deadlocks.
Synchronization Deadlocks and prevention
Chapter 7: Deadlocks.
Deadlock Management.
CS703 - Advanced Operating Systems
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Operating Systems (CS 340 D)
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
ICS 143 Principles of Operating Systems
Chapter 7 Deadlocks.
CSCI 511 Operating Systems Chapter 7 Deadlock
Process Deadlocks.
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Outline Deadlocks, dead lock prevention, avoidance.
CS 333 Introduction to Operating Systems Class 7 - Deadlock
Andy Wang Operating Systems COP 4610 / CGS 5765
Chapter 7: Deadlocks.
SE350: Operating Systems Lecture 5: Deadlock.
OPERATING SYSTEMS DEADLOCKS.
CENG334 Introduction to Operating Systems
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Presentation transcript:

Introduction to Operating Systems CPSC/ECE 3220 Summer 2018 Lecture Notes OSPP Chapter 6 – Part B (adapted by Mark Smotherman from Tom Anderson’s slides on OSPP web site)

Deadlock Definition Resource: a physical or virtual entity that can be assigned to a user or application Anything needed by a thread to do its job (CPU, disk space, memory, lock) Preemptable: can be taken away by OS Non-preemptable: must leave with thread Starvation: thread waits indefinitely Deadlock: circular waiting for resources Deadlock => starvation, but not vice versa

Example: two locks Thread A lock1.acquire(); lock2.acquire(); // critical section lock2.release(); lock1.release(); Thread B lock2.acquire(); lock1.acquire(); // critical section lock1.release(); lock2.release();

Bidirectional Bounded Buffer Thread A buffer1.put(data); buffer2.get(); Thread B buffer2.put(data); buffer1.get(); Suppose buffer1 and buffer2 both start almost full.

Two locks and a condition variable Thread A lock1.acquire(); … lock2.acquire(); while (need to wait) { condition.wait(lock2); } lock2.release(); lock1.release(); Thread B lock1.acquire(); … lock2.acquire(); condition.signal(lock2); lock2.release(); lock1.release(); What happens here?

Yet another Example

Dining Philosophers Each philosopher needs two chopsticks to eat. Each grabs chopstick on the right first.

Necessary Conditions for Deadlock Limited access to resources If infinite resources, no deadlock! No preemption If resources are virtual, can break deadlock Multiple independent requests “wait while holding” Circular chain of requests

Question How does Dining Philosophers meet the necessary conditions for deadlock? Limited access to resources No preemption Multiple independent requests (wait while holding) Circular chain of requests How can we modify Dining Philosophers to prevent deadlock?

Approaches to Handling Deadlock Prevent by limiting program behavior Limit program from doing anything that might lead to deadlock Avoid by predicting the future If we know what program will do, we can tell if granting a resource might lead to deadlock Detect and recover If we can rollback a thread, we can fix a deadlock once it occurs

Exploit or Limit Behavior Provide enough resources How many chopsticks are enough? Eliminate wait while holding Release lock when calling out of module Telephone circuit setup Eliminate circular waiting Lock ordering: always acquire locks in a fixed order Example: move file from one directory to another

Example Thread 1 Acquire A Acquire C If (maybe) Wait for B Thread 2 Acquire B Wait for A How can we make sure to avoid deadlock?

Deadlock Dynamics Safe state: Unsafe state: Doomed state: For any possible sequence of future resource requests, it is possible to eventually grant all requests May require waiting even when resources are available! Unsafe state: Some sequence of resource requests can result in deadlock Doomed state: All possible computations lead to deadlock

Possible System States

Predict the Future Banker’s algorithm (Dijkstra) State maximum resource needs in advance Allocate resources dynamically when resource is needed -- wait if granting request would lead to deadlock Request can be granted if some sequential ordering of threads is deadlock free Extends to multiple resource types

Banker’s Algorithm Grant request iff result is a safe state Sum of maximum resource needs of current threads can be greater than the total resources Provided there is some way for all the threads to finish without getting into deadlock Example: proceed iff total available resources - # allocated >= max remaining that might be needed by this thread in order to finish Guarantees this thread can finish

Banker’s Algorithm Example (1a) example using total units = 10 thread max need allocated remaining need able to finish? A 8 2 6 ? B 6 3 3 ? C 4 2 2 ? available units = 3

Banker’s Algorithm Example (1b) example using total units = 10 thread max need allocated remaining need able to finish? A 8 2 6 ? B 6 3 3 ? C 4 2 2 ? available units = 3 Show that this is a safe state since there is a sequence of thread executions that allows each thread to obtain its maximum resource need, complete its work, and release its resources: thread allocation in steps A 2 B 3 C 2 allocated 7 available 3

Banker’s Algorithm Example (1c) example using total units = 10 thread max need allocated remaining need able to finish? A 8 2 6 ? B 6 3 3 ? C 4 2 2 true available units = 3 Show that this is a safe state since there is a sequence of thread executions that allows each thread to obtain its maximum resource need, complete its work, and release its resources: thread allocation in steps A 2 2 B 3 3 C 2 grant 2=>4 allocated 7 9 available 3 1

Banker’s Algorithm Example (1d) example using total units = 10 thread max need allocated remaining need able to finish? A 8 2 6 ? B 6 3 3 ? C 4 2 2 ? available units = 3 Show that this is a safe state since there is a sequence of thread executions that allows each thread to obtain its maximum resource need, complete its work, and release its resources: thread allocation in steps A 2 2 2 B 3 3 3 C 2 grant 2=>4 release=>0 allocated 7 9 5 available 3 1 5

Banker’s Algorithm Example (1e) example using total units = 10 thread max need allocated remaining need able to finish? A 8 2 6 ? B 6 3 3 true C 4 2 2 true available units = 3 Show that this is a safe state since there is a sequence of thread executions that allows each thread to obtain its maximum resource need, complete its work, and release its resources: thread allocation in steps A 2 2 2 2 2 B 3 3 3 grant3=>6 release=>0 C 2 grant 2=>4 release=>0 0 0 allocated 7 9 5 8 2 available 3 1 5 2 8

Banker’s Algorithm Example (1f) example using total units = 10 thread max need allocated remaining need able to finish? A 8 2 6 true B 6 3 3 true C 4 2 2 true available units = 3 Show that this is a safe state since there is a sequence of thread executions that allows each thread to obtain its maximum resource need, complete its work, and release its resources: thread allocation in steps A 2 2 2 2 2 grant 6=>8 release=>0 B 3 3 3 grant3=>6 release=>0 0 0 C 2 grant 2=>4 release=>0 0 0 0 0 allocated 7 9 5 8 2 8 0 available 3 1 5 2 8 2 10

Banker’s Algorithm Example (1g) example using total units = 10 thread max need allocated remaining need able to finish? A 8 2 6 ? B 6 3 3 ? C 4 2 2 ? available units = 3 This is a safe state since there is a sequence of thread executions that allows each thread to obtain its maximum resource need, complete its work, and release its resources: thread allocation in steps A 2 B 3 C 2 allocated 7 available 3 Can you show a second sequence that leads to the recovery of all 10 resources?

Banker’s Algorithm Example (2) example using total units = 10 thread max need allocated remaining need finish? A 8 2 6 true B 6 3 3 true C 4 2 2 true available units = 3 thread A requests 2 units (of the three unallocated) A 8 4 4 false B 6 3 3 false C 4 2 2 false available units = 1 Cannot grant this request since there would not be enough unallocated units to satisfy the remaining need for any thread! Initial state Unsafe state if request is granted

Banker’s Algorithm Example (3) Example using total units = 10 thread max need allocated remaining need finish? A 8 2 6 true B 6 3 3 true C 4 2 2 true available units = 3 thread C requests 1 units (of the three unallocated) C 4 3 1 true available units = 2 Can grant this request! Initial state Safe state if request is granted

Banker’s Algorithm Example (4) Algorithm extends to vectors of resources. max need allocated remaining available R1 R2 R3 R1 R2 R3 R1 R2 R3 R1 R2 R3 A 4 1 2 3 0 0 1 1 2 5 1 1 B 7 4 4 2 4 3 5 0 1 C 3 3 3 0 2 1 3 1 2 This is a safe state since the unused units (5,1,1) can satisfy B's remaining claim of (5,0,1); when B ends it will release its resources and thus increase the unused units to (7,5,4). This can satisfy both A and C's remaining need.

Detect and Repair Algorithm Proceed without the resource Scan wait for graph Detect cycles Fix cycles Proceed without the resource Requires robust exception handling code Roll back and retry Transaction: all operations are provisional until have all required resources to complete operation Anderson notes: a) Shoot thread, force it to give up resources. In traffic example, Godzilla -- picks up a car, hurls it into the east river. Other three cars can go! This isn't always possible -- for instance, with a mutex, can't shoot a thread and leave world in a consistent state, unless the exception handling code is very carefully written. b) Continue even though you don’t have the resource you need --- e.g., Amazon will say you can have a book, if the inventory subsystem doesn’t reply quickly enough (wrong answer quickly is better than the right answer slowly)   c) Roll back actions of deadlocked threads (transactions) Common technique in databases: Transactions allow you to do this rollback, to beginning of a transaction. Of course, if restart in exactly the same way, might still get deadlock.

Detecting Deadlock