Peterson’s Algorithm

boolean flag [2];
int turn;
void P0() {
  while (true) {
    flag [0] = true;
    turn = 1;
    while (flag [1] && turn == 1) /* do nothing */;
    /* critical section */;
    flag [0] = false;
    /* remainder */;
  }
}
void P1() {
  while (true) {
    flag [1] = true;
    turn = 0;
    while (flag [0] && turn == 0) /* do nothing */;
    /* critical section */;
    flag [1] = false;
    /* remainder */
  }
}
void main() {
  flag [0] = false;
  flag [1] = false;
  parbegin (P0, P1);
}