Mutual Exclusion Problem

后端 未结 6 1097
陌清茗
陌清茗 2021-01-13 16:47

Please take a look on the following pseudo-code:

boolean blocked[2];
int turn;
void P(int id) {
      while(true) {
             blocked[id] = true;
                 


        
6条回答
  •  生来不讨喜
    2021-01-13 17:36

    Mutual Exclusion is in this exemple not guaranteed because of the following:

    We begin with the following situation:

    blocked = {false, false};
    turn = 0;
    

    P1 is now executes, and skips

      blocked[id] = false; // Not yet executed.
    

    The situation is now:

    blocked {false, true}
    turn = 0;
    

    Now P0 executes. It passes the second while loop, ready to execute the critical section. And when P1 executes, it sets turn to 1, and is also ready to execute the critical section.

    Btw, this method was originally invented by Hyman. He sent it to Communications of the Acm in 1966

提交回复
热议问题