/* Peterson's Algorithm for mutual exclusion */
/* global variables */
#define N 2 /* number of processes */
int turn = 0;
int interested[N];
interested[0]=interested[1]=false;

/* Process 0 */
while (1) {
  NonCriticalSection();
  interested[0] = true;
  turn = 0;
  while (turn == 0 && interested[1]==true) 
    { /* do nothing */ }
  CriticalSection();
  interested[0] = false;
}

/* Process 1 */
while (1) {
  NonCriticalSection();
  interested[1] = true;
  turn = 1;
  while (turn == 1 && interested[0]==true) 
    { /* do nothing */ }
  CriticalSection();
  interested[1] = false;
}
