0% found this document useful (0 votes)
35 views6 pages

Bakery Algorithm for Process Synchronization

The Bakery Algorithm is a software-based synchronization method for managing critical sections among multiple processes, inspired by real-world queueing systems like bakeries. It uses a ticketing system to ensure mutual exclusion, progress, and bounded waiting, allowing processes to enter the critical section in a fair order based on their assigned ticket numbers. The algorithm can operate without hardware support and is scalable for any number of processes.

Uploaded by

Tejaarka Piridi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

Bakery Algorithm for Process Synchronization

The Bakery Algorithm is a software-based synchronization method for managing critical sections among multiple processes, inspired by real-world queueing systems like bakeries. It uses a ticketing system to ensure mutual exclusion, progress, and bounded waiting, allowing processes to enter the critical section in a fair order based on their assigned ticket numbers. The algorithm can operate without hardware support and is scalable for any number of processes.

Uploaded by

Tejaarka Piridi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bakery Algorithm - Critical Section Solution (Lecture Notes)

### Bakery Algorithm - Software Solution for Critical Section ###

**1. Introduction**

- The Bakery Algorithm is a *software-based synchronization algorithm* designed for systems with

**multiple processes** (N > 2).

- Invented by **Leslie Lamport**.

- Inspired by real-world systems in bakeries and banks, where customers take numbered tokens and

wait their turn.

**Analogy:**

- Each customer (process) takes a numbered ticket.

- The counter serves customers in **increasing order of ticket numbers**.

- When your number is displayed, you are allowed to proceed (enter the critical section).

---

**2. Problem Setting**

- There are N processes: P0, P1, ..., PN-1.

- A shared array `num[0..N-1]` holds ticket numbers.

- Initially, all `num[i] = 0`.

**Functions:**

1. `lock(i)` - executed before entering the critical section.

2. `unlock(i)` - executed after leaving the critical section.

Page 1
Bakery Algorithm - Critical Section Solution (Lecture Notes)

**At unlock:**

```c

num[i] = 0;

```

---

**3. Lock Function (Simplified Version)**

```c

lock(i):

num[i] = MAX(num[0..N-1]) + 1;

for (p = 0; p < N; p++) {

while (num[p] != 0 && num[p] < num[i]);

```

- The process `i` gets a token number one higher than the current maximum.

- It waits until:

- Any process with a smaller token finishes, and

- Processes not competing (`num[p] = 0`) are ignored.

---

**4. Working Example**

Suppose there are **five processes**: P1 to P5.

- Initially: `num[] = [0, 0, 0, 0, 0]`

- They all invoke `lock(i)` almost simultaneously.

Page 2
Bakery Algorithm - Critical Section Solution (Lecture Notes)

| Process | num[i] Assigned |

|----------|----------------|

| P3 | 1 |

| P4 | 2 |

| P5 | 3 |

| P2 | 4 |

**Execution order:**

- P3 has the lowest nonzero num -> enters critical section first.

- After finishing, P3 sets `num[3] = 0`.

- Next smallest value (P4=2) enters.

- Then P5 (3), and finally P2 (4).

Thus, **critical section is accessed in the order of ticket numbers.**

---

**5. Need for Atomic Operation**

- The statement `num[i] = MAX(num[0..N-1]) + 1` must be **atomic**.

- If not atomic, two processes might receive the **same ticket number**.

**Example:**

If P4 and P5 both calculate MAX=1 at the same time, both set `num=2` -> both think it's their turn ->

**mutual exclusion violated**.

Hence, atomicity ensures **no duplicate ticket numbers**.

Page 3
Bakery Algorithm - Critical Section Solution (Lecture Notes)

---

**6. Original Bakery Algorithm (Non-Atomic Version)**

To remove the need for atomic operations, Lamport introduced another shared array:

```c

bool choosing[N]; // initially all false

```

**Modified Algorithm:**

```c

lock(i):

choosing[i] = true;

num[i] = 1 + max(num[0..N-1]);

choosing[i] = false;

for (p = 0; p < N; p++) {

while (choosing[p]); // wait if another process is choosing

while (num[p] != 0 &&

(num[p], p) < (num[i], i)); // tuple comparison

```

**unlock(i):**

```c

Page 4
Bakery Algorithm - Critical Section Solution (Lecture Notes)

num[i] = 0;

```

---

**7. Tuple Comparison Explanation**

A process waits if another has:

- A smaller ticket number (`num[p] < num[i]`), or

- The same ticket number but **smaller process ID (p < i)**.

Formally:

```

(a,b) < (c,d) means:

a < c OR (a == c AND b < d)

```

This ensures deterministic ordering even when two processes get equal tickets.

---

**8. Example with Choosing Array**

If P3 gets ticket 1, and P4, P5 both get ticket 2:

- P3 executes first (smallest ticket).

- Between P4 and P5 (same ticket = 2), the one with smaller ID (P4) enters first.

- Then P5 executes, followed by others.

Thus, **mutual exclusion**, **progress**, and **bounded waiting** are all ensured.

Page 5
Bakery Algorithm - Critical Section Solution (Lecture Notes)

---

**9. Properties of Bakery Algorithm**

1. **Mutual Exclusion:** Guaranteed - one process enters CS at a time.

2. **Progress:** If no one is in CS, a requesting process can enter.

3. **Bounded Waiting:** Processes are served in ticket order; no starvation.

4. **Scalability:** Works for any N processes.

5. **Fairness:** First-come, first-served (like real bakery tokens).

---

**10. Summary**

- Bakery Algorithm solves the critical section problem for **N > 2** processes.

- Inspired by real-world queueing systems.

- Works without hardware support.

- Ensures fairness and correctness through token-based sequencing.

Page 6

Common questions

Powered by AI

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 .

You might also like