Sie sind auf Seite 1von 5

Necessary Conditions for Deadlock

Mutual Exclusion
Shared resources are acquired and used mutually
exclusively
Deadlock Hold and Wait
Each process continues to hold resources already allocated
to it while waiting to acquire new resources
No Preemption
Resources granted to a process can only be released back to
the system after the process finished using it
Circular Waiting
Processes forms a chain in the wait for graph

Deadlock Wait-For Graph

If a group of processes are blocked on their resource Specify the relationship among processes and
requests that can never be satisfied, then it is a resources
deadlock A directed edge from a process to a resource represents that
the process is requesting for the resource
A directed edge from a resource to a process represents that
the resource is held by the process

P5 F1 P1 F2 P2

F5 P4 F4 P3 F3

Dining Philosophers Problem Deadlock Solutions

5 philosophers, each needs to eat with 2 forks Deadlock Prevention


Philosopher i: Disallow at least one of the four necessary conditions
repeat Deadlock Avoidance
think;
f1 = i; Grant only those requests for available resources which
cannot possibly result in deadlock
f2 = (i +1) mod 5;
wait (fork[f1]);
Deadlock Detection
wait (fork[f2]); Grant available resources to requesting processes freely and
eat; periodically check for deadlocks
signal (fork[f1]); After deadlock is detected, proceed to deadlock recovery
signal (fork[f2]);
until false;

1
Deadlock Prevention Deadlock Avoidance
Disallow at least one of the four necessary conditions System grants allocations of resources only if it is
Avoid Mutual Exclusion guaranteed to be deadlock free
Nothing can be done if the resource cannot be shared For a new request requesting for resources
Allow Preemption System pretends to grant the resources
Not all resources can be preempted (e.g., printer) Check whether the new state is safe
Grant the requested resources only if it is safe
Avoid Circular Wait
Safe
Linear ordering on resources
In the future, the system can allocate resources in at least one order
E.g., resources A, B, C, order(A) = 1, order(B) = 2, order(C) = 3 that guarantees free of deadlock
request (C and A) request (A); request (C); Unsafe
Problems: system may (still may not) end up in deadlock situation
Require resource requirement information in advance Problem
Low resource utilization
Need to know resource requirements for all processes in advance
Degradation of concurrency (resources are allocated before it is needed)

Deadlock Prevention Bankers Algorithm

Dinning philosopher problem System Model


N: Number of processes
Philosopher i:
repeat M: Number of different types of resources
think; Input: Request[i,j]
f1 = i; f2 = (i +1) mod 5; 1 i N, 1 j M
if (f1 > f2) switch(f1, f2); Number of type j resources being requested by process i
wait (fork[f1]); Output: grant or deny Request[i,j]
wait (fork[f2]);
eat;
signal (fork[f1]);
signal (fork[f2]);
until false;

Deadlock Prevention Bankers Algorithm


Disallow at least one of the four necessary conditions Information to be maintained
Avoid Hold-and-Wait Total[M]
Solution 1: A process has to get all of its resources before it begins Total[j]: Total number of available resources of type j
execution, but the resources may be released at any time, any order
MaxReq[N, M]
Problems
MaxReq[i,j]: Maximum number of type j resources that may be
Require resource requirement information in advance
requested by process i
Low resource utilization
Degradation of concurrency (worse than linear ordering) Allocated[N, M]
Solution 2: A process that has to wait for some resources Allocated[i,j]: Number of type j resources that are currently
Will release all the resources allocated to it allocated to process i
Resume when all the original and new resources are available
Problems
Not all resources can be preempted
Starvation

2
Bankers Algorithm Bankers Algorithm

Information to be derived Pretend to allocate


Need[N, M] for all j: Available[j] := Available[j] i Request[i,j];
Need[i,j]: Number of type j resources that may still be needed by for all i, j: Allocated[i,j] := Allocated[i,j] + Request[i,j];
process i for all i, j: Need[i,j] := Need[i,j] Request[i,j];
Available[M] Check safety
Available[j]: Number of resources of type j that is still available if current system is safe then
grant the allocation to the request;
else deny the request
Restore original state if request is denied
for all j: Available[j] := Available[j] + i Request[i,j];
for all i, j: Allocated[i,j] := Allocated[i,j] Request[i,j];
for all i, j: Need[i,j] := Need[i,j] + Request[i,j];

Bankers Algorithm Example Bankers Algorithm

Current system state Check safety


Total Max Alloc Need Avail for all j: Temp[j] := Available[j]; -- not to change Available
P1 P2 P3 P1 P2 P3 P1 P2 P3 Pset := the set of all processes;
R1 7 2 7 3 0 2 1 2 5 2 4 repeat
R2 6 3 3 4 1 1 0 2 2 4 4 if there exists an i such that Need[i, j] < = temp[j] then
R3 6 5 1 1 0 1 0 5 0 1 5 { -- process i can complete its execution
remove process i from Pset;
Request for all j: Temp[j] := Temp[j] + Allocated[i,j];
P1 P2 P3
-- pretend process i is done, return all resources
}
R1 0 1 0
R2 1 0 0 else return (unsafe);
R3 0 0 0 until Pset = Empty;
return (safe);

Bankers Algorithm Bankers Algorithm Example 1

Preliminary checks Current system state


Check to see if the request is invalid Total Max Alloc Need Avl
Request
P1 P2 P3 P1 P2 P3 P1 P2 P3
if Request[i,j] > Need[i,j], for any i, j, then P1 P2 P3
R1 7 2 7 3 0 2 1 2 5 2 4 R1 0 1 0
error; R2 1 0 0
R2 6 3 3 4 1 1 0 2 2 4 4
Check to see whether the system has enough resources for R3 6 5 1 1 0 1 0 5 0 1 5 R3 0 0 0
the request no problem
if i Request[i,j] > Available[j], for any j, then Preliminary checks
Request
010
deny the request; if Request[i,j] > Need[i,j], for any i, j 1 0 0 error
002
then error;
Request
if i Request[i,j] > Available[j], for any j 060
1 0 0 deny
then deny; 000

3
Bankers Algorithm Example 1 Bankers Algorithm Example 3

Current system state Current system state


Total Max Alloc Need Avl Request Total Max Alloc Need Avl Request
7 273 021 252 4 010 5 143 010 133 4 010
6 334 110 224 4 100 4 432 020 412 2 011
6 511 010 501 5 000 3 211 200 011 1 000
After pretend to allocate After pretend to allocate
Total Max Alloc Need Avl Total Max Alloc Need Avl
7 273 031 242 3 5 143 020 123 3
6 334 210 124 3 4 432 031 401 0
6 511 010 501 5 3 211 200 011 1

Bankers Algo Example 1 (Check Safety) Bankers Algorithm Example 3

Check safety Check safety


Total Max Alloc Need Avl Total Max Alloc Need Avl
5 143 020 123 3 Which process can continue?
7 273 031 242 3 Which process can continue? Need[i, j] < = Avail[j]
4 333 021 312 1
6 334 210 124 3 Need[i, j] < = Avail[j] P2 can!
P1 can! 3 211 210 001 0
6 511 010 501 5 --------------------------------------------------
Pretend: Pretend:
73 31 42 3 5 0 0 1 3 5
- Allocate resources to P1 - Allocate resources to P2
34 10 24 5 - P1 finishes 4 0 1 3 2 3 - P2 finishes
3 2 0 0 1 1 - P2 returns all its resources
11 10 01 5 - P1 returns all its resources
--------------------------------------------------
7 3 4 4 P3 can continue! Both P1 and P3 can continue!
5 0 0 1 3 3
3 1 2 5 P2 can continue! Choose P1 to continue!
4 0 1 4 2 1
All finishes Safe! All finishes Safe!
1 1 0 5 Grant request!
3 2 0 0 2 0
Grant request!

Bankers Algorithm Example 2 Bankers Algorithm Example 4

Current system state Current system state


Total Max Alloc Need Avl Request Total Max Alloc Need Avl Request
7 273 021 252 4 010 5 143 010 133 4 010
6 334 110 224 4 100 4 432 011 422 2 010
6 511 010 501 5 001 3 212 200 012 1 010
After pretend to allocate After pretend to allocate
Total Max Alloc Need Avl Total Max Alloc Need Avl Which process can continue?
Which process can continue? Need[i, j] < = Avail[j]
7 273 031 242 3 Need[i, j] < = Avail[j] 5 143 020 123 35
6 334 210 124 3 4 432 021 412 13 P2 can return all
6 511 011 500 4 None of them can!!!! 3 212 210 002 01
Deny request None of the rest can
Deny request

4
Deadlock Detection Comparisons

Just let deadlock happen Overhead in deadlock prevention (linear ordering)


No prevention, no avoidance Take resources in advance Lower resources utilization; Reduce
degree of concurrency
System periodically checks for deadlock Overhead in deadlock avoidance
When a deadlock is detected, a recovery process is required Need to update resource information for every allocation/deallocation
to break the cycle Need to run safety algorithm for each request (very expensive)
Use the safety check algorithm for deadlock detection Overhead in deadlock detection
No need to know maximum resource requirements Max Detection algorithm can be expensive
No need to know still needed resources Needed but only when there is a potential deadlock
Need to wait for resources till a deadlock is detected
deadlock recovery
No perfect solution -- Consider combinations
Kill one process at a time till deadlock cycle is broken

Safety Algorithm for Detection Readings


Check safety Section 6.2-6.4
Total Alloc Avl Req
P1 P2 P3 P1 P2 P3
R1 3 0 2 0 1 2 0 1 P2 can get the resources
R2 4 0 1 1 2 0 2 1 P2 return resources
R3 2 1 0 0 1 0 1 2
-----------------------------------------------------
R1 3 0 0 3 2 1 P1 can get the resources
R2 4 0 1 3 0 1 P1 return resources
R3 2 1 0 1 0 2
-----------------------------------------------------
R1 3 0 3 1 P3 can get the resources
R2 4 1 3 1 All finishes Safe!
R3 2 0 2 2 No deadlock!

Safety Algorithm for Detection

Check safety
Total Alloc Avl Req
P1 P2 P3 P1 P2 P3
R1 2 0 0 1 1 2 0 1 P can get the resources
2
R2 4 0 1 1 2 0 2 1 P2 return resources
R3 2 1 0 0 1 0 1 2
-----------------------------------------------------
R1 2 0 1 1 2 1 No process can get
R2 4 0 1 3 0 1 the resources
R3 2 1 0 1 0 2 Deadlock
----------------------------------------------------- P1 and P3 involved

Das könnte Ihnen auch gefallen