part iii
play

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


  1. 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

  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). 2

  3. Sy Syst stem Mo Model: 2/2 outer critical section left chop locked Semaphore C[5] = 1; request C[i].wait(); C[(i+1)%5].wait(); has 2 chops and eats use C[(i+1)%5].signal(); C[i].signal(); release inner critical section right chop locked 3

  4. Dead De adloc ock: k: De Defini nition on  A set of processes is in a de dead adloc ock 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. 4

  5. De Dead adloc ock k Ne Nece cessa ssary Con y Condi dition ons  If a a de dead adloc ock k oc occu curs rs, t the hen n ea each ch of of the the fol ollow owin ing g fou our co r cond nditi tion ons s mus ust t ho hold .  Mut utua ual Ex Excl clus usion on : At least one resource must be held in a non-sharable way.  Hol old an d and d Wai ait : A process must be holding a resource and waiting for another.  No P o Pre reem empt ption on : Resource cannot be preempted.  Ci Circ rcul ular ar Wai aiti ting ng : P 1 waits for P 2 , P 2 waits for P 3 , …, P n -1 waits for P n , and P n waits for P 1 . 5

  6. De Dead adloc ock k Ne Nece cessa ssary Con y Condi dition ons  Not ote e th that at th the e co cond nditi tion ons s ar are e necessary necessary . rs AL ALL  Thi his s mea eans ns i if a a de dead adloc ock k oc occu curs cond co nditi tion ons 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 ot al all co cond nditi tion ons s ar are e met et and and  p mea eans ns n no o de dead adloc ock, k, as as lon ong g as as one one of of the the fou our co r cond nditi tion ons s fai ails s th ther ere e will be be no no de dead adloc ock. 6

  7. De Dead adloc ock Pr k Prev even ention on: 1/ 1/7  Dea eadl dloc ock P k Pre reve vent ntion on means making sure deadlocks never occur.  To this end, if we are able to make sure at least one of the four conditions fails, there will be no deadlock. 7

  8. De Dead adloc ock k Pr Prev even ention on: 2/ 2/7 Mutua Mu ual Ex Excl clus usion on  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 8

  9. De Dead adloc ock k Pr Prev even ention on: 3/ 3/7 Hold an Ho d and d Wai ait  Hol old an d and d Wai ait : A process holds some resources and requests for other resources. wait Each philosopher holds his left chop and waits for his right hold 9

  10. De Dead adloc ock k Pr Prev even ention on: 4/ 4/7 Ho Hold an d and d Wai ait  Sol olut ution on : 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. 10

  11. De Dead adloc ock k Pr Prev even ention on: 5/ 5/7 Ho Hold an d and d Wai ait empty chair weirdo #1 #1 #4 #4 If weirdo is faster than #1, #1 cannot eat In this case, #4 has no right neighbor and the weirdo or #4 can eat but not both. and can take his right chop. If weirdo is slower than #1, #4 can eat Since there is no hold and wait, Since there is no hold and wait, there is no deadlock. there is no deadlock. The monitor solution with THINKING-HUNGRY-EATING states forces a 11 philosopher to have both chops before eating. Hence, no hold-and-wait.

  12. De Dead adloc ock k Pr Prev even ention on: 6/ 6/7 No No Pre Preem empt ption on  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). 12

  13. De Dead adloc ock k Pr Prev even ention on: 7/ 7/7 Ci Circu cular ar Wai aiting ng  Circ rcul ular ar Wai aiti ting ng : P 1 waits for P 2 , P 2 waits for P 3 , …, P n -1 waits for P n , and P n waits for P 1 . 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. 13

  14. Li Live veloc ock: 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. 14

  15. Li Live veloc ock: 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 15

  16. Li Live veloc ock: 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. 16

  17. The End 17

Recommend


More recommend