Algorithm 4Chapter Four- Deadlock (5).pptx

jamsibro140 16 views 32 slides Mar 02, 2025
Slide 1
Slide 1 of 32
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32

About This Presentation

Here is chapter four material use it


Slide Content

Deadlock Chapter 4 1 Operating System

Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Combined Approach to Deadlock Handling 2

The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set . Example: Suppose that process A asks for CD-ROM drive and gets it. A moment later, process B asks for the flatbed plotter and gets it, too. Now process A asks for the plotter and blocks waiting for it. Finally process B asks for CD-ROM drive and also blocks. This situation is called a deadlock. 3

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

Resources A resource is anything that must be acquired, used, and released over the course of time Resource types R 1, R 2, . . ., R m CPU cycles, memory space, I/O devices Each resource type R i has W i instances . A resource is anything that can only be used by a single process at any instance (at any amount). Each process utilizes a resource as follows: request use release 5

Preemptable and Non-preemptable Resources Preemptable resource C an be taken away from a process Example : Memory Non- preemptable resource Cannot be taken away from its current owner without causing the computation to fail. Example : CD-ROM D eadlock involves non-preemptable resources . 6

What is Deadlock? A Deadlock is a situation where a group of processes are permanently blocked as a result of each process having acquired a subset of the resources needed for its completion and waiting for release of the remaining resource held by others in the same group. Deadlock occurs when two or more processes are each waiting for an event that will never occur, since it can only be generated by another process in that set. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. 7

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 { P 0, 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. 8

Deadlock Modeling: Resource-Allocation Graph Two kinds of nodes: Squares — Represent resources –Instances of the resource are represented as dots within the box. Circles — Represent threads / processes. Two kinds of (directed) edges: Request edge — from process to resource — indicates the process has requested the resource, and is waiting to acquire it. Assignment edge —from resource-instance to process— indicates the Process is holding the resource instance. When a request is made, a request edge is added. When request is fulfilled, the request edge is transformed into an assignment edge. When process releases the resource, the assignment edge is deleted. 9

Resource-Allocation Graph (Cont.) 10

Resource-Allocation Graph (Cont.) 11

Example of a Resource Allocation Graph 12

Resource Allocation Graph With A Deadlock 13

Resource Allocation Graph With A Cycle But No Deadlock 14

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

Methods for Handling Deadlocks Four strategies are used for dealing with deadlocks. Just ignore the problem . Detection and recovery Dynamic avoidance Prevention 16

The Ostrich Algorithm The simplest approach is the ostrich algorithm: stick your head in the sand and pretend (assume) there is no problem 17

Deadlock Detection and Recovery Allow system to enter deadlock state Detection algorithm Recovery scheme 18

Deadlock Detection with One Resource of Each Type For such a system, we can construct a resource graph. If this graph contains one or more cycles, a deadlock exists. Any process that is part of a cycle is deadlocked . If no cycles exist, the system is not deadlocked. 19

Deadlock Detection with One Resource of Each Type Example : consider a system with seven processes , A through G , and six resources , R through W . The state of which resources are currently owned and which ones are currently being requested is as follows: Process A holds R and wants S . Process B holds nothing but wants T . Process C holds nothing but wants S . Process D holds U and wants S and T . Process E holds T and wants V . Process F holds W and wants S . Process G holds V and wants U . Q: Is this system deadlocked? And if so, which processes are involved? 20

Resource-Allocation Graph 21

Recovery from Deadlock Recovery through Preemption Select a victim Suspend that process (Mark it as not runnable). Take a resource away from the process and give it to another process . Restart the suspended process. Recovering this way is frequently difficult or impossible. Choosing the process to suspend depends largely on which ones have resources that can easily be taken back. 22

Recovery from Deadlock Recovery through Rollback Having processes check-pointed (a place where actions were stoped ) periodically. Check-pointing a process means that its state is written to a file so that it can be restarted later . The checkpoint contains not only the memory image, but also the resource state, in other words, which resources are currently assigned to the process . To do the recovery, a process that owns a needed resource is rolled back to a point in time before it acquired that resource by starting at one of its earlier checkpoints. 23

Recovery from Deadlock Recovery through Killing Processes Abort(end) one process at a time until the deadlock cycle is eliminated . If this does not help, it can be repeated until the cycle is broken. Alternatively , a process not in the cycle can be chosen as the victim in order to release its resources. In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion . Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch? 24

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

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 < P 1, P 2, …, Pn > is safe if for each P i, 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, P i can obtain needed resources, execute , return allocated resources, and terminate. When Pi terminates, Pi +1 can obtain its needed resources, and so on. 26

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

Safe, unsafe , deadlock state spaces 28

Deadlock Prevention Restrain the ways request can be made Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. Low resource utilization; starvation possible . 29

Deadlock Prevention (Cont.) 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. Preempted resources are added to the list of resources for which the process is waiting. Process will be restarted only when it can regain its old resources , as well as the new ones that it is requesting. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 30

Combined Approach to Deadlock Handling Prevention (Cont.) Combine the three basic approaches prevention avoidance detection allowing the use of the optimal approach for each of resources in the system. Partition resources into hierarchically ordered classes. Use most appropriate technique for handling deadlocks within each class. 31

The End 32