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, the process synchronization is a method of coordinating execution of multiple processes so it is ensured that all the 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. Though, it also resolves many other issues related to synchronization in a concurrent system. Therefore, the process synchronization ensures that multiple processes access the shared resources without interfering with one another. It also prevent the possibility of inconsistency in data 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, the 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, the 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.

After knowing these basic terms, now let us discuss the bakery algorithm in process synchronization.

Bakery Algorithm in Process Synchronization

The Bakery Algorithm is a simple process synchronization algorithm which is used for preventing the problem of race conditions in critical sections of the program or in an operating system.

The bakery algorithm is a mutual exclusion algorithm. Hence, it allows multiple processes to access the critical section of the program in a right manner. This algorithm operates by giving a unique number to each process that requests for accessing to the critical section.

The bakery algorithm is based on the first come first serve property. Therefore, the process with the smallest number is given priority to access the critical section first. In case when two or more processes assigned the same number, then the process with lowest process ID is given priority.

In this algorithm, two arrays are maintained, one array is of flags to indicate that a process is currently being requesting to enter to the critical section, and another array is of numbers that indicate the order of processes in which they have requested to enter the critical section.

When a process requests to access the critical section, it first sets its flag to true and then selects the largest number in the array and increases it by one to obtain its own unique number. This process then waits to get access to the critical section.

When the process has entered the critical section, its flag is set to false which indicates that this process is no longer need to access the critical section. Hence, the process releases its number so that the other processes can use it.

Fundamentally, the bakery algorithm in process synchronization ensures that no two processes can access the critical section simultaneously. Although, the bakery algorithm is not free from the problem of starvation, i.e. in this algorithm, a process may have to wait for indefinitely long time if the assigned number is too high.

Bakery Algorithm Data Structures

choosing [0 … n-1] number [0 … n-1] 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] {};
      while number [j] != 0 and (number [j], j) < (number [i], i)
      {};
   }
   critical section number [i] = 0; remainder section
}

Explanation − The execution of the above code is explained below −

Initially, the process sets its "choosing" variable to be true that indicates that it is intended to enter the critical section. After that it is assigned a highest number according to the other processes. The "choosing" variable is then set to false which indicates that it has a number assigned to it. This is the most important part of the bakery algorithm.

The objective of the first three lines of the code is that if a process is modifying its number then at that time some other process should not be allowed to check its old number which is now out of use. After that the numbers of processes are checked where process with lowest number or process ID gets inside the critical section.

Conclusion

In conclusion, the bakery algorithm is a simple and efficient process synchronization algorithm which can be used in multiprocessing systems to synchronize access to a critical section in a code. The implementation of this algorithm is simple and easy which does not require complex data structures and hardware support.

The baker algorithm ensures that all the processes have a fair chance to access the critical section. Another major advantage of bakery algorithm is scalability which means it can handle any number of processes without any degradation in performance. Hence, the bakery algorithm is an efficient and reliable mutual exclusion algorithm used in process synchronization to avoid the critical section problem.

Updated on: 04-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements