Critical Section Problem: Algorithm 2a


[Algorithm 4 from 1st edition of text]
Requires an array of boolean values.

var flag: array [0..1] of boolean;


repeat

        flag[i] := true;
        while flag[j] do
                begin
                        flag[i] := false;
                        while flag[j] do no-op;
                        flag[i] := true;
                end;

                critical section

        flag[i] := false;

                remainder section

until false;

This algorithm can suffer starvation if both processes alternate in a certain pattern.