The program text below shows
BAKERY[2], a program that implements
Lamport's
bakery algorithm for mutual exclusion.
Two processes, P1 and P2, coordinate access to a
critical section, where at most one process should reside at any
given time.
Deciding which process enters the critical section is similar to
serving customers at a bakery, where the processes
are the customers and the baker is the critical section,
which can serve only one customer at a time.
Each process selects a ``ticket number'' in
y1 and y2,
as the customers in a store would, and
the process with the lowest number is allowed to enter the critical section.
This is an infinite-state program, since the value of the integer
variables y1 and y2 may grow arbitrarily large.
A ticket with value 0 means that the respective process is not
interested in accessing the critical section.
This informal description of the algorithm can be made precise if the system and its properties are modeled formally. The properties we would like to show are: