Deadlock
– Ch. 7
SISTIM OPERASI
(Operating System)
IKI-20230
Johny Moningka
([email protected])
Fakultas Ilmu Komputer Universitas Indonesia Semester 2000/2001
Deadlock
n
System Model
n
Deadlock Characterization
n
Methods for Handling Deadlocks
n
Deadlock Prevention
n
Deadlock Avoidance
n
Deadlock Detection
Deadlock JM-2000/v1.1/3
The Deadlock Problem
n
Sekumpulan proses sedang blocked karena
setiap proses sedang menunggu (antrian)
menggunakan “resources” yang sedang
digunakan (hold) oleh proses lain.
n
Contoh:
n OS hanya mempunyai akese ke 2 tape drives. n P1dan P2memerlukan 2 tape sekaligus untuk
mengerjakan task (copy).
n P1dan P2masing-masing hold satu tape drives dan
sedang blocked, karena menunggu 1 tape drives “available”.
Deadlock JM-2000/v1.1/4
Terminologi
n Resource: R1, R2, . . ., Rm
n Klasifikasi: serially shareable/reusable
• Lebih dari satu proses dapat menggunakan resource.
• Tapi hanya ada satu proses yang dapat menggunakan resource pada satu saat.
• Reusable: setelah selesai resource diberikan ke proses lain. • Contoh: printer, CPU, etc.
n Setiap jenis resource dapat mempunyai lebih dari satu object (instances):
• Each resource type Ri has Wi instances
• Contoh: Printer => Rp mempunyai Wp1, Wp2, Wp3 etc.
n Each process utilizes a resource as follows: • request
• use • release
Deadlock JM-2000/v1.1/5
Model
n
Deadlock Prevention: Pencegahan adanya
faktor-faktor penyebab deadlock
n
Deadlock Avoidance: Menghindari dari situasi
yang potensial dapat mengarah menjadi
deadlock
n
Deadlock Detection
: Jika deadlock ternyata tidak
terhindari maka bagaimana mendeteksi
terjadinya deadlock, dilanjutkan dengan
penyelamatan (recovery).
Deadlock Prevention
n
Pencegahan: Faktor-faktor penyebab deadlock
yang harus dicegah untuk terjadi
n
4 faktor yang harus dipenuhi untuk terjadi
deadlock:
n Mutual Exclusion: pemakaian resources.
n Hold and Wait: cara menggunakan resources.
n No preemption resource: otoritas/hak. n Circular wait: kondisi saling menunggu.
n
Jika salah satu bisa dicegah maka deadlock pasti
tidak terjadi!
Deadlock JM-2000/v1.1/7
Necessary Conditions
n
Mutual Exclusion
n Serially-shareable resources (mis. Buffer) n Contoh: Critical section mengharuskan mutual
exclusion (termasuk resource), sehingga potensi proses akan saling menunggu (blocked).
n
Hold & wait :
n Situasi dimana suatu proses sedang hold suatu
resource secara eksklusif dan ia menunggu mendapatkan resource lain (wait).
Deadlock JM-2000/v1.1/8
Necessary condition (cont.)
n
No-Preemption Resouce :
n Resource yang hanya dapat dibebaskan secara sukarela oleh proses yang telah mendapatkannya
n Proses tidak dapat dipaksa (pre-empt) untuk melepaskan resource yang sedang di hold
n
Circular wait :
n Situasi dimana terjadi saling menunggu antara beberapa proses sehingga membentuk waiting-chain (circular)
n Misalkan proses (P0, P1, .. Pn) sedang blok
menunggu resources: P0 menunggu P1, P1 menunggu P2, .. dan Pn menunggu P0.
Deadlock JM-2000/v1.1/9
Deadlock Prevention (1)
n
Tindakan preventif:
n Batasi pemakaian resources
n Masalah: sistim tidak efisien, tidak feasible
n
Mutual Exclusion:
n tidak diperlukan untuk shareable resources
n read-only files/data : deadlock dapat dicegah dengan
tidak membatasi akses (not mutually exclusive)
n tapi terdapat resource yang harus mutually exclusive (printer)
Deadlock Prevention (2)
n
Hold and Wait
n Request & alokasi dilakukan saat proses start (dideklarasikan dimuka program)
n Request hanya bisa dilakukan ketika tidak sedang mengalokasi resource lain; alokasi beberapa resource dilakukan sekaligus dalam satu request
n Simple tapi resource akan dialokasi walau tidak selamanya digunakan (low utilization) serta beberapa proses bisa mengalami starvation
Deadlock JM-2000/v1.1/11
Deadlock Prevention (3)
n
Mencegah Circulair Wait
n Pencegahan: melakukan total ordering terhadap
semua jenis resource
n Setiap jenis resource mendapatkan index yang unik dengan bilangan natural: 1, 2, . .
• Contoh: tape drive=1, disk drive=5, printer=12
n Request resource harus dilakukan pada
resourceresource dalam urutan menaik (untuk index sama -request sekaligus)
n Jika Pi memerlukan Rk yang berindeks lebih kecil dari yang sudah dialokasi maka ia harus melepaskan semua resource Rj yang berindeks >= Rk
Deadlock JM-2000/v1.1/12
Deadlock Prevention (4)
n
Mencegah No-Preemption
n Jika proses telah mengalokasi resource dan ingin mengalokasi resource lain –
tapi tidak diperoleh (wait) : maka ia melepaskan semua resource yang telah dialokasi.
n Proses akan di-restart kelak untuk mecoba kembali mengambil semua resources
Deadlock JM-2000/v1.1/13
Deadlock Avoidance
n Pencegahan:
n Apabila di awal proses; OS bisa mengetahui resource mana saja yang akan diperlukan proses.
n OS bisa menentukan penjadwalan yang aman (“safe sequence”) alokasi resources.
n Model:
n Proses harus menyatakan max. jumlah resources yang diperlukan untuk selesai.
n Algoritma “deadlock-avoidance” secara dinamik akan memeriksa alokasi resource apakah dapat mengarah ke status (keadaan) tidak aman (misalkan terjadi circulair wait condition)
n Jadi OS, tidak akan memberikan resource (walaupun available), kalau dengan pemberian resource ke proses menyebakan tidak aman (unsafe).
Deadlock JM-2000/v1.1/15
Safe state
n
Prasyarat:
n Proses harus mengetahui max. resource yang
diperlukan (upper bound) => asumsi algoritma.
n Proses dapat melakukan hold and wait, tapi terbatas pada sekumpulan resource yang telah menjadi “kreditnya”.
n Setiap ada permintaan resource, OS harus memeriksa
• “jika resource diberikan”, dan terjadi “worst case” semua proses melakukan request “max. resource”
• Terdapat “urutan yang aman” dari resources yang available, untuk diberika ke proses, sehingga tidak terjadi deadlock.
Deadlock JM-2000/v1.1/16
Safe condition
n
Resources: 12 tape drive.
5 8 U3 4 6 U2 1 4 U1 Allocation Max. need User A (Available): 12 - 10 = 2 Safe sequnce:
2 tape diberikan ke U2, U2 selesai => Av = 6, Berikan 3 tape ke U1, Berikan 3 tape ke U3. No deadlock.
Deadlock JM-2000/v1.1/17
Unsafe condition
n
Resources: 12 tape drive.
6 8 U3 4 6 U2 1 4 U1 Allocation Max. need User A (Available): 12 - 11 = 1
Terdapat 1 tape available, sehingga dapat terjadi Deadlock.
Banker’s Algorithm
n
Proses harus “declare” max. kredit resource yang
diinginkan.
n
Proses dapat block (pending) sampai resource
diberikan.
n
Banker’s algorithm menjamin sistim dalam
keadaan safe state.
n
OS menjalankan Banker’s algorithm,
n Saat proses melakukan request resource.n Saat proses terminate atau release resource yang digunakan => memberikan resource ke proses yang
Deadlock JM-2000/v1.1/19
Banker’s Algorithm (2)
n
Method:
1. Scan table row by row and find a job that can finish 2. Add finished job's resources to number available. n Repeat 1 and 2 until:
• no more jobs can finish (unsafe) or
• all jobs finish (safe)
Deadlock JM-2000/v1.1/20
Banker’s Algorithm (3)
n Misakan terdapat: nproses dan mresources. n Definisikan:
• Available: Vector/array dengan panjang m.
If available [j] = k, terdapat k instances resouce jenis Rj yang dapat digunakan.
• Max: matrix n x m.
If Max [i,j] = k, maka proses Pi dapat request paling banyak k instances resource jenis Rj.
• Allocation: matrix n x m.
If Allocation[i,j] = k maka Pi saat ini sedang menggunakan (hold) k instances Rj.
• Need: matrix n x m.
If Need[i,j] = k, maka Pi palaing banyak akan membutuhkan instance Rj untuk selesai.
Deadlock JM-2000/v1.1/21
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work := Available // resource yang free
Finish [i] = false for i = 1,3, …, n.
2. Find and i such that both: // penjadwalan alokasi resource
(a) Finish [i] = false // asume, proses belum complete
(b) Needi≤Work // proses dapat selesai, ke step 3
If no such i exists, go to step 4.
3. Work := Work + Allocationi // proses dapat selesai Finish[i] := true
go to step 2.
4. If Finish [i] = true for all i, then the system is in a safe state.
Safety Algorithm (2)
5 8 U3 4 6 U2 1 4 U1 Allocation Max. need UserTerdapat 3 proses: n = 3, 1 resource: m = 1 Jumlah resource m = 12.
Snapshot pada waktu tertentu:
Max: [4 6 8] Allocation: [1 4 5] Available: [2]
Deadlock JM-2000/v1.1/23
Safety Algorithm (3)
Need [i,j] = Max[i,j] – Allocation [i,j]. Max: [4 6 8] Allocation: [1 4 5] = Need: [3 2 3]
-Let Need[3]; Max[3]; Aloc[3]; Finish[3]; Work [1]; 1. Work = Available; // Work = 2;
Finish[0]=false, Finish[1]=false, Finish[3]=false; 2. do {
FlagNoChange = false; for I=0 to 2 {
if ((Finish[I] == false)) && (Need[I] <= Work) { Finis[I] = true;
Work = Work + Aloc[I]; FlagNoChange = true; }
} until (FlagNoChange);
Deadlock JM-2000/v1.1/24
Deadlock Detection
n
Mencegah dan menghidari dari deadlock sulit
dilakukan:
n Kurang efisien dan utilitas sistim
n Sulit diterapkan: tidak praktis, boros resources
n
Mengizinkan sistim untuk masuk ke “state
deadlock”
n Gunakan algoritma deteksi (jika terjadi deadlock)
• Deteksi: melihat apakah penjadwalan pemakaian resource yang tersisa masih memungkinkan berada dalam safe state (variasi “safe state”).
Deadlock JM-2000/v1.1/25
Single Instance
n
Gunakan: resource allocation graph
n Node mewakili proses, arcus mewakili request dan
hold dari resources.
n
Dapat disederhanakan dalam “wait-for-graph”
n Pi→Pj if Piis waiting for Pj.n
Secara periodik jalankan algoritma yang mencari
cycle pada graph:
n Jika terdapat siklus (cycle) pada graph maka telah terjadi deadlock.
Resource-Allocation Graph
Deadlock JM-2000/v1.1/27
Recovery from Deadlock
n
Abort all deadlocked processes.
n
Abort one process at a time until the deadlock
cycle is eliminated.
n
In which order should we choose to abort?
n Priority of the process.n How long process has computed, and how much longer to completion.
n Resources the process has used.
n Resources process needs to complete.
n How many processes will need to be terminated.
n Is process interactive or batch?
Deadlock JM-2000/v1.1/28
Recovery from Deadlock
nSelecting a victim – minimize cost.
n
Rollback – return to some safe state, restart
process fro that state.
n
Starvation – same process may always be
picked as victim, include number of rollback in
cost factor.
Deadlock JM-2000/v1.1/29
Combined Approach
n
Combine the three basic approaches
n preventionn avoidance n detection
allowing the use of the optimal approach for each
of resources in the system.
n
Partition resources into hierarchically ordered
classes.
n