chapter 7: deadlocks - · pdf filechapter 7: deadlocks ... •a system with cd1 and cd2...
TRANSCRIPT
Chapter 7: Deadlocks
• The Deadlock Problem
• System Model
• Deadlock Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
The Deadlock Problem• Deadlock:
• A process requests resources; if the resources are not available atthat time, the process enters a waiting state. Sometimes, a waitingprocess can not change its state, because the resources it hasrequested are held by other waiting processes.
• Example of deadlocks:
• A system with CD1 and CD2 drives allocated to P1 and P2. P1requested CD2 and P2 requested CD1. Each is waiting for the event“CD is released,” which can be caused only by one of the otherwaiting processes(same resource type deadlock).
• A system with one printer and one DVD. Suppose that process Pi isholding the DVD and process Pj is holding the printer. If Pi requeststhe printer and Pj requests the DVD drive, a deadlock occurs.
• Semaphores A and B, initialized to 1 P0 P1wait (A); wait(B) wait (B); wait(A)
System Model• A system have a finite number of resources R1,R2,R3,…Rm divided into
several resource types/classes (CPU cycles, files, and I/O devices) each
consisting of some number of identical instances (CPU1 & CPU2).
• These resources can requested by a number of processes P1,P2,…., Pn.
• If a process requests an instance of a resource type, any instance of the type
should satisfy the request. Else they are not identical.
• A process may utilize a resource in only the following sequence:
• Request: The process requests the resource. If it cannot be granted
immediately, the process must wait until it can acquire the resource.
• Use: The process can operate on the resource.
• Release: The process releases the resource.
System Model• The request and release of resources may be system calls:
• request() ,release() device
• open() ,close() file, and allocate()
• free() memory system calls.
• Resource allocation table:
• is a system table that records whether each resource is free or
allocated by which process.
• If a process requests a resource type that is currently allocated to
another process, it can be added to a queue of processes waiting for this
resource.
• Each process of a set of processes is in a deadlocked state is waiting
for an event that can be caused only by another process in the set.
• This event is resource acquisition and release.
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
• Note – Most OS do not prevent or deal with deadlocks
Deadlock CharacterizationDeadlock can arise if four conditions hold simultaneously.
•Mutual exclusion: only one process at a time can use aresource, If another process requests that resource, therequesting process must be delayed until the resource hasbeen released.
•Hold and wait: a process holding at least one resource iswaiting to acquire additional resources held by otherprocesses
•No preemption: a resource can be released only voluntarilyby the process holding it, after that process has completed itstask
•Circular wait: there exists a set {P0, P1, …, Pn} of waitingprocesses such that P0 is waiting for a resource that is held byP1, P1 is waiting for a resource that is held by P2, …, Pn–1 iswaiting for a resource that is held by Pn, and Pn is waiting fora resource that is held by P0.
Resource Allocation Graph• This graph consists of:
• A set of vertices V (all the active processes in the system Process P = {P1, P2, ..., Pn },and all resource types in the system Resource R = {R1, R2, ..., Rm}).
• A set of edges E:
• Request Edge (Pi → Rj): Process Pi has requested an instance of resource type Rj and iscurrently waiting for that resource. points to only the rectangle Rj
• assignment edge (Rj → Pi ): An instance of resource type Rj has been allocated toprocess Pi . One dot in the rectangular.
• Work Sequence:• Each process Pi as a circle• Each resource type Rj as a rectangle (each instance as a dot within the rectangle).• A request edge is inserted in the resource-allocation graph When process Pi requests
an instance of resource type Rj .• The request edge is instantaneously transformed to an assignment edge When this
request can be fulfilled .• When the process no longer needs the resource, it releases the resource.
PiRj
Pi
Rj
Process
Resource Type of 4 instants
Pi requests instance of Rj
Pi is holding an instance of Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Given the resource-allocation graph:
• If the graph contains no cycles, no deadlock.
• If the graph does contains a cycle, then a deadlock
may exist.
• If each resource type has exactly one instance, then a
cycle means that a deadlock has occurred.
• If the cycle involves only a set of resource types, each
has only a single instance, then a deadlock has
occurred.
• Each process involved in the cycle is deadlocked.
• In this case, a cycle in the graph is both a necessary
and a sufficient condition for the existence of
deadlock.
• If each resource type has several instances, then a
cycle does not necessarily imply that a deadlock has
occurred.
• A cycle in the graph is a necessary but not a sufficient
condition for the existence of deadlock. Graph With A Cycle But No Deadlock
Resource Allocation Graph
Methods for Handling Deadlocks• Ensure that the system will never enter a deadlock state ,
the system can use either:• Deadlock prevention: a set of methods to ensure that at least one
of the necessary conditions cannot hold.• Deadlock avoidance requires that the OS be given additional
information in advance concerning which resources a process willrequest and use during its lifetime. the OS can decide for eachrequest whether or not the process should wait.
• Allow the system to enter a deadlock state and thenrecover, by provide an algorithm that examines the state ofthe system to determine whether a deadlock has occurredand an algorithm to recover from the deadlock.
• Ignore the problem and suppose that deadlocks neveroccur in the system; used by most OS, including UNIX andwindows, the application developer to write programs thathandle deadlocks
Deadlock Prevention• 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 processrequests a resource, it does not hold any other resources
• Require process to request and be allocated all its resources beforeit begins execution
• Allow process to request resources only when the process has none• Low resource utilization; starvation possible
• No Preemption –• If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then allresources currently being held are released
• Preempted resources are added to the list of resources for whichthe process is waiting
• Process will be restarted only when it can regain its old resources, aswell as the new ones that it is requesting
Deadlock AvoidanceRequires that the system has some additional a priori information available
• Simplest and most useful model requires that each process declare themaximum number of resources of each type that it may need
• The deadlock-avoidance algorithm dynamically examines the resource-allocationstate to ensure that there can never be a circular-wait condition
• Resource-allocation state is defined by the number of available and allocatedresources, and the maximum demands of the processes
Safe State• When a process requests an available resource, system must
decide if this allocation leaves the system in a safe state
• System is in safe state That is:• If P0 requested resource are not immediately available.
• P0 can wait until all P1 have finished, When P1 is finished, P0 can obtain requested resources, execute, return allocated resources, and terminate
• When Pi terminates, Pi +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.
• 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• Used If we have a resource-allocation system with only one instance of each
resource type.
• A new type of edge called Claim edge Pi Rj indicated that process Pj mayrequest resource Rj; 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 allocatedto the process
• When a resource is released by a process, assignment edge reconverts to a claimedge
• Resources must be claimed a priori in the system before starting execution of theprocess all its claim edges must appear on the resource allocation graph.
Banker’s Algorithm• Multiple instances per each resource.
• 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 oftime.
Data Structures for the Banker’s Algorithm • Let n = number of processes, and m = number of resources types.
• Available[i]: Vector of length m. • If available [j] = k, there are k instances of resource type Rj available
• Max[I,j]: n x m matrix. • If Max [i,j] = k, then process Pi may request at most k instances of type Rj.
• Allocation[I,j]: n x m matrix. • If Allocation[i,j] = k then Pi is currently allocated k instances of Rj
• Need[I,j] 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]
Safety Algorithm
Resource-Request Algorithm for Process Pi
Example of Bankers Algorithm
• Consider a system with five processes P0 through P4 and three resource types A (10
instances), B (5 instances), and C (7 instances).
• Suppose that, at time T0, the following snapshot of the system:
IF P1 requests additional (A(1),B(0),C(2)). To decide whether this request can be
immediately granted, we first check that Request t1 ≤ Available— (1,0,2) ≤ (3,3,2).
Available = System resources – Allocated by all process
Max is max required for execute process – Need = Max(P) – Allocated (P)
Deadlock Detection•If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, thena deadlock situation may occur.
•In this environment, the system may provide:•An algorithm that examines the state of the system to
determine whether a deadlock has occurred•An algorithm to recover from the deadlock
Single Instance of Each Resource Type• Make a small modification on the Resource Allocation Graph ( Resulted Wait-For Graph)
• Maintain wait-for graph• An edge from Pi to Pj in a wait-for graph implies that process Pi is waiting for process Pj to
release a resource that Pi needs. • An edge Pi → Pj exists in a wait-for graph if and only if the corresponding resource allocation
graph contains two edges Pi → Rq and Rq → Pj for some resource• Nodes are processes• Pi Pj if Pi is waiting for Pj
• Periodically invoke an algorithm that searches for a cycle in the graph.
• If there is a cycle, there exists a deadlock
• An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph.
Resource-Allocation Graph Corresponding wait-for graph
Several Instances of a Resource Type• Available: A vector of length m indicates the number of available resources of
each type.
• Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process.
• Request: An n x m matrix indicates the current request of each process. If Request [i][j] = k, process Pi is requesting k more instances of resource type. Rj.
Several Instances of a Resource Type
Several Instances of a Resource Type
Example of Detection Algorithm• Five processes P0 through P4; three resource types A (7 instances), B (2
instances), and C (6 instances)
• The snapshot of the system at time T0 is:
We claim that the system is not in a
deadlocked state. Indeed, if we execute
our algorithm, we will find that the sequence
<P0, P2, P3, P1, P4> results in
Finish[i] == true for all i.
P2 request(0,0,1)
Deadlock have occurred because
the requested is less than the
available
Recovery from Deadlock
1. Process Termination• Abort all deadlocked processes• Abort one process at a time until the deadlock cycle is
eliminated• 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?
2. Resource Preemption• Selecting a victim – minimize cost
• Rollback – return to some safe state, restart process for that state
• Starvation – same process may always be picked as victim, include number of rollback in cost factor
End of Chapter 7