N process Peterson algorithm


Introduction

A traditional approach to solving the critical section issue in programming simultaneously for both procedures is Peterson's algorithm. But since you said "N" processes, I presume you mean an altered Peterson's method that can manage over two procedures.

Mutual exclusion is guaranteed by the initial Peterson's method for two distinct processes, yet it can't be immediately expanded to support N methods. Lamport's bakery algorithm, for example, is a deviation and an extra period of Peterson's algorithm which may be applied to N processes.

The N process Peterson Algorithm

Peterson's algorithm which can cope with N processes is called Lamport's bakery algorithm. The sequence of when procedures may access the critical phase is decided using a ticket system. Below is an overall explanation of how it operates−

  • By increasing a common variable identified as a "ticket," every procedure that wishes to proceed to the crucial phase obtains a "ticket."

  • When an operation decides it desires to proceed to the phase that is crucial, it creates a separate "choosing" flag.

  • The procedure patiently awaits as long as its ticket is the smallest among all processes that are running or until whichever process has a less extensive ticket's choosing flag is set to false.

  • The procedure establishes its personal choosing flag to false once it establishes that it has the fewest tickets.

  • The method moves into the crucial phase and carries out the tasks it is intended to.

  • The process releases the opportunity for additional procedures to execute after finishing the crucial section by increasing its ticket number to signify completion.

  • The non-essential tasks part may then be reached in the procedure itself.

Use Cases of N process Peterson algorithm

The Lamport's bakery method, or other N procedures dealing with algorithms, may be used in the following real-time instances −

  • Ticketing System − Several ticketing employees or methods might require to have access to the records or carry out specific tasks simultaneously in a system that sells tickets for happenings or public transportation. In order to guarantee uniformity and avoid disagreements, Lamport's bakery method can be used to make certain that just a single ticketing representative has access to and control the ticket inventory at once.

  • Resource Management − Lamport's bakery method can be used to manage access to shared assets in a system that is distributed where multiple processes are competing for assets like bandwidth on the network or disc access. It hinders resource conflict and guarantees that each procedure receives a proper proportion of the resources at its disposal by giving access in an equitable and neat manner.

  • Concurrent Programming − The Lamport's bakery technique, or comparable computations, can be used to synchronize access to crucial code sections when creating applications with multiple threads or perpendicular algorithm design. The method may be employed, for instance, to regulate the use of discussed information structures or assets in a modeling program where numerous threads simulate various entities in order to prevent corruption of information or discrepancies.

  • Multi-Processor Systems − Lamport's bakery method can be used in structures that have several machines or cores, including a server with numerous processors, in order to control the usage of discussed stores or other resources that are shared. It makes sure the information consistency remains intact and disagreements are avoided by organizing access among the processing devices.

Example

Here's an example implementation of the N process Peterson algorithm in Python.

In this example, the code demonstrates the Peterson algorithm for N processes. Each process enters the critical section and performs its tasks. The critical section is protected by the algorithm, ensuring mutual exclusion among the processes. The output shows the order in which the processes enter and exit the critical section.

import threading

# Shared variables
num_processes = 3
flag = [False] * num_processes
turn = 0

# Function to enter the critical section
def enter_critical_section(process_id):
   flag[process_id] = True
   turn = process_id

   # Check if other processes are in their critical sections
   for i in range(num_processes):
      if i != process_id:
         # Wait until it's the current process's turn or the other process releases the turn
         while flag[i] and turn == process_id:
               pass

   # Critical section
   print("Process", process_id, "in critical section")

   # Exit the critical section
   flag[process_id] = False
   print("Process", process_id, "exited critical section")

# Create threads for each process
threads = []
for i in range(num_processes):
   thread = threading.Thread(target=enter_critical_section, args=(i,))
   threads.append(thread)

# Start the threads
for thread in threads:
   thread.start()

# Wait for all threads to complete
for thread in threads:
   thread.join()

Output

Process 0 in critical section
Process 0 exited critical section
Process 1 in critical section
Process 1 exited critical section
Process 2 in critical section
Process 2 exited critical section

Note − In this modified version, the N process Peterson algorithm emulates the behavior of Peterson's algorithm for two processes by using an array of flags and a turn variable. It provides mutual exclusion and ensures that only one process enters the critical section at a time.

Benefits of N process Peterson algorithm

As a modification of Peterson's method for N operations, Lamport's bakery algorithm provides the following benefits−

  • Mutual Exclusion − A single process may access the critical region at one point thanks to the technique's mutual exclusion warranty. Race conditions are avoided and data reliability is maintained thanks to these possessions.

  • Fairness − Having access to the crucial section is fairly granted thanks to the algorithm.

  • Scalability − The algorithm is appropriate for structures with an unpredictable or substantial amount of simultaneous operations because it can manage any amount of operations.

  • Simplicity − Lamport's bakery method is not particularly easy to comprehend and use in comparison with certain different algorithms for N processes.

Drawbacks of N process Peterson algorithm

As a modification of Peterson's method for N operations, Lamport's bakery algorithm provides the following drawbacks−

  • Increased Complexity − The Peterson algorithm over two separate processes is extended to accommodate N processes, which adds difficulty to the method.

  • Increased Overhead − prior to stepping into the most important phase, the bakery algorithm calls for methods to collect and modify ticket numbers as well as to ascertain the progress of every other step.

  • Limited Scalability − Although the bakery algorithm is capable of managing a wide range of operations, it may not be the best option for numerous procedures.

  • Lack of Priority Management − Process prioritization is not a feature of the bakery algorithm by default.

Conclusion

In conclusion, the issue of critical sections in programming with concurrent processes can be solved using Lamport's bakery method and related N-process managing computation. They guarantee fairness and mutual exclusion, making sure procedures have orderly access to common assets. These methods do, however, have some drawbacks, including higher complexity, overhead, constrained scalability, the absence of important administration, the possibility of starvation, and reliance on shared RAM.

Updated on: 17-Jul-2023

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements