Bakery Algorithm
1. Introduction to Bakery Algorithm
The Bakery Algorithm is a software-based solution to the critical section problem for
nprocesses, designed by Leslie Lamport.It ensures that multiple processes can safely access a shared resource (critical section) without interference, similar to taking a number at a bakery.
2. Core Idea
Each process, before entering its critical section, receives a number (ticket).
The process with the smallest number gets to enter the critical section first.
If two processes get the same number, the process with the smaller process ID (PID) enters first.
3. Data Structures Used
n: The total number of processes.choosing[n]: A boolean array wherechoosing[i]istrueif processiis currently picking a number.number[n]: An integer array wherenumber[i]stores the number (ticket) chosen by processi. Initially, allnumber[i]are .
4. Algorithm Steps (for process i)
4.1. Entry Section (Choosing a Number)
Process
iindicates it's choosing a number:choosing[i] = trueProcess
iselects a number by finding the maximum number currently held by any process and adding :number[i] = max(number[0], ..., number[n-1]) + 1Process
ifinishes choosing its number:choosing[i] = false
4.2. Entry Section (Waiting for Turn)
Process
iiterates through all other processesj(from ton-1, excludingi).For each process
j:Wait if process
jis still choosing a number:while (choosing[j] is true) { /* busy-wait */ }Wait if process
jhas a valid number and its turn is before processi's turn:while (number[j] != 0 && (number[j], j) < (number[i], i)) { /* busy-wait */ }(The comparison (number[j], j) < (number[i], i) means:If
number[j] < number[i], then processj's turn is first.If
number[j] == number[i], then the process with the smaller process ID (j < i) has priority.)
4.3. Critical Section
Once all waiting conditions are met, process
ienters its critical section.
4.4. Exit Section
After finishing the critical section, process
iresets its number, indicating it no longer needs the critical section:number[i] = 0
5. Properties Ensured
Mutual Exclusion: Only one process can be in the critical section at any given time.
Progress: If no process is in its critical section and some processes want to enter, then only those processes whose numbers are valid can participate in the decision of which process will enter its critical section next.
Bounded Waiting: A bound exists on the number of times other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before the request is granted.
6. Disadvantages
Busy Waiting: Processes consume CPU cycles while actively waiting in a loop for their turn, which can be inefficient.
Implementation Complexity: The algorithm, especially the lexicographical comparison, can be tricky to implement correctly.
Memory Consistency Issues: Might not work correctly on systems with weak memory models where memory operations are not guaranteed to be ordered as expected without specific hardware instructions (memory barriers).