Mutual Exclusion Problem

后端 未结 6 1091
陌清茗
陌清茗 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:30

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

    We begin with the following situation:

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

    The execution runs as follows:

    P0: while (true) {
    P0:   blocked[0] = true;
    P0:   while (turn != 0) {
    P0:     while (blocked[1]) {
    P0:     }
    P1: while (true) {
    P1:   blocked[1] = true;
    P1:   while (turn != 1) {
    P1:   }
    P1:   criticalSection(P1);
    P0:     turn = 0;
    P0:   while (turn != 0)
    P0:   }
    P0:   critcalSection(P0);
    

提交回复
热议问题