Lamport's Bakery Algorithm
In this article, we will understand Lamport's bakery algorithm in detail.
What do you mean by Lamport's bakery algorithm?
Lamport proposed a bakery algorithm, a software solution, for the n process mutual exclusion problem. This algorithm solves a critical problem, following the fairest, first come, first serve principle.
Why is it called the bakery algorithm?
This algorithm is known as the bakery algorithm as this type of scheduling is adopted in bakeries where token numbers are issued to set the order of customers. When a customer enters a bakery store, he gets a unique token number on its entry. The global counter displays the number of customers currently being served, and all other customers must wait at that time. Once the baker finishes serving the current customer, the next number is displayed. The customer with the next token is now being served.
Similarly, in Lamport's bakery algorithm, processes are treated as customers. In this, each process waiting to enter its critical section gets a token number, and the process with the lowest number enters the critical section. If two processes have the same token number, the process with a lower process ID enters its critical section.
Algorithm of Lamport's bakery algorithm
Structure of process Pi for Lamport's bakery algorithm to critical section problem.
This algorithm uses the following two Boolean variables.
All entering variables are initialized to false, and n integer variables numbers are all initialized to 0. The value of integer variables is used to form token numbers.
When a process wishes to enter a critical section, it chooses a greater token number than any earlier number.
Consider a process Pi wishes to enter a critical section, it sets
entering[i] to true to make other processes aware that it is choosing a token number. It then chooses a token number greater than those held by other processes and writes its token number. Then it sets entering[i] to false after reading them. Then It enters a loop to evaluate the status of other processes. It waits until some other process Pj is choosing its token number.
Pi then waits until all processes with smaller token numbers or the same token number but with higher priority are served fast.
When the process has finished with its critical section execution, it resets its number variable to 0.
The Bakery algorithm meets all the requirements of the critical section problem.
Advantages of Lamport's bakery algorithm
Disadvantages of Lamport's bakery algorithm
Next TopicBasic Disk vs Dynamic Disk