Bakery Algorithm in Process Synchronization

It is a prerequisite to understand the terms "process synchronization", "critical section", and "inter-process communication" before we proceed to discuss the Bakery Algorithm in Process Synchronization.

What is Process Synchronization?

In a multiprocessing system, process synchronization is a method of coordinating execution of multiple processes so it is ensured that all processes access shared resources in a controlled and predictable manner.

The primary goal of process synchronization is to avoid the problem of race conditions in the system. It also resolves many other issues related to synchronization in a concurrent system. Therefore, process synchronization ensures that multiple processes access the shared resources without interfering with one another and prevents the possibility of data inconsistency due to concurrent access.

Critical Section in Process Synchronization

In process synchronization, a critical section is a portion of a program that must be executed exclusively by one process at a time. In the critical section, shared resources like hardware devices, data structures, variables, etc. must be synchronized among concurrent processes to ensure consistency.

We use different process synchronization techniques like semaphores, mutexes, etc. to ensure that only one process accesses the critical section at a time. These synchronization techniques provide mutual exclusion and make sure that only one process can enter the critical section at a time.

What is Inter-Process Communication (IPC)?

Interprocess communication is a technique that allows different processes running on a computer to communicate and share data and resources with each other. In multiprocessing systems, interprocess communication is an essential aspect of modern operating systems.

There are various methods to implement interprocess communication such as shared memory, sockets, pipes, message queues, and remote procedure calls (RPC). Each of these methods has its own advantages and disadvantages, and the selection of a method depends on the needs of the application.

Bakery Algorithm in Process Synchronization

The Bakery Algorithm is a mutual exclusion algorithm developed by Leslie Lamport that solves the critical section problem for multiple processes. It is named after the ticket numbering system used in bakeries where customers take a number and are served in order.

This algorithm operates by assigning a unique number to each process that requests access to the critical section. The algorithm follows the first-come-first-serve principle, where the process with the smallest number gets priority to access the critical section first. When two or more processes are assigned the same number, the process with the lowest process ID is given priority.

Algorithm Components

The bakery algorithm maintains two arrays for n processes:

  • choosing[i] Boolean flag indicating that process i is currently selecting its number

  • number[i] The ticket number assigned to process i (0 means not interested in critical section)

Bakery Algorithm Pseudocode

// Shared variables
choosing[0...n-1] = {false}  // Initially all false
number[0...n-1] = {0}       // Initially all zero

// Process i algorithm
while (true) {
    choosing[i] = true;
    number[i] = max(number[0], number[1], ..., number[n-1]) + 1;
    choosing[i] = false;
    
    for (j = 0; j < n; j++) {
        while (choosing[j]) { /* wait */ }
        while (number[j] != 0 && (number[j], j) < (number[i], i)) {
            /* wait */
        }
    }
    
    // Critical Section
    
    number[i] = 0;  // Exit protocol
    
    // Remainder Section
}

How the Algorithm Works

Bakery Algorithm Flow Process wants CS Set choosing[i] = true Get number = max(all numbers) + 1 Set choosing[i] = false Wait for turn, then enter CS

Step-by-Step Execution

  1. Entry Protocol: Process sets choosing[i] = true to indicate it's selecting a number

  2. Number Selection: Process takes the maximum of all current numbers and adds 1

  3. Number Assigned: Process sets choosing[i] = false to indicate number selection is complete

  4. Waiting Phase: Process waits for all other processes to complete number selection, then waits for processes with smaller (number, process_id) pairs

  5. Critical Section: Process enters critical section when its turn arrives

  6. Exit Protocol: Process sets number[i] = 0 to release the critical section

Key Properties

Property Description
Mutual Exclusion Only one process can be in critical section at a time
Progress If no process is in CS and some want to enter, one will eventually enter
Bounded Waiting Every process will eventually get a chance to enter CS
No Hardware Support Works on any architecture without special atomic instructions

Advantages and Disadvantages

Advantages

  • Fairness: Processes are served in first-come-first-serve order

  • No hardware support required: Works on any system without atomic operations

  • Scalable: Can handle any number of processes

  • Starvation-free: Every process eventually gets access to critical section

Disadvantages

  • High time complexity: Each process must check all other processes before entering

  • Space overhead: Requires O(n) space for n processes

  • Busy waiting: Processes continuously check conditions, wasting CPU cycles

Conclusion

The Bakery Algorithm is a fundamental mutual exclusion algorithm that ensures fair access to critical sections without requiring special hardware support. While it has higher overhead compared to hardware-based solutions, it provides a theoretical foundation for understanding process synchronization and demonstrates how software-only solutions can achieve mutual exclusion in concurrent systems.

Updated on: 2026-03-17T09:01:38+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements