Professional Documents
Culture Documents
Materi SO - 7
Materi SO - 7
Sistem Operasi
S-1 Ilmu Komputer USU
Deadlocks
z
▪ System Model
▪ Deadlock Characterization
▪ Deadlock Prevention
▪ Deadlock Avoidance
▪ Deadlock Detection
▪ request
▪ use
▪ release
Deadlock Characterization
z
▪ T = {T1, T2, …, Tn}, the set consisting of all the threads in the
system
▪ R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system
▪ Thread
▪ Ti requests instance of Rj
Ti
Rj
▪ Ti is holding an instance of Rj
Ti
Rj
Example of a Resource Allocation Graph
z
Resource Allocation Graph With A Deadlock
z
Graph With A Cycle But No Deadlock
z
Basic Facts
z
▪ Deadlock prevention
▪ Deadlock avoidance
▪ No Preemption –
▪ That is:
▪ If Pi resource needs are not immediately available, then Pi can wait until
all Pj have finished
▪ When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate
▪ When Pi terminates, Pi +1 can obtain its needed resources, and so on
Basic Facts
z
▪ Multiple instances
Data Structures for the Banker’s Algorithm
z
▪ Max: n x m matrix; the max demand of each thread. If Max [i,j] = k, then
thread Ti may request at most k instances of resource type Rj
4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for Thread Pi
z
Requesti = request vector for thread Ti. If Requesti [j] = k then thread
Ti wants k instances of resource type Rj
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since
thread has exceeded its maximum claim
2. If Requesti Available, go to step 3. Otherwise Ti must wait, since
resources are not available
3. Pretend to allocate requested resources to Ti by modifying the state as
follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe the resources are allocated to Ti
If unsafe Ti must wait, and the old resource-allocation state is restored
Example of Banker’s Algorithm
z
3 resource types:
T4 002 433 10 4 7
10 5 7
z
Example (Cont.)
▪ The content of the matrix Need is defined to be Max – Allocation
Need
ABC
T0 743
T1 122
T2 600
T3 011
T4 431
▪ The system is in a safe state since the sequence < T1, T3, T4, T2, T0>
satisfies safety criteria
Example: T1 Request (1,0,2)
z
▪ Check that Request Available (that is, (1,0,2) (3,3,2) true
T1 302 020
T2 302 600
T3 211 011
T4 002 431
▪ Executing safety algorithm shows that sequence < T1, T3, T4, T0,
T2> satisfies safety requirement
Deadlock Detection
z
▪ Detection algorithm
▪ Recovery scheme
Single Instance of Each Resource Type
z
▪ Ti → Tj if Ti is waiting for Tj