part iii synchronization
play

Part III Synchronization Software and Hardware Solutions Computers - PowerPoint PPT Presentation

Part III Synchronization Software and Hardware Solutions Computers are useless. They can only give answers. 1 Fall 2015 Pablo Picasso So Softwa ware re So Solut ution ons s for or Two Pr Tw o Proc oces esse ses Suppose we


  1. Part III Synchronization Software and Hardware Solutions Computers are useless. They can only give answers. 1 Fall 2015 Pablo Picasso

  2. So Softwa ware re So Solut ution ons s for or Two Pr Tw o Proc oces esse ses  Suppose we have two processes P 0 and P 1 .  Let one process be P i and the other be P j , where j = 1- i . Thus, if i = 0, then j = 1 and if i = 1, then j = 0.  We wish to design an enter-exit protocol for a critical section to ensure mutual exclusion.  We will go through a few unsuccessful attempts and finally obtain a correct one.  These solutions are pure software-based. 2

  3. At Attemp empt t I: 1/ 1/3  Shared variable process P i turn controls who if it is not my turn, I wait do { can enter the critical section. enter  Since turn is either while (turn != i); 0 or 1, only one can critical section enter. turn = j; exit  However, processes are forced to run in an alternating way. } while (1);  Not ot goo good! d! I am done, it is your turn now 3

  4. At Attemp empt t I: 2/ 2/3  Mut utua ual Ex Excl clus usion on process P i  P 0 in its CS if turn=0 . if it is not my turn, I wait do {  P 1 in its CS if turn=1 .  If P 0 and P 1 are BOTH enter in their CSs, then while (turn != i); turn=0 and turn=1 critical section must BOTH be true. turn = j;  This is absurd, because exit a variable can only hold one and only one value } while (1); (i.e., cannot hold both 0 and 1) at any time. I am done, it is your turn now 4

  5. Attemp At empt t I: 3/ 3/3  Pro rogr gres ess process P i  If P i sets turn to j and if it is not my turn, I wait do { never uses the critical section again, P j can enter enter but cannot enter while (turn != i); again. critical section  Thus, an irrelevant process blocks other turn = j; exit processes from entering a critical section. Not ot good! good! } while (1);  Does bounded waiting hold? Exe xerc rcise se! I am done, it is your turn now 5

  6. Attemp At empt t II: 1/ 1/5 bool flag[2];  Shared variable I am interested flag[i] is the “state” do { wait for you of process P i : interested or not-interested . enter  P i indicates its intention flag[i] = TRUE; while (flag[j]); to enter, waits for P j to exit, enters its section, critical section exit and, finally, changes to “ I am out ” upon exit. flag[i] = FALSE; } I am not interested 6

  7. At Attemp empt t II: 2/ 2/5 bool flag[2];  Mut utua ual Ex Excl clus usion on I am interested  P 0 is in CS if flag[0] do { is TRUE AN AND flag[1] wait for you is FALSE . enter  P 1 is in CS if flag[1] flag[i] = TRUE; is TRUE AN AND flag[0] while (flag[j]); is FALSE . critical section exit  If both are in their CSs, flag[i] = FALSE; flag[0] must be both TRUE and FALSE .  This is absurd. } I am not interested 7

  8. At Attemp empt t II: 3/ 3/5 bool flag[2];  Pro rogr gres ess I am interested  If both P 0 and P 1 set do { wait for you flag[0] and flag[1] to TRUE at the same enter time, then both will flag[i] = TRUE; loop at the while while (flag[j]); forever and no one can critical section exit enter. flag[i] = FALSE;  A decision cannot be made in finite time. } I am not interested 8

  9. At Attemp empt t II: 4/ 4/5 bool flag[2];  Bou ound nded ed W Wai aiti ting ng I am interested  P 0 is in the enter section do { wait for you but switched out before setting flag[0] to TRUE . enter flag[i] = TRUE;  P 1 reaches its CS and sees while (flag[j]); flag[0] being not TRUE . P 1 enters. critical section exit  P 1 can enter and exit in flag[i] = FALSE; this way repeatedly many times. Thus, there is no fixed bound for P 0 . } I am not interested 9

  10. At Attemp empt t II: 5/ 5/5 bool flag[2]; P 0 is switched out here I am interested flag[0] flag[1] P 0 P 1 do { wait for you LOAD #0 F F flag[1]=T F T enter while .. F T flag[i] = TRUE; in CS while (flag[j]); flag[1]=F F F critical section loop back exit flag[i] = FALSE; flag[i] = LOAD i LOAD address flag[i] MOVE T or F to flag[i] } I am not interested a context switch may occur here 10

  11. At Attemp empt t III/A A Co Comb mbina nation on: : 1/ 1/12 12 Peterson’s Algorithm bool flag[2] = FALSE; // process P i int turn; I am interested do { yield to you first flag[i] = TRUE; enter turn = j; while (flag[j] && turn == j); I am done critical section wait while you are interested and it is flag[i] = FALSE; exit your turn. 11 }

  12. At Atte tempt mpt III: : Mu Mutu tual al Exc xclusion usion 2/ 2/12 12 process P i process P j flag[i] = TRUE; flag[j] = TRUE; turn = j; turn = i; while (flag[j] && turn == j); while (flag[i] && turn == i);  If P i is in its critical section, then it sets  flag[i] to TRUE  turn to j (but turn may not be j after this point because P j may ay set it to i later).  and waits until flag[j] && turn == j becomes FALSE 12

  13. At Atte tempt mpt III: : Mu Mutu tual al Exc xclusion usion 3/ 3/12 12 process P i process P j flag[i] = TRUE; flag[j] = TRUE; turn = j; turn = i; while (flag[j] && turn == j); while (flag[i] && turn == i);  If P j is in its critical section, then it sets  flag[j] to TRUE  turn to i (but turn may not be i after this point because P i may ay set it to j later).  and waits until flag[i] && turn == i becomes FALSE 13

  14. At Atte tempt mpt III: : Mu Mutu tual al Exc xclusion usion 4/ 4/12 12 process P i process P j flag[i] = TRUE; flag[j] = TRUE; turn = j; turn = i; while (flag[j] && turn == j); while (flag[i] && turn == i);  If processes P i and P j are both in their critical they are both TRUE sections, then we have:  flag[i] and flag[j] are both TRUE .  flag[i] && turn == i and flag[j] && turn == j are both FALSE .  Therefore, turn == i and turn == j must both be FALSE . 14

  15. At Atte tempt mpt III: : Mu Mutu tual al Exc xclusion usion 5/ 5/12 12 process P i process P j flag[i] = TRUE; flag[j] = TRUE; turn = j; turn = i; while (flag[j] && turn == j); while (flag[i] && turn == i);  Since turn == i and turn == j are both FALSE and since turn is set to j (by P i ) or i (by P j ) before entering the critical section, only one of turn == i and turn == j can be FALSE but not both.  Therefore, we have a contradiction and mutual exclusion holds. 15

  16. At Atte tempt mpt III: : Mu Mutu tual l Exclusion usion 6/1 /12  We normally use the proof by contradiction technique to establish the mutual exclusion condition.  To do so, follow the procedure below:  Find the condition C 0 for P 0 to enter its CS  Find the condition C 1 for P 1 to enter its CS  If P 0 and P 1 are in their critical sections, C 0 and C 1 must both be true.  From C 0 and C 1 being true, we should be able to derive an absurd result.  Therefore, mutual exclusion holds. 16

  17. At Atte tempt mpt III: : Mu Mutu tual l Exclusion usion 7/1 /12  We care about the conditions C 0 and C 1 . The way of reaching these conditions via instruction execution is un-important.  Never use an execution sequence to prove mutual exclusion. In doing so, you make a serious mistake, which is usually referred to as proof by example.  You may use a single example to show a proposition being false. But, you cannot use a single example to show a proposition being true. That is, 3 2 + 4 2 = 5 2 cannot be used to prove a 2 + b 2 = c 2 for any right triangles. 17

  18. At Attempt mpt III: : Progress ess 8/1 /12 process P i process P j flag[i] = TRUE; flag[j] = TRUE; turn = j; turn = i; while (flag[j] && turn == j); while (flag[i] && turn == i);  If P i and P j are both waiting to enter their critical sections, since the value of turn can only be i or j but not both, one process can pass its while loop ( i.e ., decision time is finite).  If P i is waiting and P j is not interested in entering its CS:  Since P j is not interested in entering, flag[j] was set to FALSE when P j exits and P i enters.  Thus, the process that is not entering does not 18 influence the decision.

  19. At Atte tempt mpt III: : Bo Boun unded ded Wa Waiting ting 9/ 9/12 12 process P i process P j flag[i] = TRUE; flag[j] = TRUE; turn = j; turn = i; while (flag[j] && turn == j); while (flag[i] && turn == i);  If P i wishes to enter, we have three cases: 1. P j is outside of its critical section. 2. P j is in its critical section. 3. P j is in the entry section . 19

  20. At Atte tempt mpt III: : Bo Boun unded ded Wa Waiting ting 10 10/1 /12 process P i process P j flag[i] = TRUE; flag[j] = TRUE; turn = j; turn = i; while (flag[j] && turn == j); while (flag[i] && turn == i);  CASE I : If P j is outside of its critical section, P j sets flag[j] to FALSE when it exits its critical section, and P i may enter.  In this case, P i does not wait. 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend