Upload
cameron-freeman
View
261
Download
1
Embed Size (px)
DESCRIPTION
7.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 What is a Deadlock? A deadlock is impossibility to change state of a waiting process because the resources it has requested are held by other waiting processes. It is very desirable to prevent deadlocks If they can occur, we need to take care of their handling through an operating system
Citation preview
Chapter 7: DeadlocksChapter 7: Deadlocks
7.2 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Chapter 7: DeadlocksChapter 7: Deadlocks
The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock
7.3 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
What is a Deadlock?What is a Deadlock?
A deadlock is impossibility to change state of a waiting process because the resources it has requested are held by other waiting processes.
It is very desirable to prevent deadlocks If they can occur, we need to take care of
their handling through an operating system
7.4 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
The Deadlock ProblemThe 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 System has 2 disk drives. P1 and P2 each hold one disk drive and each
needs another one.
7.5 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Bridge Crossing ExampleBridge 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.
7.6 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
System ModelSystem Model
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices Each resource type Ri has Wi instances (sectors on the hard
disc, pages in the main memory, multiple CPUs, etc.) The request and release of resources are system calls
( request(), release () device; open(), close (file) file; allocate(), free() memory, etc.)
Each process utilizes a resource as follows: Request (followed by the allocation of the resource) use release
7.7 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock CharacterizationDeadlock Characterization
Mutual exclusion: only one process at a time can use a resource. If another process requests that resource, the requesting process must be delayed until the resource has been released.
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 Pn is waiting for a resource that is held by P0.
Deadlock can arise if four conditions hold simultaneously.
7.8 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation GraphResource-Allocation Graph
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 Pi Rj
assignment edge – directed edge Rj Pi
A set of vertices V and a set of edges E.
7.9 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph (Cont.)Resource-Allocation Graph (Cont.)
Process
Resource Type with 4 instances
Pi requests instance of Rj - request edge
Pi is holding an instance of Rj – assignment edge
Pi
Pi Rj
Rj
7.10 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Example of a Resource Allocation GraphExample of a Resource Allocation Graph
7.11 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
The Deadlock ConditionThe Deadlock Condition
It can be shown that if the graph contains no cycles, then no process in the system is deadlocked.
If the graph does contain a cycle, then a deadlock may exist.
7.12 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Resource Allocation Graph With A DeadlockResource Allocation Graph With A Deadlock
Cycles:
P1R1P2R3P3R2P1
P2R3P3R2P2
Processes P1, P2 and P3 are deadlocked:
P2 is waiting for R3, which is held by P3
P3 is waiting for either P1 or P2 to release R2
P1 is waiting for P2 to release R1
7.13 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Graph With A Cycle But No DeadlockGraph With A Cycle But No Deadlock
Cycle:
P1R1P3R2P1
There is no deadlock:
P4 may release its instance on resource type R2. That resource can then be allocated to P3, breaking the cycle.
7.14 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Basic FactsBasic 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.
7.15 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Methods for Handling DeadlocksMethods for Handling 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.
7.16 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Methods for Handling DeadlocksMethods for Handling Deadlocks
If a system does not employ either a deadlock-avoidance or deadlock-prevention algorithm, then a deadlock situation may arise.
If a system neither ensures that a deadlock will never occur nor provides a mechanism for deadlock detection and recovery, then we may arrive at a situation where the system is in a deadlocked state and there is no way of recognizing what has happened.
7.17 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Methods for Handling DeadlocksDeadlock Methods for Handling Deadlocks
Deadlock prevention means that a system has to ensure that at least one of the four deadlock conditions cannot hold.
Deadlock avoidance requires that the operating system be given in advance additional information concerning which resources a process will request and use during its lifetime.
To decide whether the current request can be satisfied or must be delayed, the system must consider the resources currently available, the resources currently allocated to each process, and the future requests and releases of each process.
7.18 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock CharacterizationDeadlock Characterization
Mutual exclusion: only one process at a time can use a resource. If another process requests that resource, the requesting process must be delayed until the resource has been released.
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 Pn is waiting for a resource that is held by P0.
Deadlock can arise if four conditions hold simultaneously.
7.19 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock PreventionDeadlock Prevention Mutual Exclusion – not required for sharable
resources (a read-only file is a typical example of a sharable resource), a process never needs to wait for a sharable resource; must hold for nonsharable resources (a printer is a typical example of such a resource). In general, this condition can not be denied.
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Require a 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.
7.20 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Prevention (Cont.)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.
7.21 Silberschatz, Galvin and Gagne ©2005Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock AvoidanceDeadlock 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.