Administrivia Assignments 0 & 1 Class contact f or the next two - - PowerPoint PPT Presentation

administrivia
SMART_READER_LITE
LIVE PREVIEW

Administrivia Assignments 0 & 1 Class contact f or the next two - - PowerPoint PPT Presentation

Administrivia Assignments 0 & 1 Class contact f or the next two weeks Next week midt er m exam pr oj ect r eview & discussion (Chr is Chamber s) Following week Memor y Management (Wuchi Feng) 1 CSE 513 I


slide-1
SLIDE 1

1

Administrivia

Assignments 0 & 1 Class contact f or the next two weeks Next week

midt er m exam pr oj ect r eview & discussion (Chr is Chamber s)

Following week

Memor y Management (Wuchi Feng)

slide-2
SLIDE 2

2

CSE 513 I ntroduction to Operating Systems Class 4 - I PC & Synchronization (2) Deadlock

Jonathan Walpole

  • Dept. of Comp. Sci. and Eng.

Oregon Health and Science University

slide-3
SLIDE 3

3

Counting semaphores

A binary semaphore can only take on the values

  • f [0, 1].

Class exercise: create a counting semaphore

(generalized semaphore that we discussed previously) using just a binary semaphore!!

slide-4
SLIDE 4

4

Possible solution

Semaphore S1, S2, S3; // BINARY!! int C = N; // N is # locks down_c(sem){ downB(S3); downB(S1); C = C – 1; if (C<0) { upB(S1); downB(S2); } else { upB(S1); } upB(S3); } up_c(sem){ downB(S1); C = C + 1; if (C<=0) { upB(S2); } upB(S1); }

slide-5
SLIDE 5

5

Monitors

I t is dif f icult to produce correct programs using

semaphores

cor r ect or der ing of up and down is t r icky! avoiding deadlock is t r icky! boundar y condit ions ar e t r icky!

Can we get the compiler to generate the

correct semaphore code f or us?

what ar e suit able higher level abst r act ions f or

synchr onizat ion?

slide-6
SLIDE 6

6

Monitors

  • Collect related shared objects together in a monitor
  • Encapsulation and mutual exclusion

Local dat a variables are accessible only via t he monit or’s

procedures

P

rocesses ent er t he monit or by invoking one of it s procedures

Only one process may execut e wit hin t he monit or at a

given t ime

  • Condition variables (cv)

Wait (cv) – process blocked (queued) unt il condit ion holds Signal(cv) – signals t he condit ion and unblocks (dequeues)

a process

slide-7
SLIDE 7

7

Monitor structures

initialization code monitor operations y x shared data condition queues monitor entry queue

slide-8
SLIDE 8

8

Monitor example f or mutual exclusion

process Producer begin loop <produce char “c”> BoundedBuffer.deposit(c) end loop end Producer process Consumer begin loop BoundedBuffer.remove(c) <consume char “c”> end loop end Consumer monitor: BoundedBuffer var buffer : ...; nextIn, nextOut :... ; entry deposit(c: char) begin ... end entry remove(var c: char) begin ... end end BoundedBuffer

slide-9
SLIDE 9

9

Monitor example with condition variables

monitor : BoundedBuffer var buffer : array[0..n-1] of char nextIn,nextOut : 0..n-1 := 0 fullCount : 0..n := 0 notEmpty, notFull : condition entry deposit(c:char) entry remove(var c: char) begin begin if (fullCount = n) then if (fullCount = n) then wait(notFull) wait(notEmpty) end if end if buffer[nextIn] := c c := buffer[nextOut] nextIn := nextIn+1 mod n nextOut := nextOut+1 mod n fullCount := fullCount+1 fullCount := fullCount-1 signal(notEmpty) signal(notFull) end deposit end remove end BoundedBuffer

slide-10
SLIDE 10

10

Monitor design choices

  • Condition variables introduce a problem f or mutual

exclusion

  • nly one process act ive in t he monit or at a t ime, so what

t o do when a process is unblocked on signal?

must not block holding t he mut ex, so what t o do when a

process blocks on wait ?

  • Should signals be stored/ remembered?

signals are not st ored if signal occurs bef ore wait , signal is lost !

  • Should condition variables count?
slide-11
SLIDE 11

11

Monitor design choices

  • Choices when A signals a condition that unblocks B

A wait s f or B t o exit t he monit or or blocks again B wait s f or A t o exit t he monit or or block Signal causes A t o immediat ely exit t he monit or or block

(on what condit ion?)

  • Choices when A signals a condition that unblocks B & C

B is unblocked, but C remains blocked C is unblocked, but B remains blocked

  • Choices when A calls wait and blocks

a new ext ernal process is allowed t o ent er but which one?

slide-12
SLIDE 12

12

Common monitor semantics

Hoare semantics

On signal, allow signaled pr ocess t o r un; upon it s

exit f r om t he monit or , signaler pr ocess cont inues

Brinch Hansen semantics

signaler must immediat ely exit f ollowing signal

slide-13
SLIDE 13

13

Message Passing

I nterprocess communication

via shar ed memor y acr oss machine boundar ies

Message passing can be used locally or remotely

f or synchronization or general communication

pr ocesses use send and receive pr imit ives r eceive can block like wait send unblocks a pr ocess blocked on r eceive (like

signal unblocking a wait ing pr ocess)

slide-14
SLIDE 14

14

Producer consumer with message passing

slide-15
SLIDE 15

15

Design Choices f or Message Passing

Mailboxes

syst em maint ains a buf f er of sent , but not yet

r eceived, messages

Rendezvous

sender and r eceiver must be act ive at t he same

t ime

r eceive must be blocked bef or e send occur s ker nel does no buf f er ing when does t he send r et ur n?

slide-16
SLIDE 16

16

Barriers

Use of a barrier

pr ocesses appr oaching a bar r ier all pr ocesses but one blocked at bar r ier last pr ocess ar r ives, all ar e let t hr ough

slide-17
SLIDE 17

17

Deadlock

slide-18
SLIDE 18

18

Resources and Deadlocks

  • Processes need access to resources in order to make progress
  • Examples of computer resources

print ers t ape drives kernel dat a st ruct ures (process & f ile t able ent ries …

)

locks/ semaphores t o prot ect crit ical sect ions

  • Suppose a process holds resource A and requests resource B

at t he same t ime anot her process holds B and request s A bot h are blocked and remain so …

t his is deadlock

slide-19
SLIDE 19

19

Resource Usage Model

  • Sequence of events required to use a resource
  • r equest t he r esour ce (like acquir ing a mut ex lock)
  • use t he r esour ce
  • r elease t he r esour ce (like r eleasing a mut ex lock)
  • Must wait if request is denied
  • block
  • busy wait
  • f ail wit h er r or code
slide-20
SLIDE 20

20

Preemptable vs Nonpreemptable Resources

Preemptable resources

can be t aken away f r om a pr ocess wit h no ill ef f ect s

Nonpreemptable resources

will cause t he pr ocess t o f ail if t aken away

Deadlocks occur when processes are granted

exclusive access to non- preemptable resources and wait when the resource is not available

slide-21
SLIDE 21

21

Def inition of Deadlock

A set of processes is deadlocked if each process in the set is waiting f or an event that only another process in the set can cause

  • Usually the event is the release of a currently held

resource

  • None of the processes can …

be awakened run release resources

slide-22
SLIDE 22

22

Deadlock conditions

  • A deadlock situation can occur if and only if the f ollowing

conditions hold simultaneously

Mut ual exclusion condit ion – resource assigned t o one

process

Hold and wait condit ion – processes can get more t han

  • ne resource

No preempt ion condit ion Circular wait condit ion – chain of t wo or more processes

(must be wait ing f or resource f rom next one in chain)

slide-23
SLIDE 23

23

Resource acquisition scenarios

down (resource_1); use resource_1; up (resource_1); down (resource_1); down (resource_2); use both resources; up (resource_2); up (resource_1); down (resource_2); use resource_2; up (resource_2); down (resource_1); down (resource_2); use both resources; up (resource_2); up (resource_1);

slide-24
SLIDE 24

24

Resource acquisition scenarios

down (resource_1); down (resource_2); use resources; up (resource_2); up (resource_1); down (resource_2); down (resource_1); use resources; up (resource_1); up (resource_2); down (resource_1); use resource; up (resource_1); down (resource_2); use resource; up (resource_2); down (resource_2); use resource; up (resource_2); down (resource_1); use resource; up (resource_1);

slide-25
SLIDE 25

25

Flavors of Deadlock

Not so bad

Pr ogr ammer cr eat es a sit uat ion t hat deadlocks Kill t he pr ogr am and move on

Worse

Spin locks and locking mechanisms wit hin t he OS

slide-26
SLIDE 26

26

Other examples of deadlock

slide-27
SLIDE 27

27

Deadlock modeling

Resource Allocation Graphs (RAGs) Resource R assigned to process A Process B waiting f or resource S Process C and D are deadlocked over T & U

slide-28
SLIDE 28

28

Dealing with deadlock

Four general strategies

I gnor e t he pr oblem

  • Hmm…

advantages, disadvantages?

Det ect ion and r ecover y Dynamic avoidance t hr ough r esour ce allocat ion Pr event ion, by st r uct ur ally negat ing one of t he

f our condit ions

slide-29
SLIDE 29

29

Deadlock detection (1 resource of each)

Let the problem happen, then recover How do you know it happened? Do a depth- f irst- search on the resource

allocation graph

slide-30
SLIDE 30

30

Deadlock detection (1 resource of each)

Let the problem happen, then recover How do you know it happened? Do a depth- f irst- search on the resource

allocation graph

slide-31
SLIDE 31

31

Deadlock detection (1 resource of each)

Let the problem happen, then recover How do you know it happened? Do a depth- f irst- search on the resource

allocation graph

slide-32
SLIDE 32

32

Deadlock detection (1 resource of each)

Let the problem happen, then recover How do you know it happened? Do a depth- f irst- search on the resource

allocation graph

slide-33
SLIDE 33

33

Deadlock detection (1 resource of each)

Let the problem happen, then recover How do you know it happened? Do a depth- f irst- search on the resource

allocation graph

slide-34
SLIDE 34

34

Deadlock modeling with multiple resources

Theorem: I f a graph does not contain a cycle

then no processes are deadlocked

A cycle in a RAG is a necessar y condit ion f or

deadlock

I s the existence of a cycle a suf f icient

condition?

slide-35
SLIDE 35

35

Deadlock modeling with multiple resources

Theorem: I f a graph does not contain a cycle

then no processes are deadlocked

A cycle in a RAG is a necessar y condit ion f or

deadlock

I s the existence of a cycle a suf f icient

condition?

slide-36
SLIDE 36

36

Deadlock detection (multiple resources)

slide-37
SLIDE 37

37

Deadlock detection (multiple resources)

Available resource vector Total resource vector

slide-38
SLIDE 38

38

Deadlock detection (multiple resources)

Available resource vector Total resource vector What I am requesting now What I have (now!)

slide-39
SLIDE 39

39

Detection algorithm

I s there a sequence of running process such

that all the resources will be returned?

slide-40
SLIDE 40

40

Detection algorithm

  • 1. Look f or an unmarked process Pi, f or which the

ith row of R is less than or equal to A

  • 2. I f such a process is f ound, add the i- th row
  • f C to A, mark the process and go back to

step 1

  • 3. I f no such process exists the algorithm

terminates If all marked, no deadlock

slide-41
SLIDE 41

41

Detection algorithm

slide-42
SLIDE 42

42

Detection algorithm

slide-43
SLIDE 43

43

Detection algorithm

slide-44
SLIDE 44

44

Detection algorithm

2 2 2 0

slide-45
SLIDE 45

45

Detection algorithm

2 2 2 0

slide-46
SLIDE 46

46

Detection algorithm

4 2 2 1 2 2 2 0

slide-47
SLIDE 47

47

Detection algorithm

4 2 2 1 2 2 2 0 No deadlock!

slide-48
SLIDE 48

48

Detection algorithms

How of ten should the algorithm run?

Af t er ever y r esour ce r equest ? Per iodically? When CPU ut ilizat ion is low? When we suspect deadlock?

slide-49
SLIDE 49

49

Recovery f rom deadlock

What should be done to recover?

Abor t deadlocked pr ocesses and r eclaim r esour ces Tempor ar ily r eclaim r esour ce, if possible Abor t one pr ocess at a t ime unt il deadlock cycle is

eliminat ed

Wher e t o st ar t ?

  • Low priority process
  • How long process has been executing
  • How many resources a process holds
  • Batch or interactive
  • Number of processes that must be terminated
slide-50
SLIDE 50

50

Other deadlock recovery techniques

Recovery through rollback

Save st at e per iodically

  • St ar t comput at ion again f r om “checkpoint ”

Done f or lar ge comput at ion syst ems

slide-51
SLIDE 51

51

Deadlock avoidance

Detection vs. avoidance…

Det ect ion – “opt imist ic” appr oach

  • Allocate resources
  • “Break” system to f ix it

Avoidance – “pessimist ic” appr oach

  • Don’t allocate resource if it may lead to deadlock

Which one t o use depends upon t he applicat ion

slide-52
SLIDE 52

52

Resource allocation plot

?

slide-53
SLIDE 53

53

Resource allocation graph

slide-54
SLIDE 54

54

Saf e states

Saf e state – “when system is not deadlocked

and there is some scheduling order in which every process can run to completion even if all

  • f them suddenly request their maximum

number of resource immediately”

6 2 5 10 total 3

slide-55
SLIDE 55

55

Unsaf e states

6 2 5 10 total 3 5 2 5 2

unsafe

slide-56
SLIDE 56

56

Banker’s algorithm f or multiple resources

  • Look f or a row, R, whose unmet resource needs are all

smaller than or equal to A. I f no such row exists, the system will eventually deadlock since no process can run to completion

  • Assume the process of the row chosen requests all the

resources that it needs (which is guaranteed to be possible) and f inishes. Mark that process as terminated and add all its resources to A vector

  • Repeat steps 1 and 2, until either all process are

marked terminated, in which case the initial state was saf e, or until deadlock occurs, in which case it was not

slide-57
SLIDE 57

57

Avoidance modeling

Available resource vector Total resource vector

Maximum Request Vector

Row 2 is what process 2 might need

RUN ALGORITHM ON EVERY RESOURCE REQUEST

slide-58
SLIDE 58

58

Avoidance algorithm

Ma x re que st ma trix

slide-59
SLIDE 59

59

Avoidance algorithm

Ma x re que st ma trix

slide-60
SLIDE 60

60

Avoidance algorithm

Ma x re que st ma trix

slide-61
SLIDE 61

61

Avoidance algorithm

2 2 2 0

Ma x re que st ma trix

slide-62
SLIDE 62

62

Avoidance algorithm

2 2 2 0

Ma x re que st ma trix

slide-63
SLIDE 63

63

Avoidance algorithm

4 2 2 1 2 2 2 0

Ma x re que st ma trix

slide-64
SLIDE 64

64

Deadlock avoidance

Deadlock avoidance is usually impossible

because you don’t know in advance what r esour ces a

pr ocess will need!

Alternative approach “deadlock prevention”

Pr event t he sit uat ion in which deadlock might occur

f or all t ime!

At t ack one of t he f our condit ions t hat ar e

necessar y f or deadlock t o be possible

slide-65
SLIDE 65

65

Attacking the conditions

Attacking mutual exclusion?

a bad idea f or some r esour ce t ypes may wor k f or ot her s

Attacking no preemption?

a bad idea f or some r esour ce t ypes may wor k f or ot her s

slide-66
SLIDE 66

66

Attacking the conditions

Attacking hold and wait

have pr ocesses r equest all r esour ces bef or e

beginning

– Underallocation of resources – Unknown requests

if new r equest , deallocat e and r eallocat e!

slide-67
SLIDE 67

67

Attacking the conditions

Attacking circular wait

same pr oblem/ solut ion in t he dining philosopher s number all r esour ces and acquir e in t he same or der may be har d t o get an or der ing t hat ever yone likes

1 2 3 4 5 6 7

slide-68
SLIDE 68

68

Attacking the conditions

Attacking circular wait

Same pr oblem in t he dining philosopher s Number all r esour ces Typically har d t o get an or der ing t hat ever yone

likes

1 2 3 4 5 6 7

slide-69
SLIDE 69

69

Attacking the conditions

Attacking circular wait

Same pr oblem in t he dining philosopher s Number all r esour ces Typically har d t o get an or der ing t hat ever yone

likes

1 2 3 4 5 6 7

slide-70
SLIDE 70

70

A word on starvation

Starvation and deadlock are two dif f erent

things

Wit h deadlock – no wor k is being accomplished f or

t he pr ocesses t hat ar e deadlocked, because pr ocesses ar e wait ing f or each ot her

Wit h st ar vat ion – wor k (pr ogr ess) is get t ing done,

however , a par t icular set of pr ocesses may not be get t ing any wor k done because t hey cannot obt ain t he r esour ce t hey ar e t r ying t o get

slide-71
SLIDE 71

71

Summary

What is deadlock? Deadlock detection algorithms Read

Chapt er 3 Sample pr oblems

  • Chap 3: 15, 20, 21
slide-72
SLIDE 72

72

Spare slides

Solution to sleeping barber problem.