Figure 1 Critical-Section Algorithm in Psuedocode

var j:0..n;
repeat

   repeat
       flag[i] := want-in;
       j:=turn;
       while j!=i
           do if flag[j]!=idle
               then j:=turn
               else j:=j+1 mod n;
       flag[i]:=in-cs;
       j:=0;
       while (j<n) and (j=i or flag[j]!=in_cs) do j:=j+1;
   until (j>=n) and (turn=i or flag[turn]=idle);
   turn:=i;

   /********************/
   /* Critical Section */
   /********************/

   j:=turn+1 mod n;
   while (flag[j]=idle) do j:=j+1 mod n;
   turn:=j;
   flag[i]:=idle;

   /*********************/
   /* Remainder Section */
   /*********************/

until false;
/* End of File */