lect9 deadlocks
TRANSCRIPT
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 1/34
OPERATING S YSTEMS
(IS ZC362 LECTURE
9)
DEADLOCK
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 2/34
Process states
2
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 3/34
The Deadlock Problem
A set of blocked processes each holding aresource and waiting to acquire a resourceheld by another process in the set
Permanent blocking of a set of processes thateither compete for system resources orcommunicate with each other
No efficient solution
Involve conflicting needs for resources by twoor more processes
3
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 4/34
Examples Example 1
System has 2 disk drives
P 1 and P 2 each hold one disk drive and eachneeds another one
4
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 5/34
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 OSes do not prevent or deal with
deadlocks
5
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 6/34
6
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 7/34
System Model
Processes P1, P2, …, Pn
Resource types R 1, R 2, . . ., R mCPU cycles, memory space, I/O devices
Each resource type R i has W i instances.
7
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 8/34
8
Resource Allocation Request
If the request cannot be granted immediately then therequesting process must wait until it can acquire theresource.
Use
Process operates on resource. Release
Process release the resource.
Resources
Physical resource -- Printer,Memory space….. Logical resource --- Semaphore,Files…..
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 9/34
9
Reusable Resources
Used by only one process at a time and notdepleted by that use
Processes obtain resources that they laterrelease for reuse by other processes
Processors, I/O channels, main and secondarymemory, devices, and data structures such asfiles, databases, and semaphores
Deadlock occurs if each process holds oneresource and requests the other
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 10/34
10
Example of Deadlock Space is available for allocation of
200Kbytes, and the following sequence ofevents occur
Deadlock occurs if both processes progressto their second request
P1
. . .
. . .Request 80 Kbytes;
Request 60 Kbytes;
P2
. . .
. . .Request 70 Kbytes;
Request 80 Kbytes;
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 11/34
11
Consumable Resources Created (produced) and destroyed (consumed)
Interrupts, signals, messages, and information inI/O buffers
There is no limit on the number of consumableresources of a particular type.
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 12/34
12
Example of Deadlock Deadlock occurs if receive is blocking
P1
. . .
. . .
Receive(P2);
Send(P2, M1);
P2
. . .
. . .
Receive(P1);
Send(P1, M2);
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 13/34
Deadlock Characterization
Mutual exclusion: only one process at a time can use a resource.
At least one resource must be in a non sharablemode; that is only one process at a time can use a
resource. If any other process request the resource, the
requesting process must be delayed until theresource has been released.
Hold and wait: a process holding at least one resource is waiting to
acquire additional resources held by other processes
13
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 14/34
Deadlock Characterization
No preemption: a resource can be released only voluntarily by the
process holding it, after that process has completed itstask .(The resource cannot be preempted).
Circular wait: there exists a set {P 0, P 1, …, P 0} of waiting processessuch that
P 0 is waiting for a resource that is held by P 1,
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, and
P n is waiting for a resource that is held by P 0.
14
Deadlock can arise if all four conditions hold simultaneously.
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 15/34
Traffic Deadlock
15
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 16/34
Resource-Allocation Graph
V is partitioned into two types:
P = {P 1, P 2, …, P n }, the set consisting of all theprocesses in the system
R = {R 1, R 2, …, R m }, the set consisting of all
resource types in the system
request edge – directed edge P 1 R j assignment edge – directed edge R j P i
A set of vertices V and a set of edges E .
16
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 17/34
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
P i
R j
P i
R j 17
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 18/34
Example of a Resource Allocation Graph
18
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 19/34
Resource Allocation Graph With A Deadlock
19
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 20/34
Graph With A Cycle But No Deadlock
20
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 21/34
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
Most of the Operating Systems are nothandling Deadlock problems
It is too costly to prevent / avoid / detect – recoverdeadlock
21
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 22/34
Methods 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; used bymost operating systems, including UNIX
22
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 23/34
Deadlock Prevention
Ensure that at least one of the 4 necessarycondition be prevented.
Mutual Exclusion – not required for sharableresources; must hold for non-sharableresources
Only one process may use a resource at a time
Restrain the ways request can be made
23
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 24/34
Hold and Wait – must guarantee thatwhenever a process requests a resource, itdoes not hold any other resources
Require process to request and be allocated all itsresources before it begins execution
Implementation: System calls requestingresources precede all other function calls
Low resource utilization; Starvation possible
24
Deadlock Prevention (Cont.)
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 25/34
Alternative method: Allow a process to request resources only when
the process has no resources.
Release all resources before requesting for a new
one This method is inefficient
A process may be held up for a long time waiting forall of its resource request be filled, when it could have
proceeded with only some of the resources. A process may not know in advance all of the
resources that it will require.
25
Deadlock Prevention (Cont.)
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 26/34
Deadlock Prevention (Cont.) No Preemption
If a process that is holding some resources requests
another resource that cannot be immediately allocated toit, then all resources currently being held are released
Preempted resources are added to the list of resourcesfor which the process is waiting
Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting
Alternate Approach:
If a process request some resources,
Allot to the process if the resource is free
Else, check whether they are allocated to some otherprocess that is waiting for additional resource. If so,preempt the resource from the waiting process and allotthem to the requesting process.
26
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 27/34
Circular Wait
Impose a total ordering of all resource types,and require that each process requests
resources in an increasing order ofenumeration
27
Deadlock Prevention (Cont.)
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 28/34
28
Example
Assign a unique integer number to individual resource which allowsus to compare two resources and determine one proceed other.
Define a one – to – one function F: R
N F(tape drive)=1
F(disk drive)=5
F(printer) =12
Process can request resources only in an increasing order of
enumeration.
So R(j) is acceptable to a process if and only if F(R(i))< F(R(j)). IfF(R(i))> F(R(j)) then release (R(i)) before R(j) entered in.
Example: Process needs tape drive and printer must request thetape driver first then printer.
Disadvantage of Deadlock Prevention
Low device Utilization
Reduced system throughput.
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 29/34
Deadlock Avoidance Requires that the system has some additional a priori
Information available .(Each request requires the
information of the resources currently available, theresources currently allocated to each process,future
request and release of each process etc…)
Simplest and most useful model requires that each process
declare the maximum number of resources of each typethat it may need
The deadlock-avoidance algorithm dynamically examinesthe 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 maximumdemands of the processes
29
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 30/34
Safe State
A state is a safe state if the system can
allocate resources to each process in someorder and still avoid a deadlock.
If the system is in safe state only if there exists
a safe sequence. When a process requests an available
resource, system must decide if immediate
allocation leaves the system in a safe state
30
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 31/34
31
Example 3 processes and 12 CD drives
At time t0 the system is in safe state <P1,P0,P2> System will go to unsafe state if process P2 request and allocated one
more resource.
P2 9 3
Avoidance algorithm ensures that the system will never deadlock.
Initially the system is in safe state if a process is requesting a resource,the resource is granted only if the allocation leaves the system in safestate.
In this scheme if a process request a resource that is currentlyavailable, it may still have to wait. So resource utilization can be lower.
Max. Needs Currently holding
P0 10 5
P1 4 2
P2 9 2
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 32/34
System is in safe state if there exists a sequence <P 1, P 2 ,…, P n > of ALL the processes is the systems such that foreach Pi, the resources that Pi can still request can be
satisfied by currently available resources + resources heldby all the P j, with j < i
That is:
If Pi resource needs are not immediately available, then
Pi can wait until all Pj have finished When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate
When Pi terminates, Pi +1 can obtain its needed
resources, and so on If no such sequence exists, then the system state is said
to be unsafe.
32
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 33/34
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 willnever enter an unsafe state.
33
8/3/2019 Lect9 Deadlocks
http://slidepdf.com/reader/full/lect9-deadlocks 34/34
Safe, Unsafe , Deadlock State
34