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:
( a, b ) < ( c, d ) if a < c or if a = c and b < d.
Used to determine the order of processes. Notice that (number[i],i) can be unambigously ranked relative to all other like pairs of process numbers and PIDs.
max( a0, a1, ..., an-1 ) is a number k such that k >= ai for i=0,1,...,n-1.
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.