Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
N process Peterson algorithm
The N-process Peterson algorithm addresses the critical section problem for multiple processes, extending beyond Peterson's original two-process solution. While Peterson's classic algorithm handles mutual exclusion between two processes, scaling it to N processes requires more sophisticated approaches like Lamport's Bakery Algorithm.
How the N-Process Algorithm Works
The N-process extension uses a ticket-based system where processes obtain numbered tickets to determine their order of access to the critical section. Here's how it operates:
Each process wanting to enter the critical section gets a unique ticket number by incrementing a shared counter
The process sets a "choosing" flag while obtaining its ticket
Before entering, the process waits until it has the smallest ticket number among all active processes
Once confirmed as having priority, the process enters the critical section
After completing critical work, the process resets its ticket and choosing flag
Example Implementation
Here's a Python implementation demonstrating the N-process Peterson algorithm concept:
import threading
import time
# Shared variables
num_processes = 3
flag = [False] * num_processes
turn = 0
ticket = [0] * num_processes
choosing = [False] * num_processes
next_ticket = 1
def enter_critical_section(process_id):
global next_ticket
# Get ticket (simplified bakery algorithm concept)
choosing[process_id] = True
ticket[process_id] = next_ticket
next_ticket += 1
choosing[process_id] = False
# Wait for turn based on ticket priority
for i in range(num_processes):
if i != process_id:
while choosing[i]: # Wait if other process is choosing
pass
while (ticket[i] != 0 and
(ticket[i] < ticket[process_id] or
(ticket[i] == ticket[process_id] and i < process_id))):
pass
# Critical section
print(f"Process {process_id} entered critical section (ticket: {ticket[process_id]})")
time.sleep(0.1) # Simulate work
print(f"Process {process_id} exiting critical section")
# Reset ticket
ticket[process_id] = 0
# Create and start threads
threads = []
for i in range(num_processes):
thread = threading.Thread(target=enter_critical_section, args=(i,))
threads.append(thread)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
Process 0 entered critical section (ticket: 1) Process 0 exiting critical section Process 1 entered critical section (ticket: 2) Process 1 exiting critical section Process 2 entered critical section (ticket: 3) Process 2 exiting critical section
Advantages and Disadvantages
| Advantages | Disadvantages |
|---|---|
| Mutual Exclusion: Guarantees only one process in critical section | Complexity: More complex than two-process Peterson algorithm |
| Fairness: First-come-first-served ticket ordering | Overhead: Requires ticket management and checking |
| Deadlock-Free: No circular wait conditions | Memory Usage: Requires O(N) shared variables |
| Starvation-Free: Every process eventually gets access | Performance: Busy waiting can waste CPU cycles |
Common Use Cases
Database Systems Managing concurrent access to shared database records
Operating Systems Process synchronization in kernel-level operations
Distributed Systems Coordinating resource access across multiple nodes
Multi-threaded Applications Protecting shared data structures in concurrent programs
Conclusion
The N-process Peterson algorithm, exemplified by Lamport's Bakery Algorithm, extends mutual exclusion to multiple processes using a ticket-based approach. While it guarantees fairness and prevents deadlock, it comes with increased complexity and overhead compared to simpler two-process solutions.
