Deadlock Conditions for Deadlock Deadlock Prevention Deadlock Detection Deadlock Recovery Dining Philosophers Semaphores.

Slides:



Advertisements
Similar presentations
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Advertisements

Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Deadlock Prevention, Avoidance, and Detection.  The Deadlock problem The Deadlock problem  Conditions for deadlocks Conditions for deadlocks  Graph-theoretic.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 7 – Deadlock and Indefinite Postponement Outline 7.1 Introduction 7.2Examples of Deadlock.
7.4 Resource Concepts Preemptible resources (e.g. processors and main memory) –Can be removed from a process without loss of work Nonpreemptible resources.
1 Wednesday, June 28, 2006 Command, n.: Statement presented by a human and accepted by a computer in such a manner as to make the human feel that he is.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
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.
CS 450 OPERATING SYSTEMS DEADLOCKS Manju Muralidharan Priya.
Deadlock Prevention CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
Chapter 7 – Deadlock and Indefinite Postponement
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation
Silberschatz, Galvin and Gagne  Operating System Concepts Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for.
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided?
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Operating Systems Part III: Process Management (Deadlocks)
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
Deadlock Operating Systems: Internals and Design Principles.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
Deadlocks System Model RAG Deadlock Characterization
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlock. Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock.
Deadlocks Introduction to Operating Systems: Module 7.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Informationsteknologi Monday, October 1, 2007Computer Systems/Operating Systems - Class 111 Today’s class Deadlock.
Process Synchronization 1. while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced;
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);
Lecture 6: Process Synchronization Dr. Nermin Hamza 1.
Deadlock Operating Systems (CC2011NI) -Mr. Pranoy Man Pradhan.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Background on the need for Synchronization
Chapter 7 – Deadlock and Indefinite Postponement
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Monitors Chapter 7.
Lecture 2 Part 2 Process Synchronization
Monitors Chapter 7.
Conditions for Deadlock
Andy Wang Operating Systems COP 4610 / CGS 5765
Monitors Chapter 7.
OPERATING SYSTEMS DEADLOCKS.
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Deadlock Conditions for Deadlock Deadlock Prevention Deadlock Detection Deadlock Recovery Dining Philosophers Semaphores

Introduction Deadlock –A process or thread is waiting for a particular event that will not occur System deadlock –One or more processes are deadlocked

Traffic Deadlock

Simple Resource Deadlock Most deadlocks develop because of the normal contention for dedicated resources Circular wait is characteristic of deadlocked systems

Resource deadlock example. This system is deadlocked because each process holds a resource being requested by the other process and neither process is willing to release the resource it holds. Simple Resource Deadlock

Example: Dining Philosophers Problem statement: Five philosophers sit around a circular table. Each leads a simple life alternating between thinking and eating spaghetti. In front of each philosopher is a dish of spaghetti that is constantly replenished by a dedicated wait staff. There are exactly five forks on the table, one between each adjacent pair of philosophers. Eating spaghetti (in the most proper manner) requires that a philosopher use both adjacent forks (simultaneously). Develop a concurrent program free of deadlock and indefinite postponement that models the activities of the philosophers.

Related Problem: Indefinite Postponement Indefinite postponement –Also called indefinite blocking or starvation –Occurs due to biases in a system’s resource scheduling policies Aging –Technique that prevents indefinite postponement by increasing process’s priority as it waits for resource

Resource Concepts Preemptible resources (e.g. processors and main memory) –Can be removed from a process without loss of work Nonpreemptible resources (e.g. tape drives and optical scanners) –Cannot be removed from the processes to which they are assigned without loss of work Reentrant code –Cannot be changed while in use –May be shared by several processes simultaneously Serially reusable code –May be changed but is reinitialized each time it is used –May be used by only one process at a time

Four Necessary Conditions for Deadlock Mutual exclusion condition –Resource may be acquired exclusively by only one process at a time Wait-for condition (hold-and-wait condition) –Process that has acquired an exclusive resource may hold that resource while the process waits to obtain other resources No-preemption condition –Once a process has obtained a resource, the system cannot remove it from the process’s control until the process has finished using the resource Circular-wait condition –Two or more processes are locked in a “circular chain” in which each process is waiting for one or more resources that the next process in the chain is holding

Deadlock Prevention Deadlock prevention –Condition a system to remove any possibility of deadlocks occurring –Deadlock cannot occur if any one of the four necessary conditions is denied –First condition (mutual exclusion) cannot be broken

Denying the “Wait-For” Condition When denying the “wait-for condition” –All of the resources a process needs to complete its task must be requested at once –This leads to inefficient resource allocation

Denying the “No-Preemption” Condition When denying the “no-preemption” condition –Processes may lose work when resources are preempted –This can lead to substantial overhead as processes must be restarted

Denying the “Circular-Wait” Condition Denying the “circular-wait” condition: –Uses a linear ordering of resources to prevent deadlock –More efficient resource utilization than the other strategies Drawbacks –Not as flexible or dynamic as desired –Requires the programmer to determine the ordering or resources for each system

Havender’s linear ordering of resources for preventing deadlock. Denying the “Circular-Wait” Condition

Deadlock Detection Deadlock detection –Used in systems in which deadlocks can occur –Determines if deadlock has occurred –Identifies those processes and resources involved in the deadlock –Deadlock detection algorithms can incur significant runtime overhead

Resource-Allocation Graphs Resource-allocation graphs –Squares Represent processes –Large circles Represent classes of identical resources –Small circles drawn inside large circles Indicate separate identical resources of each class

Resource-allocation and request graphs. Resource-Allocation Graphs

Reduction of Resource-Allocation Graphs Graph reductions –If a process’s resource requests may be granted, the graph may be reduced by that process –If a graph can be reduced by all its processes, there is no deadlock –If a graph cannot be reduced by all its processes, the irreducible processes constitute the set of deadlocked processes in the graph

Graph reductions determining that no deadlock exists. Reduction of Resource-Allocation Graphs

Deadlock Recovery Deadlock recovery –Clears deadlocks from system so that deadlocked processes may complete their execution and free their resources Suspend/resume mechanism –Allows system to put a temporary hold on a process –Suspended processes can be resumed without loss of work Checkpoint/rollback –Facilitates suspend/resume capabilities –Limits the loss of work to the time the last checkpoint was made

Deadlock Solutions Four major areas of interest in deadlock research –Deadlock prevention –Deadlock avoidance –Deadlock detection –Deadlock recovery

P0P0 P1P1 P2P2 P3P3 P4P4 R0R0 R4R4 R1R1 R3R3 R2R2

5.5.2 Test-and-Set Instruction Use a machine-language instruction to ensure that mutual exclusion primitives are performed indivisibly –Such instructions are called atomic –Machine-language instructions do not ensure mutual exclusion alone—the software must properly use them For example, programmers must incorporate favored threads to avoid indefinite postponement –Used to simplify software algorithms rather than replace them Test-and-set instruction –testAndSet(a, b) copies the value of b to a, then sets b to true –Example of an atomic read-modify-write (RMW) cycle

using System; using System.Threading; class Program { static int value; static void Main() { Thread thread1 = new Thread(new ThreadStart(A)); Thread thread2 = new Thread(new ThreadStart(A)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); Console.WriteLine(value); Console.ReadKey(); } static void A() { // Add one. Interlocked.Add(ref value, 1); } Interlocked.Add

using System; using System.Threading; class Program { static int value; static void Main() { Thread thread1 = new Thread(new ThreadStart(A)); Thread thread2 = new Thread(new ThreadStart(A)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); Console.WriteLine(value); Console.ReadKey(); } static void A() { // Add one then subtract two. Interlocked.Increment(ref value); Interlocked.Decrement(ref value); } Interlocked.Increment & Decrement

using System; using System.Threading; class Program { static long value1; static void Main() { Thread thread1 = new Thread(new ThreadStart(A)); thread1.Start(); thread1.Join(); // Written [2] Console.WriteLine(Interlocked.Read(ref value1)); Console.ReadKey(); } static void A() { // Replace value with 10. Interlocked.Exchange(ref value1, 10); // CompareExchange: if 10, change to 20. long result = Interlocked.CompareExchange(ref value1, 20, 10); // Returns original value from CompareExchange [1] Console.WriteLine(result); } Interlocked.Exchange, CompareExchange, and Read If the value is equal to the second argument then it is changed to the third argument.

Interlocked The interlocked Add, Increment, Decrement, Exchange and CompareExchange methods are atomic. This means that no operations can be performed on the value during the call. Interlocked.Add( ) Interlocked.Increment( ) Interlocked.Decrement( ) Interlocked.Exchange( ) Interlocked.CompareExchange( )

Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait and signal commands Also called P and V operations, respectively

Mutual Exclusion with Semaphores Binary semaphore: allow only one thread in its critical section at once –Wait operation If no threads are waiting, allow thread into its critical section Decrement protected variable (to 0 in this case) Otherwise place in waiting queue –Signal operation Indicate that thread is outside its critical section Increment protected variable (from 0 to 1) A waiting thread (if there is one) may now enter

Counting Semaphores Counting semaphores –Initialized with values greater than one –Can be used to control access to a pool of identical resources Decrement the semaphore’s counter when taking resource from pool Increment the semaphore’s counter when returning it to pool If no resources are available, thread is blocked until a resource becomes available

Mutual exclusion with semaphores. Mutual Exclusion with Semaphores