Professional Documents
Culture Documents
Deadlock Solved
Deadlock Solved
, we have determined that the system is in a safe state. With 4 instances of the resource available, we could complete any of the three processes; let's say that we complete P, first Now we can free the 2 that P, is holding. With the 6 available, we can complete either P, or P,; let’s compete P, first. Since there is nothing to free we don't change the amount available and we complete P,Banker's Algorithm Example — What If We Allocate A Few More? Alloc | Max. Need unsafe Avy: Let’s allocate 2 instances of our resource to P, and one mote to P,. With only one instance of the resource left, there is no process whose potential need can be met. So we cannot be certain that any will run to completion, The system is in an unsafe state and the allocation was not a prudent move.Banker’s Algorithm Example — 3 Types of Resources Alloc Need ABC ABC 10 00 02 11 02 Let's take a look at a more typical situation. There are three different types of resources, A, B and C. Shown here is their current allocations, the maximum allocations that they may request and what remains available. Our first step is to calculate the needs.Banker’s Algorithm Example - Calculating the Needs Alloc Need | Work ABC ABC|ABC 010 743/332 P, can 00 122 get its need and finish 02 600 11 011 02 431 P, has 0 instances of A, 1 instance of B and 0 instances of C. It has the right to 7 instances of A, 4 instances of B and 3 instances of C. 7 instances of A minus 0 allocated instances of A equals a need of 7 instances of A. 5 instances of B minus 1 allocated instance of B equals a need of 4 instances of B 3 instances of C minus 0 allocated instances of C equals a need of 3 instances of C. ‘We now do the same for processes P,, P,, P;, and P,. When we finish, we see that P, can get everything that it may need, so it will run through completionBanker’s Algorithm Example - After Finishing Process P, ‘Alloc | Max | Need | Work ABC|ABC/ABC/ABC 010/753 (743/532 000 000 | 000 302 600 P;can 211 oO1t get its need 002 431 and finish After we re-allocate the 2 instances of A that PI actually is holding, we have 5 A's, 3.B’s and 2 C’s. With this, we can satisfy P, and it can run through completion.Banker’s Algorithm Example - After Finishing Process P; Alloc Max Need | Work ABC|ABCIABC/ABC 010/753 743/743 000 000 | 000 3.02 600 000 000 P,can get its need Pa O02 | 433 [AS by ane finish Once P3 finishes, we re-allocate its 2 A’s, 1 B and 1 C, making 7A’s, 4 B's and 3 C’s available for re-allocation. Now we can finish P,.Banker’s Algorithm Example - After Finishing Process P, Max Need | Work ABC/|ABC|ABC 753 1743/745 000 | 000 P,can get its need 60 0 and finish 000 000 We can now free up the 2 C’s that belong to P, and and now we have 7 A's, 4 B's and 5C’s, This is enough to allow us to complete P).Banker’s Algorithm Example - After Finishing Process P, Max Need | Work ABC/|ABC|ABC 753 1743]1047 000 | 000 Pycan 000 get its need and finish; 000 000 Lastly, we free up the 3A’s and 2 C’s that complete Py. as. This leaves us with enough toBanker’s Algorithm Example - After Finishing Process Py Work Alloc ABC Max ABC 000 1057 000 The system 000 is safe 000 000 There is a sequence in which we can complete all the processes if they do need their maximum allocation. The system is safe.Banker’s Algorithm Example —- Can We Grant P,’s Request? P, requests Max | Need | Avail | |. 2) Iris less ABCIABC/ABC] J than the B 743] 33 2-+4aailable 2 resources 122 Let's examine a request of process P, for 1 A, 0 B's and 2.C’s. Since this is less than what is available, we have the resources available for the request, but will it leave us in a safe state?Banker’s Algorithm Example — After We Grant P,’s Request A similar Max ‘Avail || execution of our BC A BC]| algorithm 53 23 0 || willfind the sequence 22
02
22
33
Let's pretend to allocate it. This leave us with 2 A’s, 3 B's and 0 C’s available.
We have enough for P, to run to completion, even if it needed its maximum,
allocation. After de-allocating its 3 A's and 2 C's, we would have 5 A's, 3 B’s and
2C’s available.
With this, we could run P, through completion. Then we could free its resources,
which would give us 7 A's, 4B’s and 3 C’s. With this we could easily complete P,,
P, and P,. Thus, this request can be made without placing the system in an unsafe
state,Banker’s Algorithm Example — Can We
Grant P,’s Request?
P, requests
Max | Need | Avail | | 23°
Itis more
ABC/|ABC|ABC than the
753 174312 3 Oy ;mmiladle
resources
322 )020
90 600
22 oll
43 431
If P4 were to request 3 A’s and 3 B's, we would be unable to fulfill the request
because we do not have the resources available - we are short 1 A.Banker’s Algorithm Example — Can We
Grant P,’s Request?
Py requests
Max | Need | Avail | | (2°
Itis less
ABC/|ABC|ABC than the
753 1743] 2 3 O-t+mailable
resources
322 |)020
90 600
22 oll
43 431
Let's see what happens if Py requests 2 B’s. It’s less than the available resources,
But does it place us in an unsafe state?Banker’s Algorithm Example —
Recalculating P,’s Allocation
Alloc | Max | Need | Work
BC/ABCIABC|ABC| | audingto
30/753 /713/200 P
allocation
02 322 )020
02 90 600
11 22 oll
02 43 431
Let's add the 2 B’s to P,’s allocation and recalculate its need and what would be
available,Banker’s Algorithm Example —
Recalculating P,’s Allocation
Alloc | Max | Need | Avail
BC|ABCJABC/ABC| | qheris
30 713 | not enough
200 left for
02 020 anyone's
need;
02 itis not
safe
11
02
The problem is that 2 A’s and 0 B’s or C’s leave us unable to meet any possible
requests by other processes. This request won't be granted because it places us in
an unsafe state.Deadlock Detection
* Ifa system does not provide for deadlock
avoidance or prevention, then it must
provide:
— an algorithm for deadlock detection
— an algorithm for deadlock recovery
‘Some systems don’t avoid or prevent deadlock. What they do is detect it and
recover from it. That means that such systems need an algorithm for each of these
two tasks,
13Detecting Deadlock With Single-Instances of
Each Resource
In the case of single-instance resource, we can use a graphical approach. Let's take
another look at our resource allocation graph. What we will do is simplify this by
redrawing the request edges that currently run from process to resource. Instead,
let's have them point directly to the process holding the requested resource.Detecting Deadlock Corresponding Wait-For
Graph
tos
This is what the graph looks like after the redrawing the request edges. Now we
know at a single glance that P, is waiting for a resource that P, is holding; which
resource is not important. Similarly PI is waiting for a resource that P, is holding
and P, is waiting for a resource that P, is holding. It is fairly straightforward to
write a program that can detect whether there is a circuit like this in such a graph.Detecting Deadlock With Multiple-Instances
of Each Resource
Let Work and Finish be vector of length m and n respectively.
Initialize Work := Available
FORI=1Ton
IF Allocation <> 0 THEN Finish
ELSE Finish{i] = True;
WHILE there exists an T such that
(Finish[i] = False) AND (Request, <= Work) DO
BEGIN
IF such an Texists
THEN BEGIN
Work := Work + Allocation,;
Finishfi] := True
END; { then }
END; ( while }
IF Finish(i) = False then the system is in a deadlock state and Process P,
deadlocked.
This is similar to the Banker's Algorithm that we looked at before. We assume that
certain resources have been allocated and that others have been requested.
We'll look to see if there are any requests that we can fulfill. If that’s the case,
these processes can run through completion and then their resources can be re-
allocated to other processes.
We continue this until there are no more cases like this. If we haven't finished all
our processes, there is a deadlock which includes this process.Detecting No Deadlock — An
Example
Request
ABC
Avail
BC
000
000
202
000
Request, You might also like