resource allocat ion and deadlock handling what s in a
play

Resource Allocat ion and Deadlock Handling What s in a deadlock - PowerPoint PPT Presentation

Resource Allocat ion and Deadlock Handling What s in a deadlock Deadlock: A set of blocked pr ocesses each wait ing f or an event (e.g. a r esour ce t o become available) t hat only anot her pr ocess in t he set can cause 2 Examples


  1. Resource Allocat ion and Deadlock Handling

  2. What ’s in a deadlock Deadlock: A set of blocked pr ocesses each wait ing f or an event (e.g. a r esour ce t o become available) t hat only anot her pr ocess in t he set can cause 2

  3. Examples of (pot ent ial) Deadlocks in Resour ce Allocat ion • semaphores A and B , init ialized t o 1 (or: syst em has 2 t ape drives; P 0 and P 1 each hold one t ape drive and each needs anot her one) P P 0 1 wait (A); wait (B); wait (B); wait (A) • 200Kbyt es memory-space is available P P 0 1 request (80Kbyt es); request (80Kbyt es); … … request (70Kbyt es); request (70Kbyt es); deadlock might occur if bot h processes progress t o t he second request • message-passing wit h blocking receive P P 0 1 receive(P 1 ); receive(P 0 ); send(P 1 , M1); send(P 0 , M0); 3

  4. Bridge Crossing Example • Traf f ic only in one direct ion. • Each “half ” of t he bridge can be viewed as a resource. • I f a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). – several cars may have t o be backed up – st arvat ion is possible. 4

  5. Condit ions f or Deadlock [Cof f man-et al 1971] 4 conditions must hold simultaneously f or a deadlock t o occur: • Mutual exclusion: only one pr ocess at a t ime can use a r esour ce. • Hold and wait: a pr ocess holding some r esour ce can r equest addit ional r esour ces and wait f or t hem if t hey ar e held by ot her pr ocesses. • No preemption: a r esour ce can only be r eleased volunt ar ily by t he pr ocess holding it , af t er t hat pr ocess has complet ed it s t ask. – Q: examples preempt able/ non-preemt able resources? • Circular wait: t her e exist s a cir cular chain of 2 or mor e blocked pr ocesses, each wait ing f or a r esour ce held by t he next pr ocess in t he chain 5

  6. Resource Allocat ion & Handling of Deadlocks • St r uct ur ally r est r ict t he way in which pr ocesses r equest r esour ces – deadlock prevent ion : deadlock is not possible • Requir e pr ocesses t o give advance inf o about t he (max) r esour ces t hey will r equir e; t hen schedule pr ocesses in a way t hat avoids deadlock. – deadlock avoidance : deadlock is possible, but OS uses advance inf o t o avoid it • Allow a deadlock st at e and t hen r ecover • I gnor e t he pr oblem and pr et end t hat deadlocks never occur in t he syst em (can be a “solut ion” somet imes?!… ) 6

  7. Resource Allocat ion wit h Deadlock Prevent ion Rest rain t he ways request s can be made; at t ack at least one of t he 4 condit ions, so t hat deadlocks are impossible t o happen: • Mutual Exclusion – (cannot do much here … ) • Hold and Wait – must guarant ee t hat when a process request s a resource, it does not hold any ot her resources . – Require process t o request and be allocat ed all it s resources at once or allow process t o request resources only when t he process has none. – Low resource ut ilizat ion; st arvat ion possible. • No Preemption – I f a process holding some resources request s anot her resource t hat cannot be immediat ely allocat ed, it releases t he held resources and has t o request t hem again (risk f or st arvat ion). Circular Wait – impose t ot al ordering of all resource t ypes, and require • t hat each process request s resources in an increasing order of enumerat ion (e.g f irst t he t ape, t hen t he disk). • Examples? 7

  8. Fight t he circular wait : Dining philosophers example r equest f or ks in incr easing f or k-id var f [0..n]: bin-semaphor e / init all 1 / Pn P_i: (i!=n) Repeat Repeat Wait (f [(i+1)modn]) Wait (f [i]) Wait (f [(i+1)modn]) Wait (f [i]) Eat Eat Signal(f [(i+1)modn]) Signal(f [i]) Signal(f [i]) Signal(f [(i+1)modn]) Think f or ever Think f or ever 8

  9. Fight t he hold and wait : Dining philosophers example leave_f orks(i) semaphor e S[N] take_f orks(i) wait (mut ex) wait (mut ex) int st at e[N] st at e(i) := st at e(i) := HUNGRY THI NKI NG t est (i) Pi: do t est (lef t (i)) signal(mut ex) t est (r ight (i) < t hink> wait (S[i]) signal(mut ex) t ake_f or ks(i) < eat > leave_f or ks(i) f or ever test(i) if st at e(i) ==HUNGRY && st at e(lef t (i) ) != EATI NG && st at e(lef t (i) ) != EATI NG t hen st at e(i) := EATI NG 9 signal(S[i])

  10. Fight t he no-preempt ion: Dining philosophers example var f [0..n]: r ecor d s: bin-semaphor e available: boolean / init all 1 / t rylock(f ork) P_i: wait (f ork.s) I f f ork.available t hen Repeat f ork.available := f alse While < not holding bot h f or ks> do ret := t rue Lock(f [i]) else ret := f alse I f !t rylock(f [(i+1)modn]) t hen release f [i]; Ret urn(ret ) od Signal(f ork.s) Eat Release(f [i]) Release(f [(i+1)modn]) Lock(f ork) Think Repeat f or ever Unt il (t rylock(f ork)) 10

  11. Syst em Model • Resource t ypes R 1 , R 2 , . . ., R m – e.g. CPU, memory space, I / O devices, f iles – each resource t ype R i has W i inst ances. • Each process ut ilizes a resource as f ollows: – request – use – release Resour ce-Allocat ion Gr aph A set of vert ices V and a set of edges E . – V is part it ioned int o t wo set s: • P = { P 1 , P 2 , … , P n } t he set of processes • R = { R 1 , R 2 , … , R m } t he set of resource t ypes i  R j – request edge: P assignment edge: R j  P – i 11

  12. Example of a Resour ce Allocat ion Gr aph 12

  13. Resour ce Allocat ion Gr aph Wit h A Deadlock 13

  14. Resour ce Allocat ion Gr aph Wit h A cycle but no Deadlock 14

  15. Basic Fact s gr aph cont ains no cycles  no deadlock. • (i.e. cycle is always a necessary condit ion f or deadlock) I f gr aph cont ains a cycle  • – if one inst ance per resource t ype, t hen deadlock. – if several inst ances per resource t ype, t hen possibilit y of deadlock • Thm: if immediat e-allocat ion-met hod, t hen knot  deadlock . – Knot = knot – st rongly connect ed subgraph (no sinks) wit h no out going edges 15

  16. Resource Allocat ion wit h Deadlock Avoidance Requires a priori inf ormat ion available. • e.g.: each process declares maximum number of resources of each t ype t hat it may need (e.g memory/ disk pages). Deadlock- avoidance algo : • examines t he resource-allocat ion st at e… – available and allocat ed resources – maximum possible demands of t he processes. • … t o ensure t here is no pot ent ial f or a circular-wait : – saf e st at e  no deadlocks in t he horizon. – unsaf e st at e  deadlock might occur (lat er… ) – Q: how t o do t he saf et y check? • Avoidance = ensure t hat syst em will not ent er an unsaf e st at e. I dea : I f sat isf ying a request will result in an unsaf e st at e , t he request ing process is suspended unt il enough resources are f ree-ed by processes t hat will t erminat e in t he meanwhile. 16

  17. Enhanced Resource Allocat ion Graph f or Deadlock Avoidance i  R j : P • Claim edge P j may r equest r esour ce R j – represent ed by a dashed line. • Claim edge conver t s t o r equest edge when a pr ocess r equest s a r esour ce. • When a r esour ce is r eleased by a pr ocess, assignment edge r econver t s t o a claim edge. • Resour ces must be claimed a pr ior i in t he syst em. 17

  18. Example Resour ce-Allocat ion Gr aph For Deadlock Avoidance: Saf e St at e 18

  19. Example Resour ce-Allocat ion Gr aph For Deadlock Avoidance: Unsaf e St at e 19

  20. Saf et y checking: Mor e on Saf e St at e saf e state = t her e exist s a saf e sequence < of P 1 , P 2 , … , P n > t er minat ing all pr ocesses: f or each P i , t he r equest s t hat it can st ill make can be gr ant ed by cur r ent ly available r esour ces + t hose held by P 1 , P 2 , … , P i- 1 The syst em can schedule t he pr ocesses as f ollows : • – if P i ‘s r esour ce needs ar e not immediat ely available, t hen it can • wait unt il all P 1 , P 2 , … , P i-1 have f inished • obt ain needed resources, execut e, release resources, t erminat e. – t hen t he next pr ocess can obt ain it s needed r esour ces, and so on. 20

  21. Banker’s Algorit hm f or Resource Allocat ion wit h Deadlock Avoidance Data Structures : • Max: n x m mat rix. – Max [ i,j ] = k : P i may request max k inst ances of resource t ype R j . • Allocat ion: n x m mat rix. – Allocat ion[ i,j ] = k: P i is current ly allocat ed k inst ances of R j . • Available: lengt h m vect or – available [ j ] = k : k inst ances of resource t ype R j available. • Need: n x m mat rix: – Need [ i,j ] = Max [ i,j ] – Allocat ion [ i,j ]: pot ent ial max request by P i f or resource t ype R j RECALL: Avoidance = ensure t hat syst em will not ent er an unsaf e st at e. I dea : I f sat isf ying a request will result in an unsaf e st at e , t hen request ing process is suspended unt il enough resources are f ree-ed by processes t hat will t erminat e in t he meanwhile. 21

Recommend


More recommend