Chapter7 DeadLocks
Chapter7 DeadLocks
Chapter7 DeadLocks
DeadLocks
Objectives
• Process
Pi
• Pi requests instance of Rj
Rj
• Pi is holding an instance of Rj Pi
Rj
Resource Allocation Graph With A Deadlock
There is a cycle
and
Deadlock
Graph With A Cycle But No Deadlock
There is a cycle
but
No Deadlock
Basic Facts
Ignore the problem and pretend that deadlocks never occur in the system;
Used by most operating systems, including UNIX
OS does not bother with deadlocks that can occur in applications
Deadlock Prevention
Mutual Exclusion – not required for sharable resources; must hold for nonsharable
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
Deadlock Prevention (Cont.)
No Preemption –
A processing holding resources make requests: if request cannot be granted, release
(preempt) the held resources, and try again later.
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
Deadlock Prevention (Cont.)
All resources are ordered and assigned an integer number
A process can request resources in increasing order of enumeration
Resources
R1 R2 R3 R4 R5
Example:
Process 1 Process 2 Process 3
Request R2 Request R1 Request R3
Request R4 Request R2 Request R4
Request R3
Proof
Consider the resources that are allocated at the moment. Consider the process that has the
highest numbered allocated resource.
That process will not block; will be able to continue and finish. Because:
Itcan not make a request to a resource with a smaller number and get block. This will
not happen.
Itcan make a request to a resource with a larger number. That resource is not allocated
yet (otherwise that would be the highest numbered allocated resource). Hence the
process will get the resource immediately. In this way, that process will not block. Will
be able to run and complete.
Then,the same thing will be applicable to the process that is holding the next highest
numbered resource. That will be able to run an finish.
All process may run and finish sooner or later.
Deadlock Avoidance
Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need
to hold simultaneously. (maximum demand)
Resource-allocation state is defined by the number of available and allocated resources, and
the maximum demands of the processes
Safe state
When a process requests an available resource, system must decide if immediate allocation
leaves the system in a safe state
A state is safe if the system can allocate resources to each process (up to its maximum) in some
order and still avoid a deadlock.
We are considering a worst-case situation here. Even in the worst case (process requests up their
maximum at the moment), we don’t have deadlock in a safe state.
Safe state
More formally: A system state is safe if there exists a safe sequence of all processes (<P1, P2,
…, Pn>) 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
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.
Basic Facts
If a system is in safe state no deadlocks
When a request is done by a process for some resource(s): check before allocating
resource(s); if it will leave the system in an unsafe state, then do not allocate the
resource(s); process is waited and resources are not allocated to that process.
Safe, Unsafe , Deadlock State
Avoidance Algorithms
Claim edge Pi Rj indicates that process Pi may request resource Rj; represented by a
dashed line
The request can be granted only if converting the request edge to an assignment edge does
not result in the formation of a cycle in the resource allocation graph.
Data Structures for the Banker’s Algorithm
Let n = number of processes, and
m = number of resources types.
Available: Vector of length m. If Available[j] == k, there are k instances of resource type Rj
at the time deadlock avoidance algorithms is run.
V == Xi ?
V <= Xi ?
V Xi <= V ?
ABC ….
V is a vector; V = [3 3 2]
332
Ex: Compare [3 3 2] with [2 2 1]
[2 2 1] <= [3 3 2]
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available (initialize Work temporary vector)
Finish [i] = false for i = 0, 1, …, n-1
(Work is a temporary vector initialized to the Available (i.e., free) resources at that time when the safety
check is performed)
Allocation Need
2. Find an i such that both: ABC ABC
(a) Finish [i] = false
P0 010 P0 743
(b) Needi Work
P1 200 P1 122
If no such i exists, go to step 4
P2 302 P2 600
3. Work = Work + Allocationi
Finish[i] = true P3 211 P3 011
go to step 2
P4 002 P4 431
4. If Finish [i] == true for all i, then the system state is safe; o.w. unsafe.
Resource-Request Algorithm
for Process Pi
Algorithm
Is it a safe state?
Example of Banker’s Algorithm
new state (we did not go to that state yet; we are just checking)
Example: P1 requests (1,0,2)
No process has a row in Need matrix that is less than or equal to Available.
Therefore, the new state would be UNSAFE. Hence we should not go
to the new state. The request is not granted. P0 is waited.