Operating Systems (CS 340 D) Princess Nora University Faculty of Computer & Information Systems Computer science Department.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 7: Deadlocks.
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.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Deadlocks  (How to Detect Them and Avoid Them) A:
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.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
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.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Objectives Understand 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 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.
Chapter 7: Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process.
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.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department.
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.
Mi-Jung Choi Dept. of Computer and Science Silberschatz, Galvin and Gagne ©2006 Operating System Principles Chapter 7: Deadlocks.
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.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Deadlock A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. Example : “When.
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.
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.
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Operating Systems (CS 340 D)
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 1/11/2019 B.Ramamurthy.
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.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

Operating Systems (CS 340 D) Princess Nora University Faculty of Computer & Information Systems Computer science Department

(Chapter-7) Deadlocks

Chapter 7: Deadlocks 1.The Deadlock Problem 2.Deadlock Characterization 3.Methods for Handling Deadlocks 3

OBJECTIVES :  To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks  To present methods for Handling Deadlocks 4

The Deadlock Problem 5

 In a multiprogramming environment, several processes may compete for a finite number of resources.  A process requests resources; if the resources are not available at that time, the process enters a waiting state.  Sometimes, a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes. This situation is called a DEADLOCK. 6

 What is a deadlock problem?? o A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set  Example o System has 2 disk drives o P 1 and P 2 each hold one disk drive and each need the other one  Note :– Most OSs do not prevent or deal with deadlocks The Deadlock Problem (cont..) 7

Bridge Crossing Example  Traffic only in one direction  Each section of a bridge can be viewed as a resource  If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback)  Several cars may have to be backed up if a deadlock occurs  Starvation is possible 8

System Model  The resources are partitioned into several types (R 1, R 2,..., R m ) o E.g.: CPU cycles, memory space, I/O devices  Each resource type R i has W i instances. o E.g :  If a system has (2) CPUs, then the resource type CPU has (2) instances.  The resource type printer may have (5) instances. 9 Instances are identical

10  Each process utilizes a resource as follows: 1. Request >> If the request cannot be granted immediately,then the requesting process must wait until it can acquire the resource. 2. Use 3. Release  A set of processes is in a deadlocked state when every process in the set is waiting for an event that can be caused only by another process in the set.

System Model (cont.)  Resource forms:  Example:  consider a system with one printer and one DVD drive. Suppose that process Pi is holding the DVD and process Pj is holding the printer. If Pi requests the printer and Pj requests the DVD drive, a deadlock occurs. 11 Physical resources Logical resources e.g. printers, DVD drives, memory space, and CPU cycles e.g. files

The Deadlock Characterization 12

Deadlock Characterization 1. Mutual exclusion: the resources are sharable or non sharable  mutual exclusion happen when only one process at a time can use a resource…(i.e. No resource sharing) 2. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes Deadlock can arise if four conditions hold simultaneously: 13

Deadlock Characterization (cont..) 3. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task 4. Circular wait:  There exists a set {P 0, P 1, …, P n } of waiting processes such that : o P 0 is waiting for a resource that is held by P 1, o P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, o P n is waiting for a resource that is held by P P0P0 P1P1 P2P2 PnPn

Resource-Allocation Graph 15

Resource-Allocation Graph  Deadlocks can be described in terms of a directed graph called a system resource-allocation graph.  This graph consists of a set of vertices V and a set of edges E. 16 V is partitioned into two types: E is partitioned into two types: P = {P 1, P 2, …, P n }, the set consisting of all the processes in the system Request edge – directed edge P i  R j R = {R 1, R 2, …, R m }, the set consisting of all resource types in the system Assignment edge – directed edge R j  P i

Resource-Allocation Graph (Cont.)  Process  Resource Type with 4 instances  P i requests instance of R j  P i is holding an instance of R j PiPi PiPi RjRj RjRj 17

Example of a Resource Allocation Graph n The sets P, R, and E:  P = {P1, P2, P3}  R = {R1, R2, R3, R4}  E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3} n Resource instances:  (1) instance of resource type R1  (2) instances of resource type R2  (1) instance of resource type R3  ( 3) instances of resource type R4 18

Example of a Resource Allocation Graph n Process states:  Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1.  Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3.  Process P3 is holding an instance of R3. 19

Basic Facts  How we recognize deadlock by using a Resource Allocation Graph??  If graph contains no cycles  no deadlock  If graph contains a cycle   If only one instance per resource type, then there is a deadlock  If several instances per resource type, possibility of deadlock. 20

Resource Allocation Graph With NO Deadlock n Example (1): l The graph contains NO cycles  NO deadlock is occurred. 21

l It ‘s a dead lock situation >>> Processes P1, P2, and P3 are deadlocked…….why??  There is no chance to break the cycle. Resource Allocation Graph With a Deadlock 22 n Example (2): l The graph contains (2)cycles  may be a deadlock is occurred.  Cycle (1) : P1 → R1 → P2 → R3 → P3 → R2 → P1  Cycle (2) : P2 → R3 → P3 → R2 → P2

Graph With A Cycle But No Deadlock l NO dead lock situation.....WHY????  process P4 may release its instance of resource type R2…… That resource can then be allocated to P3, breaking the cycle. 23 n Example (3): l The graph contains (1)cycle  may be a deadlock Cycle : P1 → R1 → P3 → R2 → P1

Methods for Handling Deadlocks 24

Methods for Handling Deadlocks  OS can deal with the deadlock problem in one of three ways 25 1-prevent or avoid deadlocks 2-Allow3-Ignore ensuring that the system will never enter a deadlocked state. Allow the system to enter a deadlocked state, detect it, and recover. Ignore the problem  Used by most operating systems, including Unix & windows  This method is cheaper than 1 or 2.  Solution: The system must be restarted manually

26  To ensure that deadlocks never occur, the system can use either 1. a deadlock prevention or 2. a deadlock-avoidance scheme Method (1) for Handling Deadlocks

27 Deadlock preventionDeadlock avoidance provides a set of methods to ensure that at least one of the necessary 4 conditions (explained before ) cannot hold. These methods prevent deadlocks by constraining how requests for resources can be made. requires that the operating system be given additional information in advance concerning which resources a process will request and use during its lifetime. With this additional knowledge, the operating system can decide for each request whether or not the process should wait. Method(1) for Handling Deadlocks

1-Deadlock Prevention 28

Deadlock Prevention 1- Mutual Exclusion – not required for sharable resources; must hold for non- sharable resources.  That is, at least one resource must be nonsharable. Sharable resources, in contrast, do not require mutually exclusive access and thus cannot be involved in a deadlock 2- Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources by using one of the following protocols: Problems-> Low resource utilization; starvation possible Remove the possibility of deadlock occurring by denying one of the four necessary conditions: The process must request all its resources before execution. or allow process to request resources only when the process has none 29

Deadlock Prevention (Cont.) 3- No Preemption –  If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released  Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting  It is applied to resources whose state easily saved and restored later, such as CPU registers and memory space but it not applicable for resources such as printers  The main Advantages is more better resource utilization  Problems The cost of removing a process's resources starvation possible It is not applicable for all resource types such as printers 30

31  One way to ensure that this condition never holds is to impose a total ordering of all resource types and to require that each process requests resources in an increasing order of enumeration.  let R = {R1, R2,..., Rm} be the set of resource types. We assign to each resource type a unique integer number, which allows us to compare two resources and to determine whether one precedes another in our ordering.  Formally, we define a one-to-one function F: R → N, where N is the set of natural numbers. 4- Circular Wait –

Deadlock Prevention (Cont.)  Example : if the set of resource types R includes tape drives, disk drives, and printers,  then the function F might be defined as follows: F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12  We can now consider the following protocol to prevent deadlocks:  Each process can request resources only in an increasing order of enumeration.  That is, a process can initially request any number of instances of a resource type —say, Ri. After that, the process can request instances of resource type Rj if and only if F(Rj ) > F(Ri ). …………..(Protocol#1) 4- Circular Wait – cont.. 32

Deadlock Prevention (Cont.)  EX: a process that wants to use the tape drive and printer at the same time must first request the tape drive and then request the printer.  Alternatively, we can require that a process requesting an instance of resource type Rj must have released any resources Ri such that F(Ri ) ≥ F(Rj ). …..(Protocol#2)  Problems Resources must be requested in ascending order of resource number rather than as needed 4- Circular Wait – cont.. 33

2-Deadlock Avoidance 34

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 35

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 Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < I o That is: If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished o When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate o When Pi terminates, Pi +1 can obtain its needed resources, and so on 36

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

Safe, Unsafe, Deadlock State 38

Avoidance algorithms The avoidance algorithms ensure that the system will always remain in a safe state.  Single instance of a resource type  Use a resource-allocation graph  Multiple instances of a resource type  Use the banker’s algorithm 39

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 40

Resource-Allocation Graph 41

Unsafe State In Resource-Allocation Graph 42

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 form of a cycle in the resource allocation graph 43

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  Banker's Algorithm consist of (2) sub-algorithms: o Safety algorithm o Recourse-Request Algorithm  44

Banker’s Algorithm  When a new process enters the system, it must declare the maximum number of instances of each resource type that it may need. This number may not exceed the total number of resources in the system.  When a process requests a set of resources, the system must determine whether the allocation of these resources will leave the system in a safe state. o If it will, the resources are allocated; o otherwise, the process must wait until some other process releases enough resources. 45

Data Structures for the Banker’s Algorithm 46 nAvailable: Vector of length m. If available [j] = k, there are k instances of resource type Rj available nMax: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj nAllocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj nNeed: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task Need [i,j] = Max[i,j] – Allocation [i,j] Let n = number of processes, and m = number of resources types.

Safety Algorithm 47 1.Let Work and Finish be vectors of length m and n, respectively. Initialize:  Work = Available  Finish [i] = false for i = 0, 1, …, n- 1 2.Find any i such that both:  (a) Finish [i] = false  (b) Need i  Work  If no such i exists, go to step 4 3. Work = Work + Allocation i Finish[i] = true go to step 2 4.If Finish [i] == true for all i, then the system is in a safe state

48 Resource-Request Algorithm for Process Pi  Request = request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j 1.If Request i  Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim 2.If Request i  Available, go to step 3. Otherwise P i must wait, since resources are not available 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available – Request; Allocation i = Allocation i + Request i ; Need i = Need i – Request i ; lIf safe  the resources are allocated to Pi lIf unsafe  Pi must wait, and the old resource-allocation state is restored

Example of Banker’s Algorithm  A system with 5 processes (p 0,p 1,p 2,p 3,p 4 ), 3 resource types )A,B,C) with instances (10,5,7)  Snapshot at time T 0 : AllocationMax need ABCABC P P P P p

Example of Banker’s Algorithm (Cont.) AllocationMAXNeedAvailable ABCABCABCABC P P P P p Is system now in safe state? - If a request R 1 = (1,0,2) arrive, Can this request be granted immediately?

Example of Banker’s Algorithm (Cont.) AllocationMAXNeedAvailable ABCABCABCABC P P P P p Calculate new state after granting request R 1 = (1,0,2)

Example of Banker’s Algorithm (Cont.) - If a request R 4 = (3,3, 0) arrive, Can this request be granted immediately? - If a request R 0 = (0,2, 0) arrive, Can this request be granted immediately? - If a request R 3 = (1, 1, 1) arrive, Can this request be granted immediately? AllocationMAXNeedAvailable ABCABCABCABC P P P P p

Thank you End of Chapter 7 53