Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
What is deadlock?
• Deadlock is the permanent blocking of a set of processes thateither compete for system resources or communicate witheach other.
• A set of processes is deadlocked if each process in the set iswaiting for an event that only another process in the set cancause.
c b
d a4 4
1
1
3
32 2
Resources
Resources are a general name given to concrete objects that canbe granted to individual processes, e.g., hardware devices (a tapedrive for example), files, semaphores, etc.
• A preemptable resource is one that can be taken away fromthe process owning it with no ill e�ects. Memory is anexample of a preemptable resource (take away memory from aprocess means swap the process out).
• A nonpreemptable resource, in contrast, is one that cannot betaken away from its current owner without causing thecomputation to fail. For example, burning a CD-ROM isnonpreemptable.
In general, deadlocks involve nonpreemptable resources. Thus ourtreatment will focus on nonpreemptable resources.
The dining philosophers problem
Another classic interprocess communication (IPC) problem:1 Several (usually 5) silent philosophers sit at a table around a
bowl of spaghetti. Their activities consist of thinking andeating alternatively.
2 A fork is placed between each pair of adjacent philosophers. Aphilosopher can only eat while holding both the fork to theleft and the fork to the right. This implies a philosopher canpick up a second fork while holding the first one.
3 Eating is not limited by the amount of spaghetti left: assumean infinite supply.
4 The major constraint in the problem is that forks must bepicked up and put down one by one.
The problem is to design a set of dining rules (i.e., a concurrentalgorithm) such that each philosopher won’t starve.
A solution that has deadlock potential
1 semaphore fork[5]={1,1,1,1,1};
2
3 philosopher(int i) {
4 Loop {
5 think();
6 wait(fork[i]);
7 wait(fork[(i+1) mod 5]);
8 eat();
9 signal(fork[(i+1) mod 5]);
10 signal(fork[i]);
11 }
12 }
P3
P0
P2
P4
P1
How a deadlock is possible in this solution?
The Co�man conditions for deadlock
Co�man et al. (1971) showed that four conditions must hold forthere to be a deadlock:
1 Mutual exclusion condition: Each resource is either currentlyassigned to exactly one process or is available.
2 Hold and wait condition: Processes currently holdingresources that were granted earlier can request new resources.
3 No preemption condition: Resources previously grantedcannot be forcibly taken away from a process. They must beexplicitly released by the process holding them.
4 Circular wait condition: There must be a circular chain of twoor more processes, each of which is waiting for a resource heldby the next member of the chain.
Resource allocation graphs
P1 Ra
Resouce is requested
P1 Ra
Resource is held
Requests Held by
Resource allocation graphs
P1 P2
Rb
Ra
Requests
Requests
Held by
Held by
Circular wait
P1 P2
Rb
Ra
Requests
Requests
Held by
Held by
No deadlock
Resource allocation graphs
p1 p2 p3 p4 p5
1. p1 requests left fork
2. p2 requests left fork
3. p3 requests left fork
4. p4 requests left fork
5. p5 requests left fork
6. p1 requests right fork
7. p2 requests right fork
8. p3 requests right fork
9. p4 requests right fork
10.p5 requests right fork
p1 p2 p3 p4 p5
How to deal with deadlocks?
1 Just ignore the problem altogether. Maybe if you ignore it, itwill ignore you.
2 Prevention, by structurally negating one of the four conditionsnecessary to cause a deadlock.
3 Dynamic avoidance by careful resource allocation.4 Detection and recovery, let deadlocks occur, detect them, and
take actions.
The ostrich algorithm
The ostrich algorithm: stick your head in the sand and pretendthere is no problem at all.
• Theorists: Deadlocks are completely unacceptable and mustbe 100% eliminated.
• Engineers: How often deadlock is expected to cause problem,and how serious it is? How often the system crashes for otherreasons? What if deadlocks occur on the average once everyfive years, but system crashes due to hardware failures and OSbugs occur once a week?
Eliminating deadlocks completely is extremely costly, if notimpossible, and will often place inconvenient restrictions on thesystem. Most OS, including Unix and Windows, just ignore theproblem on the assumption that most users would prefer anoccasional deadlock to a rule restricting all users to one process,one open file, and one of everything.
Deadlock prevention
This strategy seeks to impose suitable restrictions on processes sothat deadlocks are structurally impossible.
The four Co�man conditions provide a clue to some possiblesolutions.
Attacking the mutual exclusion condition
If no resources were ever assigned exclusively to a single process,we would never have deadlocks. For example, we can spool printeroutput so that only one printer daemon requests the physicalprinter. However:
1 Not all devices can be spooled.2 Competition for spooling bu�er can itself lead to deadlock.
Conclusion: generally not feasible.
Attacking the hold and wait condition
1 All processes request all their resources together in a singlestep.
2 When requesting a resource, a process first temporarily releaseall the resources it currently holds, then it tries to geteverything it needs all at once.
However:1 One may not know the type and total amount of resources it
will need at the beginning.2 Resource usage is not optimal as multiple resources may be
unnecessarily tied up together for a long time.
Conclusion: too restrictive and non-optimal performance.
Attacking the no preemption condition
Always allow the OS to preempt resources from low priorityprocesses.
This could be costly and could have ill e�ects: e.g., forcibly takingaway the CD-ROM from a process in the middle of CD burning istricky at best and impossible at worst.
Conclusion: the cost is too high.
Attacking the circular wait condition
A possible way to remove circular wait is to provide a linear orderfor all the resources. All resource requests must follow the definedorder.
In other words, if resource i ranks before resource j , then whenmaking requests for both of them, a process must request i first,followed by j .
However it may be impossible to find an ordering that satisfieseveryone. Also, dynamic resource request is hard (what if p1 has r1and r5, and wants r3 dynamically, which cannot be predicted, thenp1 needs to release r5 and requests r3 and then r5).
Conclusion: may not be feasible and non-optimal performance.
Breaking circular wait in the dining philosophers solution
1 Remove one philosopher from the dining room (use anothersemaphore to only allow at most 4 philosophers in the diningroom):
p1 p2 p3 p5
2 Define a linear order for all the forks and make requestsfollowing the order: first get the low-ranked fork and then thehigh-ranked fork:
p1 p2 p3 p5p4
< < < <
Deadlock avoidanceMake dynamic resource allocation decision carefully so that nodeadlock is possible. The following definitions are used:
1 R: a vector denotes the total amount of resources in thesystem for each type, e.g., R = (R1, R2, . . . , Rm)= (3, 15, . . . , 8).
2 V : a vector denotes the currently available resources for eachtype.
3 C : a matrix denotes the requirement of each type of resourceby each process, e.g., Cij denotes the requirement of process ifor resource j . This must be declared in advance by eachprocess. E.g., qm
j=1 Cij denotes the total resource requirementof process i , and qn
i=1 Cij denotes the total requirement ofresource i from all processes.
4 A: a matrix denotes the current allocation of resources toprocesses (similar to C).
Thus C ≠ A is a matrix represents the current need of each type ofresource from each process.
An example initial state
R1 R2 R3 R1 R2 R3 R1 R2 R3P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 6 1 3 P2 6 1 2 P2 0 0 1
P3 3 1 4 P3 2 1 1 P3 1 0 3P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R39 3 6 0 1 1Resource vector R Available vector V
Deny process initiation
The following conditions must hold:1 Rj = Vj +
qni=1 Aij : total amount equals available ones plus
used ones.2 Cij Æ Rj : one cannot claim more than available.3 Aij Æ Cij : allocation cannot exceed claimed.
Deny a process (pn+1) initiation entirely if Rj < Cn+1,j +qn
i=1 Cij ,for all j . This is to check whether enough resources are available.
Deny resource request
Once a process makes request for a resource, use banker’s
algorithm to determine if such a request shall be granted or not.
Safe state: a state (R, V , C , A) in which there exists at least onesequence of resource allocation that does not result in deadlock.
A process pi can run to its completion if all its resource requestscan be satisfied, i.e., Cij ≠ Aij Æ Vij , for all j (need does not exceedavailable). After pi completes, it will release all its allocatedresources.
Determining if a state is safe amounts to finding a possibleexecution in which all processes can complete.
Example run of banker’s algorithm
R1 R2 R3 R1 R2 R3 R1 R2 R3P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 6 1 3 P2 6 1 2 P2 0 0 1
P3 3 1 4 P3 2 1 1 P3 1 0 3P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R39 3 6 0 1 1Resource vector R Available vector V
Is this a safe state?
Example run of banker’s algorithm
R1 R2 R3 R1 R2 R3 R1 R2 R3P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 0 0 0 P2 0 0 0 P2 0 0 0P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R39 3 6 6 2 3Resource vector R Available vector V
p2 runs to completion
Example run of banker’s algorithm
R1 R2 R3 R1 R2 R3 R1 R2 R3P1 0 0 0 P1 0 0 0 P1 0 0 0P2 0 0 0 P2 0 0 0 P2 0 0 0P3 3 1 4 P3 2 1 1 P3 1 0 3P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R39 3 6 7 2 3Resource vector R Available vector V
p1 runs to completion
Example run of banker’s algorithm
R1 R2 R3 R1 R2 R3 R1 R2 R3P1 0 0 0 P1 0 0 0 P1 0 0 0P2 0 0 0 P2 0 0 0 P2 0 0 0P3 0 0 0 P3 0 0 0 P3 0 0 0P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R39 3 6 9 3 4Resource vector R Available vector V
p3 runs to completion, p4 can also complete
Thus it is a safe state
Example run of banker’s algorithm
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 6 1 3 P2 5 1 1 P2 1 0 2
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R3
9 3 6 1 1 2
Resource vector R Available vector V
Now suppose p1 requests 1 unit each of R1 and R3, should wegrant the request?
Example run of banker’s algorithm
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 2 0 1 P1 1 2 1
P2 6 1 3 P2 5 1 1 P2 1 0 2
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R3
9 3 6 0 1 1
Resource vector R Available vector V
Suppose the request is granted, is this state safe?
Example run of banker’s algorithm
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 2 0 1 P1 1 2 1
P2 6 1 3 P2 5 1 1 P2 1 0 2
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C – A
R1 R2 R3 R1 R2 R3
9 3 6 0 1 1
Resource vector R Available vector V
We cannot find a successful execution sequence to finish allprocesses, thus the state is unsafe and we should deny the requestfrom p1.
Some comments about deadlock avoidance
1 An unsafe state is NOT a deadlock state, it merely has thepotential to deadlock. Thus the strategies presented areconservative.
2 Processes are assumed to be independent.3 The maximum resource requirement must be known in
advance; there must be fixed number of resources; therequirement from each process must be stated in advance.These requirements are too restrictive in real applications.
Deadlock detection
Deadlock detection: always grant resources requests, the OSperiodically performs check to detect if there is deadlock and takesactions in case of yes.
Q: a matrix denotes the process requests for each type of resource,e.g., Qij represents the request by process i for resource j .A possible detection algorithm:
1 Mark each process that has all 0 in A.2 Let W = V .3 Find unmarked process pi such that Qij Æ Wj , for all j . If no
such process can be found, then there is a deadlock.4 If found, mark pi , let Wk = Wk + Aik for all k, then goto 3
Example run of the detection algorithm
R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1
P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector
P3 0 0 0 0 1 P3 0 0 0 1 0
P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5
Request matrix Q Allocation matrix A 0 0 0 0 1
Available vector
Mark p4 (since A4j = 0, for all j).
Example run of the detection algorithm
R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1
P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector
P3 0 0 0 0 1 P3 0 0 0 1 0
P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5
Request matrix Q Allocation matrix A 0 0 0 0 1
Available vector
W = (0, 0, 0, 0, 1).
Example run of the detection algorithm
R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1
P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector
P3 0 0 0 0 1 P3 0 0 0 1 0
P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5
Request matrix Q Allocation matrix A 0 0 0 0 1
Available vector
Mark p3 (since Q3j Æ Wj , for all j).W = W + (0, 0, 0, 1, 0) = (0, 0, 0, 1, 1).
Example run of the detection algorithm
R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1
P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector
P3 0 0 0 0 1 P3 0 0 0 1 0
P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5
Request matrix Q Allocation matrix A 0 0 0 0 1
Available vector
Terminate (since no one else can be marked), deadlock detected!
What to do if deadlock is detected?
1 Abort all deadlocked processes.2 Restart (may deadlock again).3 Successively abort deadlocked processes (can be based on
process priority).4 Successively preempt resources (can be based on priority).