Download presentation
Presentation is loading. Please wait.
1
Deadlock & Starvation
2
Deadlock & Starvation Typical a hidden side effect
Total block of one or more threads ”deadlock” Running/runable but not able to carry out the intended duty Starvation Livelock
3
Deadlock,livelock,... Not a problem for OS Fewer task is running
More resources to the tasks still able to run
4
A typical real life example
A road crossing before ”a cut in your driver license”
5
A typical example again ...
”Time” between decission and deadlock situation Red zone seems ok for everybody ”now” We do all enter the crossing I ”meet” the others deadlock !!!
6
A typical example again ...
4 critical zones 1,2 – 2,3 – 3,4 – 4,1 Strategies safe,secure, effective, at same time ?
7
A very basic deadlock Basicly crossing reservations A waits on B
8
A very basic deadlock Basicly crossing reservations A waits on B
B waits on C
9
A very basic deadlock Basicly crossing reservations A waits on B
B waits on C C waits on D
10
A very basic deadlock Basicly crossing reservations A waits on B
B waits on C C waits on D D waits on ... A !
11
A very basic deadlock Basicly crossing reservations A waits on B
B waits on C C waits on D D waits on ... A ! Circular wait lock condition – if timing is ”ok” happens often or not ! Difficult to find in test Difficult to reproduce situations Do you belive your costumer ?
12
”Advanced” version of the basic example:
Task1 and 2 will both enter two regions protected by A and B semaphore... Task Task2 wait(A); wait(B); wait(B); wait(A); ....crit reg crit region.... signal(B); signal(A); signal(A); signal(B);
13
A graphical representation – the bad one
Interleaving: måske – måske ikke
14
graphical– the good one
15
Types of resources Reuseable (use and forward to others)
Consumable (can only be used once) Reuseables memory, I/O, devices, filer,... (no new news) Consumables interrupt, signals, messages, info i IO buffers, printerpaper, disq,... :- )
16
de adlock – onl iff … (Coffmanns rules)
MUTUAL EXCLUSION. A R must only have one holder at time HOLD & WAIT. You must hold one R and try to get one more NO PREEMPTION Nobody can take a R from you CIRCULAR WAIT. A closed loop of R allocations and requests do exist
17
de adlock – onl iff ... MUTUAL EXCLUSION.
A R must only have one holder at time HOLD & WAIT. You must hold one R and try to get one more NO PREEMPTION Nobody can take a R from you CIRCULAR WAIT. A closed loop of R allocations and requests do exist Break on of these and NO DEADLOCK IN CODE !
18
de adlock – onl iff ... MUTUAL EXCLUSION.
A R must only have one holder at time HOLD & WAIT. You must hold one R and try to get one more NO PREEMPTION Nobody can take a R from you CIRCULAR WAIT. A closed loop of R allocations and requests do exist
19
de adlock – only iff ... MUTUAL EXCLUSION.
A R must only have one holder at time HOLD & WAIT. You must hold one R and try to get one more NO PREEMPTION Nobody can take a R from you CIRCULAR WAIT. A closed loop of R allocations and requests do exist
20
de adlock – only iff ... MUTUAL EXCLUSION.
A R must only have one holder at time HOLD & WAIT. You must hold one R and try to get one more NO PREEMPTION Nobody can take a R from you CIRCULAR WAIT. A closed loop of R allocations and requests do exist
21
de adlock – only iff ... MUTUAL EXCLUSION.
A R must only have one holder at time HOLD & WAIT. You must hold one R and try to get one more NO PREEMPTION Nobody can take a R from you CIRCULAR WAIT. A closed loop of R allocations and requests do exist
22
Deadlock avoidance A system when more han one of each R
26
Starvation
27
Dining Philosopher II Many solutions Som not all are effective
The easy one wait(table-sem); eat_alone(); signal(table-sem);
28
Deadlock avoidance Read it !
Basic idea is to hold a thread back if no R is available
29
where? realtime ?? bop bop bop where memory, swap, IO, filsystems
databaser,... and many other places
30
Bankiers Algorithm –never red numbers
31
Deadlock avoidance Comments Purpose is survival
The price is time for execution No (!) real time demands within the rule set
32
Deadlock detection Variant af avoidance Check on the fly
Can be implemented The first exercise today 6.9 (in BW chapter 6)
33
Resource requests Assume that If a process has requested a resource
And the resource is available Then, resource request is immediately granted i.e., typically don’t draw diagrams like: P R Draw as: P R Key: P= process R= Resource 33 33
34
Resource requests Assume that If a process has requested a resource
And the resource is available Then, resource request is immediately granted i.e., typically don’t draw diagrams like: P R Draw as: P R Key: P= process R= Resource 34 34
35
Blocked Processes A process with a request edge will thus necessarily be blocked, and the only blocked processes will be those that have at least one request edge p1 p2 35 35
36
Example 1: Which Processes Are Blocked?
36 36
37
Solution Consider one possible execution sequence:
P0 executes wait(A) P1 executes wait(B) P0 executes wait(B) P1 executes wait(A) P0 A B P1 Why consider this a deadlock? Deadlock conditions will always have a cycle; but a cycle by itself may not indicate a deadlock. Now have a deadlock! Why is this a problem? 37 37
38
MUTEX Dedicated semaphore: mutex_enter(mut); mutex_leave(mut);
mutex_try_enter(mut); no news, but gives operating system a chance to check if use is ”proper”
39
Readers/Writers Database mm Many can read at same time
When writing is to take place all others shall leave the critical region Effective is more readers than writers
40
Readers/Writers reader_enter(xx); laes(); reader_leave(xx);
writer_enter(xx); skriv(); writer_leave(); ”try” variants ... part of POSIX standard
41
posix – a small (!) snapshot
42
og many moreeeeee :-( The important ones threads semafor, mutex
condition variables spinlocks message passing ISR integration
43
For home reading only
44
Deadlock Avoidance Case 2 Multi-unit Resources: Banker’s Algorithm
The algorithm requires the following information: Available: Vector of available resources, per resource type Max: Matrix giving maximum resource usage of each process Allocation: Matrix giving current allocation of resources to processes Need: Max – Allocation (subtract these matrices) Actually two ways that a resource allocation request can fail: 1) if results in an unsafe state, and 2) if there are presently insufficient resources. The matrices and vectors have some representational overlap with what we can represent with the resource allocation diagrams. Summary of Banker’s Algorithm: (a) simulate resource allocation, (b) determine if there is a safe sequence, (c) do allocation only if safe 44 44
45
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi If Requesti [j] == k then process Pi requests k instances of resource type Rj. Needi, Allocationi = row i of Need & Allocation matrices 1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. 2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not available. 3. Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available - Requesti Allocationi = Allocationi + Requesti Needi = Needi – Requesti 4. If the resulting state is safe according to the Safety Algorithm, then Pi allocated resources, otherwise old allocation state restored Needi == row i of Need 45 45
46
Safety algorithm: Determine if a state is safe
1. Let Work and Finish be vectors of length m and n, respectively (n= number of processes; m = number of resources). Initialize: Work = Available Finish [i] = false for i = 1,2,3, …, n. // dealt with process i? 2. Find i such that both: (a) Finish [i] == false (b) Needi ≤ Work If no such i exists, go to step 4. Work = Work + Allocationi // simulate process i terminating Finish[i] = true go to step 2. 4. If Finish [i] == true for all i, then the system is in a safe state. Otherwise, in unsafe state. This determines if there is a safe sequence. The sequence in which the Finish[I] elements are assigned true values is the safe sequence. 46
47
Example n=5 processes P0 through P4 m=3 resource types
A (10 instances), B (5 instances), and C (7 instances). Snapshot at time T0: Available Allocation Max A B C P0 1 7 5 3 P1 2 P2 9 P3 P4 4 A B C 3 3 2 This resulting state wouldn’t actually be allowed next – rather it’s tentatively allowed, and then only really allowed if resulting state is safe P1 requests (1, 0, 2). Will this raise an error or cause P1 to wait? If not, what is the resulting (predicted) state? 47 47
48
Resulting (Predicted) State
Available Allocation Max A B C P0 1 7 5 3 P1 2 P2 9 P3 P4 4 A B C 2 3 0 Is the sequence: <P1, P3, P4, P0, P2> safe? Now: Is this state safe? 48 48
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.