Académique Documents
Professionnel Documents
Culture Documents
Deadlocks
Course 7: Review
Syncronization solutions
Hardware
Load/Store
Disable Interrupts
Test&Set
Higher-level API
Locks
Semaphores
Monitors
Send/Receive
Programs
Shared programs
6.2
Course 7: Review
Locks
Suppose we have some sort of implementation of a lock:
Lock.Acquire() wait until lock is free, then grab
Lock.Release() unlock, waking up anyone waiting
These must be atomic operations if two threads are waiting for the
lock and both see its free, only one succeeds to grab the lock
Then, our milk problem is easy:
milklock.Acquire();
if (nomilk)
buy milk;
milklock.Release();
Once again, section of code between Acquire() and Release() called a
Critical Section
6.3
Course 7: Review
Disable Interrupts
naive Implementation of locks:
LockAcquire { disable Ints; }
LockRelease { enable Ints; }
better Implementation of locks:
int value = FREE;
Release() {
disable interrupts;
if (anyone on wait queue) {
take thread off wait queue
Place on ready queue;
} else {
value = FREE;
}
enable interrupts;
}
Acquire() {
disable interrupts;
if (value == BUSY) {
put thread on wait queue;
Go to sleep();
// Enable interrupts?
} else {
value = BUSY;
}
enable interrupts;
}
6.4
Course 7: Review
Test&Set
test&set (&address) {
result = M[address];
M[address] = 1;
return result;
}
/* most architectures */
//Critical section
Release() {
value = 0;
}
6.5
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
6.6
6.7
Semaphores
Semaphores are a kind of generalized lock
6.8
Semaphores
Semaphores are like integers, except
No negative values
6.9
Uses of semaphores
Mutual Exclusion (initial value = 1)
6.10
Producer
Buffer
Consumer
6.11
6.12
}
Consumer() {
fullBuffers.P();
mutex.P();
item = Dequeue();
mutex.V();
emptyBuffers.V();
return item;
}
6.13
6.14
Cleaner idea: Use locks for mutual exclusion and condition variables
6.15
6.16
AddToQueue(item) {
lock.Acquire();
// Get Lock
queue.enqueue(item);
// Add item
dataready.signal();// Signal any waiters
lock.Release();
// Release Lock
}
RemoveFromQueue() {
lock.Acquire();
while (queue.isEmpty()) {
dataready.wait(&lock);
// If nothing, sleep
}
item = queue.dequeue();
lock.Release();
return(item);
}
Operating System Concepts 8th Edition
6.17
// Get Lock
6.18
Example
Example
P1
wait(B) //P(B)
wait(A) //P(A)
6.19
occurs.
Starvation is possible.
6.20
System Model
Resource types R1, R2, . . ., Rm
request
use
release
6.21
Deadlock Conditions
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a
resource.
Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes.
No preemption: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task.
Circular wait: there exists a set {P0, P1, Pn, P0} of
waiting processes such that P0 is waiting for a resource that
is held by P1, P1 is waiting for a resource that is held by
P2, etc., and Pn is waiting for a resource that is held by P0.
6.22
Resource-Allocation Graph
A set of vertices/nodes V and a set of edges E.
V consist in two types:
6.23
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Operating System Concepts 8th Edition
6.24
6.25
6.26
6.27
Basic Facts
If graph contains no cycles no deadlock.
If graph contains a cycle
6.28
Deadlock Handling
Deadlock prevention
Deadlock avoidance
Deadlock detection
Operating System Concepts 8th Edition
6.29
recover.
Ignore the problem and pretend that deadlocks never occur
6.30
Deadlock Prevention
Restrain the ways request can be made.
Mutual Exclusion not required for sharable resources;
6.31
6.32
Deadlock Avoidance
Requires that the system has some additional a priori information
available.
Simplest and most useful model requires that each process
6.33
Safe State
When a process requests an available resource, system must
of ALL the processes is the systems such that for each Pi, the
resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j < i.
That is:
6.34
Basic Facts
If a system is in safe state no deadlocks.
If a system is in unsafe state possibility of deadlock.
Avoidance ensure that a system will never enter an
unsafe state.
6.35
6.36
Avoidance algorithms
Single instance of a resource type. Use a resource-
allocation graph
Multiple instances of a resource type. Use the bankers
algorithm
6.37
requests a resource.
Request edge converted to an assignment edge when the
6.38
Resource-Allocation Graph
6.39
6.40
6.41
Bankers Algorithm
Multiple instances for resources.
Each process must a priori claim maximum use.
When a process requests a resource it may have to wait.
When a process gets all its resources it must return them in
6.42
6.43
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, , n- 1.
2. Find and i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe
state.
6.44
6.45
3 resource types:
Allocation
Max
Available
ABC
ABC
ABC
P0
010
753
332
P1
200
322
P2
302
902
P3
211
222
P4
002
433
6.46
Example (Cont.)
The content of the matrix Need is defined to be Max Allocation.
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
6.47
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
6.48
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
6.49
6.50
Resource-Allocation Graph
6.51
6.52
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
6.53
6.54
Allocation
Request
Available
ABC
ABC
ABC
P0
010
000
000
P1
200
202
P2
303
000
P3
211
100
P4
002
002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
6.55
Example (Cont.)
P2 requests an additional instance of type C.
State of system?
6.56
Detection-Algorithm Usage
When, and how often, to invoke depends on:
6.57
6.58
Rollback return to some safe state, restart process for that state.
Starvation same process may always be picked as victim,
6.59
End of Course 8