Safety Algorithm example:
Beginning state:
A has 10 instances
B has 5 instances
C has07 instances
This algorithm works by comparing what resources the process needs to finish to what resources are currently available. For each process that can complete; the resources currently allocated to that process are added to the Resources Available vector and the processes entry in the Finish vector updated to True. If all entries in the Finish vector are true at completion of the algorithm then the system is in a safe state.
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations | Resource after operation | Finish vector | |||||||||||||||||||
| i = 0 |
|
|
|||||||||||||||||||
| i = 1 p1 needs (1, 2, 2); have (3, 3, 2); finish[i] = T; have = (3, 3, 2) + (2, 0, 0) = (5, 3, 2) i++ |
Finish Order = p1 |
|
|||||||||||||||||||
| i = 2 p2 needs (6, 0, 0); have (5, 3, 2); finish[i] = F; i++ |
FO = p1 |
|
|||||||||||||||||||
| i = 3 p3 needs (0, 1, 1); have (5, 3, 2); finish[i] = T; have = (5, 3, 2) + (2, 1, 1) = (7, 4, 3); i++ |
FO = p1, p3 |
|
|||||||||||||||||||
| i = 4 p4 needs (4, 3, 1); have (7, 4, 3); finish[i] = T; have = (7, 4, 3) + (0, 0, 2) = (7, 4, 5) i++ |
FO = p1, p3, p4 |
|
|||||||||||||||||||
| (note (i = 4) == (highest process number) so i = 0) i = 0 p0 needs (7, 4, 3); have (7, 4, 5); finish[i] = T; have = (7, 4, 5) + (0, 1, 0) = (7, 5, 5) i++ |
FO = p1, p3, p4, p0 |
|
|||||||||||||||||||
| i=1 finish[i] == T; i++ |
FO = p1, p3, p4, p0 |
|
|||||||||||||||||||
| i=2 p2 needs (6, 0, 0); have (7, 5, 5); finish[i] = T; have = (7, 5, 5) + (3, 0, 2) = (10, 5, 7) i++ |
FO = p1, p3, p4, p0, p2 |
|
|||||||||||||||||||
| i=3 finish[i] == t; i++ i=4 finish[i] == t; |
FO = p1, p3, p4, p0, p2 |
|
|||||||||||||||||||
The finish order = p1, p3, p4, p0, p2 and our finish table shows all process finished (finish[x] = T). Note also that our finishing resource count (FR = (10, 5, 7)) equals our starting resource count (SR = (10, 5, 7)); a useful cross check, because if all processes have completed then all resources have been released.
| Process | Resource alloc | Resource Max | Available | Needed resource | ||||||||
| A | B | C | A | B | C | A | B | C | A | B | C | |
| p0 | 0 | 1 | 0 | 7 | 5 | 3 | 3 | 3 | 2 | 7 | 4 | 3 |
| p1 | 2 | 0 | 0 | 3 | 2 | 2 | 1 | 2 | 2 | |||
| p2 | 3 | 0 | 2 | 9 | 0 | 2 | 6 | 0 | 0 | |||
| p3 | 2 | 1 | 1 | 2 | 2 | 2 | 0 | 1 | 1 | |||
| p4 | 0 | 0 | 2 | 4 | 3 | 3 | 4 | 3 | 1 | |||
| Process | Resource alloc | Resource Max | Available | Needed resource | ||||||||
| A | B | C | A | B | C | A | B | C | A | B | C | |
| p0 | 0 | 1 | 0 | 7 | 5 | 3 | 2 | 3 | 0 | 7 | 4 | 3 |
| p1 | 3 | 0 | 2 | 3 | 2 | 2 | 0 | 2 | 0 | |||
| p2 | 3 | 0 | 2 | 9 | 0 | 2 | 6 | 0 | 0 | |||
| p3 | 2 | 1 | 1 | 2 | 2 | 2 | 0 | 1 | 1 | |||
| p4 | 0 | 0 | 2 | 4 | 3 | 3 | 4 | 3 | 1 | |||
Deadlock detection:
resource A has 7 instances| Process | Resource alloc | Resource request | Resource Available | ||||||
| A | B | C | A | B | C | A | B | C | |
| p0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| p1 | 2 | 0 | 0 | 2 | 0 | 2 | |||
| p2 | 3 | 0 | 3 | 0 | 0 | 0 | |||
| p3 | 2 | 1 | 1 | 1 | 0 | 0 | |||
| p4 | 0 | 0 | 2 | 0 | 0 | 2 | |||
| Operations | Resources after operation | Process vector | |||||||||||||||||||
i = 0; |
FO = p0 |
|
|||||||||||||||||||
i =1; |
FO = p0 |
|
|||||||||||||||||||
| i = 2; p2 req(0, 0, 0) <= avail(0, 1, 0); finish[i] = T; avail(0, 1, 0) + alloc[i](3, 0, 3) == avail (3, 1, 3); i++ |
FO = p0, p2 |
|
|||||||||||||||||||
| i = 3; p3 req(1, 0, 0) <= avail(3, 1, 3); finish[i] = T; avail(3, 1, 3) + alloc[i](2, 1, 1) = avail(5, 2, 4); i++ |
FO = p0, p2, p3 |
|
|||||||||||||||||||
| i = 4; p4 req(0, 0, 2) <= avail(5, 2, 4); finish[i] = T; avail(5, 2, 4) + alloc[i](0, 0, 2) = avail(5, 2, 6); i++ |
FO = p0, p2, p3, p4 |
|
|||||||||||||||||||
| i = 0 ... i = 1; p1 req(2, 0, 2) <= avail(5, 2, 6); finish[i] = T; avail(5, 2, 6) + alloc[i] (2, 0, 0) = avail(7, 2, 6); i++ |
FO = p0, p2, p3, p4, p1 |
|
|||||||||||||||||||