1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 7: Deadlocks.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
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.
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Chapter 7: Deadlocks Adapted to COP4610 by Robert van Engelen.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
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.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks System Model Deadlock Characterization Methods.
Lecture 12 Handling Deadlock – Prevention, avoidance and detection.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
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.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
Chap 7 Deadlocks. Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks To.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling 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 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.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
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.
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.
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 7: Deadlocks.
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Deadlock B.Ramamurthy CSE421 1/11/2019 B.Ramamurthy.
G.Anuradha Ref:- Galvin
Outline Deadlocks, dead lock prevention, avoidance.
Deadlock Prevention Restrain the ways request can be made.
Deadlock B.Ramamurthy CSE421 2/23/2019 B.Ramamurthy.
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.
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof. Mark Corner and Prof. Emery Berger’s OS course at Umass Amherst Assistant Professor, University of Maryland Baltimore County

2 Announcements Homework 2 is out (due Oct 13 th ) Readings from Silberchatz [7 th chapter]

3 Exercise: How do you implement reader writer locks? Shared Data Data set Semaphore mutex initialized to 1 Semaphore wrt initialized to 1 Integer readcount initialized to 0

Readers-Writers Problem (Cont.) The structure of a writer process do { wait (wrt) ; // writing is performed signal (wrt) ; } while (TRUE);

Readers-Writers Problem (Cont.) The structure of a reader process do { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; } while (TRUE);

6 Monitors n A high-level abstraction that provides a convenient and effective mechanism for process synchronization n Abstract data type, internal variables only accessible by code within the procedure n Only one process may be active within the monitor at a time monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } procedure Pn (…) {……} Initialization code (…) { … } }

7 Monitors

8 Implementing Locks using Swap void Swap (bool *a, bool *b) { bool temp = *a; *a = *b; *b = temp: } n Shared Boolean variable lock initialized to FALSE; n Each process has a local Boolean variable key n Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section lock = FALSE; } while (TRUE);

9 Atomic Transactions (Just a Primer!) n Assures that operations happen as a single logical unit of work, in its entirety, or not at all n Related to field of database systems n Challenge is assuring atomicity despite computer system failures n Transaction - collection of instructions or operations that performs single logical function l Here we are concerned with changes to stable storage – disk l Transaction is series of read and write operations l Terminated by commit (transaction successful) or abort (transaction failed) operation l Aborted transaction must be rolled back to undo any changes it performed

Dining-Philosophers Problem Philosophers spend their lives thinking and eating Don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl –Need both to eat, then release both when done In the case of 5 philosophers –Shared data Bowl of rice (data set) Semaphore chopstick [5] initialized to 1

Dining-Philosophers Problem Algorithm The structure of Philosopher i: do { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE); What is the problem with this algorithm?

Deadlock teminology  Deadlock  Deadlock detection –Finds instances of deadlock when threads stop making progress –Tries to recover  Deadlock prevention algorithms –Check resource requests & availability

13 Rules for Deadlock All necessary and none sufficient

14 Rules for Deadlock All necessary and none sufficient Finite resource –Resource can be exhausted causing waiting

15 Rules for Deadlock All necessary and none sufficient Finite resource –Resource can be exhausted causing waiting Hold and wait –Hold resource while waiting for another

16 Rules for Deadlock All necessary and none sufficient Finite resource –Resource can be exhausted causing waiting Hold and wait –Hold resource while waiting for another No preemption –Thread can only release resource voluntarily –No other thread or OS can force thread to release

17 Rules for Deadlock All necessary and none sufficient Finite resource –Resource can be exhausted causing waiting Hold and wait –Hold resource while waiting for another No preemption –Thread can only release resource voluntarily –No other thread or OS can force thread to release Circular wait –Circular chain of waiting threads

18 Circular Waiting If no way to free resources (preemption)

19 Deadlock Detection Define graph with vertices: –Resources = {r1, …, rm} –Threads or processes = {t1, …, tn} Request edge from thread to resource –(ti → rj) Assignment edge from resource to thread –(rj → ti) –OS has allocated resource to thread Result: –No cycles  no deadlock –Cycle  may deadlock

20 Example Deadlock or not? Request edge from thread to resource ti -> rj –Thread: requested resource but not acquired it (waiting) Assignment edge from resource to thread rj -> ti –OS has allocated resource to thread

Graph With A Cycle But No Deadlock

22 Quick Exercise Draw a graph for the following event: Request edge from thread to resource –ti -> rj Assignment edge from resource to thread –rj -> ti

23 Resource Allocation Graph Draw a graph for the following event:

24 Detecting Deadlock Scan resource allocation graph for cycles –Then break them! Different ways to break cycles: –Kill all threads in cycle –Kill threads one at a time Force to give up resources –Preempt resources one at a time Roll back thread state to before acquiring resource Common in database transactions

25 Deadlock Prevention Instead of detection, ensure at least one of necessary conditions doesn’t hold –Mutual exclusion –Hold and wait –No preemption –Circular wait

26 Deadlock Prevention Mutual exclusion –Make resources shareable (but not all resources can be shared) Hold and wait –Guarantee that thread cannot hold one resource when it requests another –Make threads request all resources they need first and release all before requesting more

27 Deadlock Prevention, continued No preemption –If thread requests resource that cannot be immediately allocated to it OS preempts (releases) all resources thread currently holds –When all resources available: OS restarts thread Not all resources can be preempted!

28 Deadlock Prevention, continued Circular wait –Impose ordering (numbering) on resources and request them in order –Most important trick to correct programming with locks!

29 Avoiding Deadlock Cycle in locking graph = deadlock Typical solution: canonical order for locks –Acquire in increasing order E.g., lock_1, lock_2, lock_3 –Release in decreasing order Ensures deadlock-freedom –but not always easy to do

30 lock (a); lock (b); unlock (b); unlock (a); lock (b); lock (a); unlock (a); unlock (b); Avoiding Deadlock Avoiding deadlock: is this ok?

31 lock (a); lock (b); unlock (b); unlock (a); lock (b); lock (a); unlock (a); unlock (b); lock (a); lock (b); unlock (b); unlock (a); lock (a); lock (b); unlock (b); unlock (a); Avoiding Deadlock Not ok – may deadlock. Solution: impose canonical order (acyclic)

Deadlock Avoidance 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 Requires that the system has some additional a priori information available

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 sequence of ALL the processes in the systems such that for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < I That is: –If P i resource needs are not immediately available, then P i can wait until all P j have finished –When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate –When P i terminates, P i +1 can obtain its needed resources, and so on

Basic Facts 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.

Safe, Unsafe, Deadlock State

Avoidance algorithms Single instance of a resource type –Use a resource-allocation graph Multiple instances of a resource type – Use the banker’s algorithm

Resource-Allocation Graph Scheme Claim edge P i  R j indicated that process P j may request resource R j ; represented by a dashed line Claim edge converts to request edge when a process requests a resource Request edge converted to an assignment edge when the resource is allocated to the process When a resource is released by a process, assignment edge reconverts to a claim edge Resources must be claimed a priori in the system

Resource-Allocation Graph

Unsafe State In Resource-Allocation Graph

Resource-Allocation Graph Algorithm Suppose that process P i requests a resource R j The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph

Banker’s Algorithm Multiple instances Each process must a priori claim maximum use When a process requests a resource it may have to wait When a process gets all its resources it must return them in a finite amount of time

Example of the Banker’s Algorithm n 5 processes P 0 through P 4 ; 3 resource types: A (10 instances), B (5instances), and C (7 instances) Snapshot at time T 0 : Allocation MaxAvailable A B C A B C A B C P P P P P

Example of the Banker’s Algorithm n The content of the matrix Need is defined to be Max – Allocation Need A B C P P P P P n The system is in a safe state since the sequence satisfies safety criteria

Example of the Banker’s Algorithm n Check that Request  Available (that is, (1,0,2)  (3,3,2)  true AllocationNeedAvailable A B CA B CA B C P P P P P n Executing safety algorithm shows that sequence satisfies safety requirement n Can request for (3,3,0) by P 4 be granted? n Can request for (0,2,0) by P 0 be granted?

45 An in-class discussion (surprise : Java swapping)