Conditions for Deadlock

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

Deadlock Prevention, Avoidance, and Detection
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
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
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
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.
Chapter 6 Concurrency: Deadlock and Starvation
 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.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
Chapter 7 – Deadlock and Indefinite Postponement
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation
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,
Concurrency, Mutual Exclusion and Synchronization.
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.
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 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
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.
Deadlock Conditions for Deadlock Deadlock Prevention Deadlock Detection Deadlock Recovery Dining Philosophers Semaphores.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
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.
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.
Process Management Deadlocks.
Chapter 5: Process Synchronization – Part 3
Background on the need for Synchronization
ITEC 202 Operating Systems
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.
Chapter 7 Deadlock.
Monitors Chapter 7.
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Monitors Chapter 7.
Concurrency: Mutual Exclusion and Process Synchronization
Andy Wang Operating Systems COP 4610 / CGS 5765
Monitors Chapter 7.
Operating System 6 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION
Introduction to Deadlocks
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

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

Introduction Deadlock System 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

Simple Resource Deadlock 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.

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 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 Solutions Four major areas of interest in deadlock research Deadlock prevention Deadlock avoidance Deadlock detection Deadlock recovery

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 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

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

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

Deadlock Recovery Deadlock recovery Suspend/resume mechanism 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

P0 P1 P2 P3 P4 R0 R4 R1 R3 R2

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

Interlocked.Add using System; using System.Threading; class Program {   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.Increment & Decrement 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.Exchange, CompareExchange, and Read 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); 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 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