CENG334 Introduction to Operating Systems

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
Chapter 7 Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks 7.1 System Model.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
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.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
02/19/2008CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
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, Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Deadlock Characterization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks Modified.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7 Deadlocks. 7.2 Modified By Dr. Khaled Wassif Operating System Concepts – 7 th Edition Silberschatz, Galvin and Gagne ©2005 Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 28 Handling Deadlock.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks System Model Deadlock Characterization Methods.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter Objectives To develop a description of deadlocks, which.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
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);
Deadlock. Examples You can't get a job without experience; you can't get experience without a job. A set of blocked processes each holding a resource.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Thursday, February 23, 2012 Chapter 6 homework questions?
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Synchronization Deadlocks and prevention
Chapter 7: Deadlocks.
Synchronization Deadlocks and prevention
Deadlock Management.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Operating Systems (CS 340 D)
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
Chapter 7 Deadlocks.
Process Deadlocks.
Chapter 7: Deadlocks.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlock B.Ramamurthy CSE421 1/11/2019 B.Ramamurthy.
Lecture 6: Deadlocks, Deadlock Risk Management
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 2/23/2019 B.Ramamurthy.
Deadlocks Session - 13.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 4/23/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 5/1/2019 B.Ramamurthy.
CSCI 315 Operating Systems Design
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 8: Deadlocks Deadlock Characterization
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

CENG334 Introduction to Operating Systems 18/02/08 CENG334 Introduction to Operating Systems Deadlocks Topics: Dining philosopher problem Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL: http://kovan.ceng.metu.edu.tr/~erol/Courses/ceng334

18/02/08 What’s a deadlock?

Deadlock A deadlock happens when 18/02/08 Deadlock A deadlock happens when Two (or more) threads waiting for each other None of the deadlocked threads ever make progress Mutex 1 holds Thread 1 waits for waits for Thread 2 Mutex 2 holds Adapted from Matt Welsh’s (Harvard University) slides.

Deadlock Definition Two kinds of resources: 18/02/08 Deadlock Definition Two kinds of resources: Preemptible: Can take away from a thread e.g., the CPU Non-preemptible: Can't take away from a thread e.g., mutex, lock, virtual memory region, etc. Why isn't it safe to forcibly take a lock away from a thread? Starvation A thread never makes progress because other threads are using a resource it needs Deadlock A circular waiting for resources Thread A waits for Thread B Thread B waits for Thread A Starvation ≠ Deadlock Adapted from Matt Welsh’s (Harvard University) slides.

Dining Philosophers Classic deadlock problem 18/02/08 Dining Philosophers Classic deadlock problem Multiple philosophers trying to lunch One chopstick to left and right of each philosopher Each one needs two chopsticks to eat Adapted from Matt Welsh’s (Harvard University) slides.

18/02/08 Dining Philosophers What happens if everyone grabs the chopstick to their right? Everyone gets one chopstick and waits forever for the one on the left All of the philosophers starve!!! Adapted from Matt Welsh’s (Harvard University) slides.

Dining Philosophers How do we solve this problem?? 18/02/08 Dining Philosophers How do we solve this problem?? (Apart from letting them eat with forks.)‏ Adapted from Matt Welsh’s (Harvard University) slides.

Dining Philosophers How do we solve this problem?? 18/02/08 Dining Philosophers How do we solve this problem?? (Apart from letting them eat with forks.)‏ Adapted from Matt Welsh’s (Harvard University) slides.

Deadlock Characterization 18/02/08 Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

How to solve this problem? 18/02/08 How to solve this problem? Solution 1: Don't wait for chopsticks Grab the chopstick on your right Try to grab chopstick on your left If you can't grab it, put the other one back down Breaks “no preemption” condition – no waiting! Solution 2: Grab both chopsticks at once Requires some kind of extra synchronization to make it atomic Breaks “multiple independent requests” condition! Solution 3: Grab chopsticks in a globally defined order Number chopsticks 0, 1, 2, 3, 4 Grab lower-numbered chopstick first Means one person grabs left hand rather than right hand first! Breaks “circular dependency” condition Solution 4: Detect the deadlock condition and break out of it Scan the waiting graph and look for cycles Shoot one of the threads to break the cycle Adapted from Matt Welsh’s (Harvard University) slides.

Circular Wait - 1 Each resource is given an ordering: 18/02/08 Circular Wait - 1 Each resource is given an ordering: F(tape drive) = 1 F(disk drive) = 2 F(printer) = 3 F(mutex1) = 4 F(mutex2) = 5 ……. Each process can request resources only in increasing order of enumeration. A process which decides to request an instance of Rj should first release all of its resources that are F(Ri) >= F(Rj).

18/02/08 Circular Wait - 2 For instance an application program may use ordering among all of its synchronization primitives: F(semaphore1) = 1 F(semaphore2) = 2 F(semaphore3) = 3 ……. After this, all requests to synchronization primitives should be made only in the increasing order: Correct use: P(semaphore1); P(semaphore2); Incorrect use: P(semaphore3); Keep in mind that it’s the application programmer’s responsibility to obey this order.

Methods for Handling Deadlocks 18/02/08 Methods for Handling Deadlocks How should we handle deadlocks Ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state and then recover. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

18/02/08 Deadlock Avoidance Requires that the system has some additional a priori information available. Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

System Model Resource types R1, R2, . . ., Rm 18/02/08 System Model Resource types R1, R2, . . ., Rm CPU, memory, I/O devices disk network Each resource type Ri has Wi instances. For instance a quad-core processor has 4 CPUs Each process utilizes a resource as follows: request use release Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

Resource-Allocation Graph 18/02/08 Resource-Allocation Graph A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn}, the set consisting of all the processes in the system. R = {R1, R2, …, Rm}, the set consisting of all resource types in the system. request edge – directed edge P1  Rj assignment edge – directed edge Rj  Pi Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

Resource Allocation Graph With A Deadlock 18/02/08 Resource Allocation Graph With A Deadlock If there is a deadlock => there is a cycle in the graph. However the reverse is not true! i.e. If there is a cycle in the graph =/> there is a deadlock Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

Resource Allocation Graph With A Cycle But No Deadlock 18/02/08 Resource Allocation Graph With A Cycle But No Deadlock However the existence of a cycle in the graph does not necessarily imply a deadlock. Overall message: If graph contains no cycles  no deadlock. If graph contains a cycle  if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock. Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

Resource-Allocation Graph Algorithm 18/02/08 Resource-Allocation Graph Algorithm Claim edge Pi  Rj indicated that process Pj may request resource Rj; represented by a dashed line. Claim edge converts to request edge when a process requests a resource. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system. Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

Safe, unsafe and deadlock states 18/02/08 Safe, unsafe and deadlock states If a system is in safe state  no deadlocks. If a system is in unsafe state  possibility of deadlock. Avoidance  ensure that a system will never enter an unsafe state. Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

18/02/08 Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a safe sequence of all processes. Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i. If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. When Pi terminates, Pi+1 can obtain its needed resources, and so on. Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

Resource-Allocation Graph Algorithm 18/02/08 Resource-Allocation Graph Algorithm Claim edge Pi  Rj indicated that process Pj may request resource Rj; represented by a dashed line. Claim edge converts to request edge when a process requests a resource. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system. Note that the cycle detection algorithm does not work with resources that have multiple instances. Cycle => Unsafe Adapted from Operating System Concepts (Silberschatz, Galvin, Gagne) slides.

18/02/08 Banker’s Algorithm While giving credits, a banker should ensure that it never allocates all of its cash in such a way that none of its creditors can finish their work and pay back the loan.

Example The system has three processes and 12 tape drives. 18/02/08 Example The system has three processes and 12 tape drives. 2 9 P2 4 P1 5 10 P0 Current Needs Maximum Needs t=t0 The system at t0 is safe since the sequence <P1,P0,P2> exists.

Example The system has three processes and 12 tape drives. 18/02/08 Example The system has three processes and 12 tape drives. 2 9 P2 4 P1 5 10 P0 Current Needs Maximum Needs t=t0 P2 requests one more drive 3 9 P2 2 4 P1 5 10 P0 Current Needs Maximum Needs t=t1 The system at t1 is no longer safe since P1 requests 2 more tape drives, finishes and releases 4 drives. However 4 drives are not sufficient for P0 or P2 complete its operation and would result in a deadlock.