Bakery Algorithm for Process Synchronization
Bakery Algorithm for Process Synchronization
Mutual exclusion in the Bakery Algorithm is ensured by the use of ticket numbers. Each process obtains a ticket number one higher than the maximum of existing numbers. Processes enter the critical section in ascending order of their ticket numbers. Tuple comparison in the modified algorithm also plays a key role, where if two processes have the same ticket number, the one with the smaller ID is allowed to enter first. This ordering prevents more than one process from being in the critical section simultaneously .
The Bakery Algorithm ensures progress by allowing a process to enter the critical section if no other process is currently executing it and the process holds the smallest non-zero ticket number. This property is significant as it guarantees that if the critical section is empty, a waiting process can enter without indefinite delay. Progress eliminates the risk of system deadlock, where processes are stuck indefinitely without making progress, thus improving system efficiency and responsiveness in multi-process environments .
The `choosing[]` array in the modified Bakery Algorithm plays a crucial role in eliminating the need for atomic assignment of ticket numbers. When a process is in the phase of selecting its ticket number, `choosing[i]` is set to true, signaling other processes to wait until the selection is complete. This prevents race conditions where multiple processes might interfere with each other’s ticket assignment. This mechanism also enforces a total order of operations by ensuring that no process can proceed with incomplete or inconsistent state observations, thereby enhancing synchronization and fairness .
The Bakery Algorithm is designed to solve the critical section problem for systems with multiple processes, ensuring mutual exclusion, progress, and bounded waiting without hardware support. It uses a ticketing system inspired by real-world bakeries, where each process takes a numbered ticket and waits its turn to enter the critical section based on the order of ticket numbers. This approach ensures that only one process enters the critical section at a time (mutual exclusion), any process can eventually enter if the critical section is free (progress), and processes will not starve because they are served in ticket order (bounded waiting).
Tuple comparison in the Bakery Algorithm is crucial for determining the order in which processes enter the critical section when they have the same ticket number. It is based on the rule that `(a,b) < (c,d)` means `a < c` or `(a == c AND b < d)`. This ensures that even if two processes receive the same ticket number, the one with the smaller process ID enters the critical section first. This approach upholds fairness and prevents scenarios where a process with an equal ticket number might be indefinitely postponed .
Non-atomic operations in the Bakery Algorithm can lead to the scenario where two processes receive the same ticket number if they compute the maximum simultaneously. This violates mutual exclusion, as both processes would consider it their turn to enter the critical section. The modified algorithm addresses this by using an additional array `choosing[]`. This array ensures that a process that is still in the process of choosing a ticket number is recognized by others, preventing simultaneous ticket assignment and ensuring deterministic ordering through tuple comparison .
The design of the Bakery Algorithm was inspired by real-world systems in bakeries and banks where customers manage their turns through a ticketing system. Customers take numbered tokens and wait until their number is called, ensuring service in the order of arrival. Similarly, in the Bakery Algorithm, processes take ticket numbers and enter the critical section in ascending order of these numbers, mimicking the first-come, first-served principle observed in real-life queues .
The statement `num[i] = MAX(num[0..N-1]) + 1` is essential to be atomic in the initial version of the Bakery Algorithm to prevent two or more processes from computing the same maximum value concurrently. If this operation is not atomic, two processes could compute the same maximum and receive identical tickets, violating the mutual exclusion requirement. Atomicity ensures that each process observes a consistent view of the ticket numbers and assigns a unique number, preventing simultaneous entry into the critical section .
The Bakery Algorithm differs from hardware-based synchronization mechanisms as it provides a software solution without requiring atomic operations or machine-level instructions. Instead of relying on special hardware instructions like test-and-set or compare-and-swap, it uses a token-based approach inspired by real-world queuing systems, thus ensuring mutual exclusion, progress, and fairness in a system-independent manner. This software-based approach is more scalable and adaptable across different computing environments .
The Bakery Algorithm guarantees several key properties that ensure fairness and prevent starvation: mutual exclusion (only one process can be in the critical section at a time), progress (if no process is in the critical section, any incoming process can enter), and bounded waiting (processes will be served in order of their tickets, preventing starvation). The algorithm is also scalable, working for any number of processes, and follows a first-come, first-served policy, akin to real-world bakery token systems .