Part III Synchronization Deadlocks and Livelocks You think you - - PowerPoint PPT Presentation

part iii
SMART_READER_LITE
LIVE PREVIEW

Part III Synchronization Deadlocks and Livelocks You think you - - PowerPoint PPT Presentation

Part III Synchronization Deadlocks and Livelocks You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program. 1 Fall 2015 Alan J. Perlis Sy Syst stem Mo Model: 1/2


slide-1
SLIDE 1

1

Part III Synchronization

Deadlocks and Livelocks

Fall 2015

You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program. Alan J. Perlis

slide-2
SLIDE 2

2

Sy Syst stem Mo Model: 1/2

  • System resources are used in the following way:

Re Requ ques est: If a process makes a request (i.e., semaphore wait or monitor acquire) to use a system resource which cannot be granted immediately, then the requesting process blocks until it can acquire the resource successfully. Use se: The process operates on the resource (i.e., in critical section). Re Relea ease se: The process releases the resource (i.e., semaphore signal or monitor release).

slide-3
SLIDE 3

3

Sy Syst stem Mo Model: 2/2

Semaphore C[5] = 1; C[i].wait(); C[(i+1)%5].wait(); C[(i+1)%5].signal(); C[i].signal(); has 2 chops and eats

  • uter critical section

left chop locked right chop locked inner critical section

request release use

slide-4
SLIDE 4

4

De Dead adloc

  • ck:

k: De Defini nition

  • n
  • A set of processes is in a de

dead adloc

  • ck state when

every process in the set is waiting for an event that can only be caused by another process in the same set.

  • The key here is that processes are all in the

waiting state.

slide-5
SLIDE 5

5

De Dead adloc

  • ck

k Ne Nece cessa ssary Con y Condi dition

  • ns
  • If a

a de dead adloc

  • ck

k oc

  • ccu

curs rs, t the hen n ea each ch of

  • f the

the fol

  • llow
  • win

ing g fou

  • ur co

r cond nditi tion

  • ns

s mus ust t ho hold. Mut utua ual Ex Excl clus usion

  • n: At least one resource

must be held in a non-sharable way. Hol

  • ld an

d and d Wai ait: A process must be holding a resource and waiting for another. No P

  • Pre

reem empt ption

  • n: Resource cannot be

preempted. Ci Circ rcul ular ar Wai aiti ting ng: P1 waits for P2, P2 waits for P3, …, Pn-1 waits for Pn, and Pn waits for P1.

slide-6
SLIDE 6

6

De Dead adloc

  • ck

k Ne Nece cessa ssary Con y Condi dition

  • ns
  • Not
  • te

e th that at th the e co cond nditi tion

  • ns

s ar are e necessary necessary.

  • Thi

his s mea eans ns i if a a de dead adloc

  • ck

k oc

  • ccu

curs rs AL

ALL

co cond nditi tion

  • ns ar

s are e met et.

  • Sinc

nce e p p  q q is eq s equi uiva valen ent t to to q q  p, , whe here re q mea eans ns n not

  • t al

all co cond nditi tion

  • ns

s ar are e met et and and p mea eans ns n no

  • de

dead adloc

  • ck,

k, as as lon

  • ng

g as as one

  • ne of
  • f the

the fou

  • ur co

r cond nditi tion

  • ns

s fai ails s th ther ere e will be be no no de dead adloc

  • ck.
slide-7
SLIDE 7

7

De Dead adloc

  • ck Pr

k Prev even ention

  • n: 1/

1/7

  • Dea

eadl dloc

  • ck P

k Pre reve vent ntion

  • n means making sure

deadlocks never occur.

  • To this end, if we are able to make sure at least
  • ne of the four conditions fails, there will be no

deadlock.

slide-8
SLIDE 8

8

De Dead adloc

  • ck

k Pr Prev even ention

  • n: 2/

2/7 Mu Mutua ual Ex Excl clus usion

  • n
  • Mutual Exclusion: Some sharable resources

must be accessed exclusively, which means we cannot deny the mutual exclusion condition.

The use of these five chopsticks must be mutually exclusive

slide-9
SLIDE 9

9

De Dead adloc

  • ck

k Pr Prev even ention

  • n: 3/

3/7 Ho Hold an d and d Wai ait

  • Hol
  • ld an

d and d Wai ait: A process holds some resources and requests for other resources.

Each philosopher holds his left chop and waits for his right hold wait

slide-10
SLIDE 10

10

De Dead adloc

  • ck

k Pr Prev even ention

  • n: 4/

4/7 Ho Hold an d and d Wai ait

  • Sol
  • lut

ution

  • n: Make sure no process can hold some

resources and then request for other resources.

  • Two strategies are possible (the monitor solution to

the philosophers problem):

A process must acquire all resources before it runs. When a process requests for resources, it must hold none (i.e., returning resources before requesting for more).

  • Resource utilization may be low, since many

resources will be held and unused for a long time.

  • Starvation is possible. A process that needs some

popular resources my have to wait indefinitely.

slide-11
SLIDE 11

11

De Dead adloc

  • ck

k Pr Prev even ention

  • n: 5/

5/7 Ho Hold an d and d Wai ait

weirdo #4

If weirdo is faster than #1, #1 cannot eat and the weirdo or #4 can eat but not both. If weirdo is slower than #1, #4 can eat Since there is no hold and wait, there is no deadlock.

#1 empty chair #4

In this case, #4 has no right neighbor and can take his right chop. Since there is no hold and wait, there is no deadlock.

#1

The monitor solution with THINKING-HUNGRY-EATING states forces a philosopher to have both chops before eating. Hence, no hold-and-wait.

slide-12
SLIDE 12

12

De Dead adloc

  • ck

k Pr Prev even ention

  • n: 6/

6/7 No No Pre Preem empt ption

  • n
  • This means resources being held by a process

cannot be taken away (i.e., no preemption).

  • To negate this no preemption condition, a

process may deallocate all resources it holds so that the other processes can use.

  • This is sometimes not doable. For example,

while philosopher i is eating, his neighbors cannot take i’s chops away forcing i to stop eating.

  • Moreover, some resources cannot be reproduced

cheaply (e.g., printer).

slide-13
SLIDE 13

13

De Dead adloc

  • ck

k Pr Prev even ention

  • n: 7/

7/7 Ci Circu cular ar Wai aiting ng

  • Circ

rcul ular ar Wai aiti ting ng: P1 waits for P2, P2 waits for P3, …, Pn-1 waits for Pn, and Pn waits for P1.

The weirdo, 4-chair, and monitor solutions all avoid circular waiting and there is no deadlock. Resources can be ordered in a hierarchical way. A process must acquire resources in this particular order. As a result, no deadlock can happen. Prove this yourself.

slide-14
SLIDE 14

14

Li Live veloc

  • ck:

k: 1/ 1/3

  • Livelock: If two or more processes continually

repeat the same interaction in response to changes in the other processes without doing any useful work.

  • These processes are not

not in the waiting state, and they are running concurrently.

  • This is different from a deadlock because in a

deadlock all processes are in the waiting state.

slide-15
SLIDE 15

15

Li Live veloc

  • ck:

k: 2/ 2/3

MutexLock Mutex1, Mutex2; Mutex1.Lock(); // lock Mutex1 while (Mutex2.isLocked()) { // loop until Mutex2 is open Mutex1.Unlock(); // release Mutex1 (yield) // wait for a while // wait for a while Mutex1.Lock(); // reacquire Mutex1 } // OK, Mutex2 is open Mutex2.Lock(); // lock Mutex2. have both Mutex2.Lock(); while (Mutex1.isLocked()) { Mutex2.Unlock(); // wait for a while Mutex2.Lock(); } Mutex1.Lock();

Both processes try to acquire two locks and they yield to each other

slide-16
SLIDE 16

16

Li Live veloc

  • ck:

k: 3/ 3/3

  • Process 1 locks Mutex1 first. If Mutex2 is not

locked, process 1 acquires it. Otherwise, process 1 yields Mutex1, waits for a while (for process 2 to take Mutex1 and finish its task), reacquires Mutex1, and checks again Mutex2 is open.

  • Process 2 does this sequence the same way with

the role of Mutex1 and Mutex2 switched.

  • To avoid this type of livelock, order the

locking se sequence in a a h hierarchical way (i.e., both lock Mutex1 first followed by Mutex2). Thus, only one process can lock both locks successfully.

slide-17
SLIDE 17

17

The End