Professional Documents
Culture Documents
Deadlock: - Examples: Traffic Jam
Deadlock: - Examples: Traffic Jam
Deadlock
Examples: Traffic Jam : Dining Philosophers Device allocation
process 1 requests tape drive 1 & gets it process 2 requests tape drive 2 & gets it process 1 requests tape drive 2 but is blocked
Semaphores :
P(s) P(t)
P(t) P(s)
recovery ?
2
Resource:
used by a single process at a single point in time any one of the same type can be allocated
Pre-emptible:
can be taken away from a process without ill effect no deadlocks with pre-emptible resources
Non-Pre-emptible:
cannot be taken away without problems most resources like this deadlock possible
3
Definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause
Necessary conditions for deadlock : Mutual Exclusion : each resource is either currently assigned to one process or is available to be assigned Hold and wait : processes currently holding resources granted earlier can request new resources Non-Pre-emption : resources previously granted cannot arbitrarily be taken away from a process; they must be explicitly released by the process Circular wait : there must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain
Deadlock :
Possible Strategies :
Ignore - the Ostrich or Head-in-the-Sand algorithm
try to reduce chance of deadlock as far as reasonable
Deadlock Prevention
negate one of the necessary conditions
solution?
make resources concurrently sharable wherever possible e.g. read-only access most resources inherently not sharable!
10
11
12
inefficient - not all resources needed all the time processes probably will not know in advance what resources they will need may have to wait excessive time to get all resources at once - starvation
- high priority processes may cause starvation of low priority processes
13
process could release existing resources it holds if it fails to get a new resource immediately
try again later a form of two phase locking used in databases
whenever a new resource is needed, the process always releases its existing resources and asks for all of them at once example: a process which copies a file from tape to disc, sorts the file on disc, and then prints the results on a printer
could request all three resources at the start
- wasteful of printer first, then tape drive later
could initially request tape and disc together, do the read and sort, then release both and finally request disc and printer together to do the printing
- more efficient - must ensure data stays intact on disc between phases
14
negating Non-Pre-emption
difficult to achieve in practice
cannot take a printer away from a process in the middle of printing cannot take a semaphore away from a process arbitrarily
- might be in the middle of updating a shared area
occasionally possible :
processes resident in main memory some deadlock occurs such as failure to allocate a page one or more processes can be swapped out to disc to release their pages and allow remaining processes to continue
- as long as they release any other resources they also hold on the way out
15
example:
1: tape drive
2: disc drive
3: printer 4: plotter 5: typesetter
example: semaphores
semaphores identified by number claimed in numerical order
16
Deadlock Avoidance
deadlock possible but avoided by careful allocation of resources avoid entering unsafe states
a state is safe if it is not deadlocked and there is a way to satisfy all requests currently pending by running the processes in some order need to know all future requests of processes
17
18
when the future request is actually made, convert this to an allocation arc
process closest to its limit is the checked, and the steps repeated
if all processes can eventually run to completion, state is safe
20
Multiple resources
m types of resource, n processes
vector comparison :
A B if Ai Bi for 0 i m
21
look for a row in R A i.e. a process whose requests can be met if no such row exists, state is unsafe add this row of R into the same row of C and subtract it from A i.e. notionally allocate the resources to the process add this row of C back into A and set the row of C to zero i.e. the process notionally completes and returns its resources repeat these steps until all C is all zero i.e. all processes notionally finished (initial state is safe) or until a suitable row in R cannot be found (unsafe)
22
the algorithm assumes processes will return their resources within a reasonable time
processes may only get their resources after an arbitrarily long delay practical use is therefore rare!
23
24
4. 5.
From given node, if there are any unmarked outgoing arcs, goto 5 else goto 6 Pick an unmarked outgoing arc and mark it
follow it to new current node and goto 3
6.
algorithm terminates
25
26
27
2 hops :
0 0 1 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 1 0
3 hops : 0 1 0 0 0 0 1 0 1 0 0 0 1 1 0 0
0 0 1 0
1 0 0 0
0 1 0 0
0 1 1 0
4 hops ?
> 4 hops ?
for (y=0; y<N; y++) { for (x=1; x<N; x++) { if ( A[x,y] ) { for (z=1; z<N; z++) { if ( A[y,z] ) A[x.z] = true; } } } }
29
30
Multiple resources
apply equivalent of Bankers algorithm using current resource requests any processes unsatisfied are deadlocked
31
Process Termination
drastic ultimate solution abort all processes involved in the deadlock
all resources returned for re-use