The Critical-Section Problem
n processes competing to use some shared data, where each process has a code segment, called critical section, in which the shared data is accessed. To ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Shared variables:
var turn: (0..1);
initially turn =0
turn=i => Pi can enter its CS
var flag: array[0..1]of bool;
flag[0] = flag[1] = false.
flag[i]=true => Pi ready to enter its critical section
repeat
flag[i]:=true;
turn :=j;
while(flag[j]&turn=j)do
no-op;
CS
flag[i]:=false;
remainder section
until false;
Is it is basically a guard on the critical section of code. While process i is waiting for process j to clear the critical section it sits in a loop doing nothing.