principles of operating systems · principles of operating systems tutorial : deadlock !! cpsc 457,...
TRANSCRIPT
PRINCIPLES OF OPERATING SYSTEMS
Tutorial : Deadlock
CPSC 457, Spring 2015 June 10/11, 2015
Department of Computer Science, University of Calgary
Deadlock Avoidance § Safe and Unsafe States § Banker’s Algorithm
2
Safe State § A system is in a safe state … § Only if there exists a safe sequence <P1, P2, ..., Pn> of all the processes in the systems such that …
§ For each Pi, the resources that Pi can sAll request can be saAsfied by current available resources + resources held by all processes Pj, with j < i.
§ That is: § If the resources that Pi needs are not immediately available, then Pi can wait unAl all the Pj, with j < i, have finished.
§ When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.
§ When Pi terminates, Pi+1 can obtain the resources, and so on.
§ If no such sequence exists, then the system state is unsafe. 3
Banker’s Algorithm § To determine whether a request can be safely granted. § Resource-‐request algorithm § Safety algorithm
§ Resource-‐request algorithm § If request > needed, return error. § If request > current available, return wait. § Pretend the allocaAon: update Available, AllocaConi, Needi § Run Safety algorithm § If safe state, grant the request. § Otherwise, wait and restore the values of Available, AllocaConi, Needi
4
Banker’s Algorithm
§ Safety algorithm § Update the current available resources § Find a process that can be saAsfied by the current available resource
§ Mark the process finished and pretend to release its resources § Repeat unAl no more process can be found § If all processes finish, return safe. Otherwise, return unsafe.
5
Banker’s Example § Processes P0, P1, P2, P3, P4 § Resources A (10 instances), B (5 instances), C (7 instances)
§ At Ame T0:
§ Can the request, , be granted? (by P1)
6
Alloca:on Max Need Available A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2 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
(Request1 = (1, 0, 2))
<= (Need1 = (1, 2, 2)) (Request1 = (1, 0, 2)) <= (Available = (3, 3, 2)) (Request1 = (1, 0, 2)) Preliminary checks
Banker’s Example
7
Alloca:on Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2
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
Pretend the allocaCon: Available = (3, 3, 2) -‐ (1, 0, 2) = (2, 3, 0) Alloca=on1 = (2, 0, 0) + (1, 0, 2) = (3, 0, 2) Need1 = (1, 2, 2) -‐ (1, 0, 2) = (0, 2, 0)
(Request1 = (1, 0, 2))
Alloca:on Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 2 3 0
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
Banker’s Example
8
Alloca:on Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 5 3 2
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
Safety Algorithm
Finish = [false, false, false, false, false]
Work = Available = (2,3,0)
Find an index i such that both
a. Finish[i] == false b. Needi <= Work
i = 1
Work = Work + AllocaConi
= (2,3,0) + (3,0,2)
= (5,3,2)
Finish = [false, true, false, false, false]
Banker’s Example
9
Alloca:on Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 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
Safety Algorithm
Finish = [false, true, false, false, false]
Work = (5,3,2)
Find an index i such that both
a. Finish[i] == false b. Needi <= Work
i = 3
Work = Work + AllocaConi
= (5,3,2) + (2,1,1)
= (7,4,3)
Finish = [false, true, false, true, false]
Banker’s Example
10
Alloca:on Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 7 5 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
Safety Algorithm
finish = [false, true, false, true, false]
Work = (7,4,3)
Find an index i such that both
a. Finish[i] == false b. Needi <= Work
i = 0
Work = Work + AllocaConi
= (7,4,3) + (0,1,0)
= (7,5,3)
finish = [true, true, false, true, false]
Banker’s Example
11
Alloca:on Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 10 5 5
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
Safety Algorithm
finish = [false, true, false, true, false]
Work = (7,5,3)
Find an index i such that both
a. Finish[i] == false b. Needi <= Work
i = 0
Work = Work + AllocaConi
= (7,5,3) + (3,0,2)
= (10,5,5)
finish = [true, true, true, true, false]
Banker’s Example
12
Alloca:on Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 10 5 7
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
Safety Algorithm
finish = [false, true, false, true, false]
Work = (10,5,5)
Find an index i such that both
a. Finish[i] == false b. Needi <= Work
i = 0
Work = Work + AllocaConi
= (10,5,5) + (0,0,2)
= (10,5,7)
finish = [true, true, true, true, true]
Since, all the processes can finish, the state aJer gran:ng the request is a safe state.