Critical Section Problem: Bakery Algorithm


The Bakery algorithm solves the critical section problem for more than two processes. Its name comes from the way in which a bakery or a deli counter works: customers choose a unique number that is larger than any previously choosen number and are then served in turn.

First, some notation:


We also the following data structures:

var choosing: array [0..n-1] of boolean;
number: array [0..n-1] of integer;

The choosing array is used to indicate that a process wants to enter it's critical section and is in the middle of selecting a number.

The number array contains the numbers associated with each process.