Lecture 12

SFR1811·2022년 3월 2일
0

5.16 Mutual Exclusion

Guarantees a critical section from being experiencing race conditions.

Rules of mutual exclusion:

  1. safety - one thread can be in critical section
  2. speed and orders of threads cannot be manipulated
  3. if a thread is not within entry and exit code of mutual exclusion, it should not block any other thread to enter the critical section
  4. liveness - avoid livelock/indefinite postponement.
  5. avoid starvation - if a thread tries to enter the critical section, it must eventually enter

Additional rule

  1. no temporal barging. by allowing new requests to enter critical section before old requests, it may cause freshness/staleness problem

5.17 Self-Testing Critical Section

void CriticalSection(){
  static uBaseTask * curr; // shared
  curr = &uThisTask();
  for(int i = 1; i <= 100; i += 1){
  	if(curr != &uThisTask()){
      abort("interference");
    }
  }
}

5.18.6 Dekker's mutual exclusion

enum Intent {WantIn, DontWantIn};
Intent * Last; // who was in last time
_Tast Dekker {
  Intent &me, &you;
  void main(){
    for(i = 1~1000){
      for(;;){
        me = WantIn;
       if(you == DontWantIn) break; // no one is awaiting for use
        if(::Last == &me){ // I used last time, so I wait this time
          me = DontWantIn;
          while(::Last == &me && you == WantIn){}
          // shouldn't it set me=WantIn???
        }
      }
      ... // Do Something In Critical Section
      if(::Last != &me) ::Last = &me;
      me = DontWantIn;
    }
  }
 public:
  Dekker(Intent & me, Intent &you) : me(me), you(you) {}
};

This algorithm works even if there is no atomic read/write! - RW-safe

5.18.7 Peterson's mutual exclusion

enum Intent {WantIn, DontWantIn};
Intent * Last; // who was in last time
_Tast Peterson {
  Intent &me, &you;
  void main(){
    for(i = 1~1000){
      me = WantIn;
      ::Last = &me; //RACE!
      while(you == WantIn && ::Last == &me){}
      ... // Do Something In Critical Section
      me = DontWantIn;
    }
  }
 public:
  Peterson(Intent & me, Intent &you) : me(me), you(you) {}
};

This is built based upon a tiny bit of ATOMICITY - atomic read/write!

So its is not RW-safe because it write at ::Last at the same time.


some midterm question
they will give you algorithm, then
what would work if some lines gets inter changed.

profile
3B CS

0개의 댓글